在 System.out.println("hello") 期间调用了多少次 memcpy?

How many times memcpy is called during System.out.println("hello")?

我认为当常量字符串 "hello" 从 Java 堆(用户 space)复制到内核 Space.

时会调用 memcpy

我想还有更多,是吗?

也许你可以自己调查一下。起点可以是

Foo.java

class Foo {
    public static void main(String[] args) {
        System.out.println("before");
        System.out.println("hello world");
    }
}

编译它

javac Foo.java

然后检查 memcpy 个调用

ltrace -f -S -e memcpy java Foo 2>&1 \
    | sed -n "/before/,/hello/ {/\(before\|hello\|>memcpy\)/p}"

产出

[pid 18509] write@SYS(1, "before", 6before)            = 6
[pid 18509] libjvm.so->memcpy(0x7fc2325f7410, 0xf341dec0, 1, 0xc0100800 ...
[pid 18509] libjvm.so->memcpy(0x7fc2325f73b0, 0xf341dec0, 11, 0xc0100800 ...
[pid 18509] write@SYS(1, "hello world", 11hello world)      = 11