JAVA technology sharing: java Direct Memory

  Direct Memory

  Speaking directly to a lot of memory, think outside the heap memory. But Conceptually, the two do not the same thing.

  External memory heap is the memory outside of the heap, we have a common metadata area, stack space, as well as direct memory, as well as the jvm c ++ program, mmap data.

  Direct memory and non-heap is not a concept, nonheap main point is that the metadata area, code cache and so on. Non-heap composition can be viewed by jconsole, there is a clear partition and content, but this is called non-heap, it is easy to misunderstand.

  Direct memory jvm parameters are controlled -XX: MaxDirectMemorySize. One that has control parameters, everyone would understand that he is in fact part of the planning jvm out of their own, and certainly the memory system, and his presence can be said that the metadata area is the same level. Both are jvm own regional planning out, to store some data.

  Gc not influence?

  It said part of the memory management is not JVM garbage collection on a lot of books. This argument is not very accurate, if you want to completely unaffected by gc, then he must not be purely and heap deal with our common jni, jni to write their own magical memory, there is no variable to control enjoyed by the method to apply ,freed. This is called gc not affected.

  The most common ByteBuffer through the code of java application, but there is a reference in the heap. And it really does not completely unaffected by the gc. His application of these low-level control of memory operations, gc really is and can not be recycled, if we apply the ByteBuffer, but did not release, it would mean that a memory leak, so the gc java to play a role at this time, not in the object heap quoted, when gc java objects can be recycled, but the object is equivalent to direct representation of memory, when the representative may be gc, he also called java code to release this memory, and ultimately achieve the purpose of the release, to avoid influences.

  Here is the release of java code.

  

 

  He applied not bring fullgc-System.gc

  

 

  From this we can see that, in fact, indirectly affected by gc of this space, but not without.

  Copy at least one layer

  虽然很多人拿直接内存和0拷贝在一起讨论,这个也不是一回事,0拷贝算是系统的特性的部分了,直接内存顶多是少一层考虑,正常文件读取到堆,需要文件到系统内存,系统内存然后通过jni把数据拷贝到堆里。如果用直接内存去读取文件的数据,只要不是用来做处理,那么他是不需要拷贝到堆的,当然如果说,你要用来做处理,那么他本身也没什么优势了。

  小结

  直接内存和堆外内存不一样

  直接内存本身不受gc的影响,但是由于有对象在堆引用这这块内存,那么受到gc的间接影响,典型的是java的代码里有system.gc去回收。

  直接内存也和0拷贝不是一回事。

  直接内存其实是jvm定义的空间,可以说堆,元数据区算在同一层次。

 

Guess you like

Origin www.cnblogs.com/qfjavabd/p/10969034.html