为什么Socket获取的字节流先要拷贝到堆外内存然后再复制到Heap区域

playboy0651140 2015-09-21
为什么Socket获取的字节流先要拷贝到堆外内存然后再复制到Heap区域呢?是什么原因造成从堆内内存获取字节流必须要经过
playboy0651140 2015-09-22
Operating systems perform I/O operations on memory areas. These memory areas, as far as the operating system is concerned, are contiguous sequences of bytes. It's no surprise then that only byte buffers are eligible to participate in I/O operations. Also recall that the operating system will directly access the address space of the process, in this case the JVM process, to transfer the data. This means that memory areas that are targets of I/O perations must be contiguous sequences of bytes. In the JVM, an array of bytes may not be stored contiguously in memory, or the Garbage Collector could move it at any time. Arrays are objects in Java, and the way data is stored inside that object could vary from one JVM implementation to another.



For this reason, the notion of a direct buffer was introduced. Direct buffers are intended for interaction with channels and native I/O routines. They make a best effort to store the byte elements in a memory area that a channel can use for direct, or raw, access by using native code to tell the operating system to drain or fill the memory area directly.


我理解了下说在堆内存中在gc的时候涉及到字节数组的拷贝所以socket不能直接操作那个区域,但是并不理解 In the JVM, an array of bytes may not be stored contiguously in memory这句话的含义
Global site tag (gtag.js) - Google Analytics