Detailed external memory heap java


Outer heap memory and heap memory

   Heap external memory, also known as direct memory (Direct Memory) is not part of the data area of ​​the virtual machine is running, nor is it Java virtual machine memory area defined in the specification. Javaer has long been an area of ​​concern are difficult, and today we are together explore this area lurks what stuff ????

    JVM memory can be exceptionally used in two kinds: external memory heap and the heap memory.

We look at that we have a relatively familiar in the heap memory:

Java heap (JAva Heap) is a Java virtual machine memory management in the largest piece of .Java heap is shared by all threads in a memory area is created when the virtual machine starts. The sole purpose of this memory area is stored object instance, almost all object instances are all here to allocate memory that describes the Java virtual machine specification is: all object instances and arrays to be allocated on the heap, but with the development of JIT compiler technology matures and escape analysis allocated on the stack, replacing the scalar optimization techniques will result in some subtle changes, all objects are allocated on the heap gradually becomes less "absolute", and all have a heap outside the concept of memory.

   Heap memory is completely responsible for allocating and freeing the JVM, if the program is not buggy code results in a memory leak, then you will not encounter this error java.lang.OutOfMemoryError.
    Use external heap memory, it is to directly allocate and free memory and improve efficiency. After JDK5.0, the code can be directly operated local memory mode has two kinds: Unsafe and using the unpublished package under NIO ByteBuffer.
We take a look at ByteBuffer NIO provided:

We will set the maximum heap memory outside 40M, running this code will find: program can run forever, will not report OutOfMemoryError. If the -verbose: gc -XX: + PrintGCDetails, the program will find frequent garbage collection activities. So DirectByteBuffer exactly how to release heap memory outside?

    We modify the JVM startup parameters under the code before the re-run:

Compared with the previous JVM startup parameters, increasing the -XX: + DisableExplicitGC, this parameter is a call to action GC prohibit code. GC code shows how to call it, through the function call System.gc (). If we add to this JVM startup parameters, then the code to call System.gc () has no effect, there is no equivalent to this line of code the same.

Obviously heap memory (including the old and the new generation's) memory is sufficient, but external memory heap overflow. That NIO memory is recovered directly, we need to rely on System.gc (). If our application uses direct memory java nio in, then use the -XX: + DisableExplicitGC must be careful, there is a potential risk of memory leaks. 

 We know that java code can not be enforced when the JVM garbage collection, garbage collection that is triggering this action, fully controlled by its own JVM, it would be useless java object to pick the right time to reclaim heap memory. Code shows the call System.gc (), JVM is recommended only for garbage collection, but in the end will not perform garbage collection is uncertain, garbage collection may, or may not. When is it the right time? In general, the system is relatively idle time (such as thread activity JVM little time), there is not enough memory, had to garbage collection. In our example that the fundamental contradiction: JVM heap memory managed by themselves, outside the heap memory must be released by our own; heap memory consumption rate is far less than the external heap memory consumption, but the problem is that you must first release the heap memory object to release heap memory outside, but we can not force the release of JVM heap memory.

 Direct Memory recall mechanism: Direct Memory GC is controlled by, e.g. ByteBuffer bb = ByteBuffer.allocateDirect (1024), execution of the code will take 1k memory outside the stack, only occupy a pointer within the Java heap object reference this 1k of space outside of the size of the heap bb only when the object is recovered will be recycled, will find here an obvious asymmetry is outside the heap may take up a lot, but did not take up much in the reactor, resulting in further did not trigger GC, it is prone to Direct memory cause physical memory run out.

 Direct ByteBuffer out of memory allocated by the GC is actually responsible for recycling, rather than Unsafe is completely self-managed, Hotspot in the GC scans whether Direct ByteBuffer objects have references, but also if there is no recovery of the external heap memory occupied by .

Use external heap memory and object pooling can reduce the GC pause time, which is their only common denominator. The short life cycle of a variable object, creating a large overhead, although the long life cycle or variable objects exist but are more suitable for use redundant object pooling. Moderate cycle life, or complex objects are more suitable for processing by the GC. However, the object variable in the long life cycle is more difficult, and the external memory heap is precisely their dishes.

The benefits of heap memory is outside:

(1) can be extended to a larger memory space. For example more than 1TB even larger than the main memory space;

(2) can theoretically reduce the GC pause time;

(3) may be shared between processes, replication between the decrease target JVM, the JVM so divided deployment easier to implement;

(4) its persistent storage can support quick restart, while also being able to reproduce the production data in test environments

Standing on the perspective of system design point of view, the use of external heap memory can provide more possible for you to design. The most important upgrade is not the performance

Why can improve memory heap outside IO efficiency?

  The heap memory management by the JVM, in "user mode"; and an outer heap memory managed by the OS, a "kernel mode." If the write data from the disk within the stack, data is first copied to the external memory heap, i.e. the kernel buffer and then written to disk by the OS, it avoids heap memory using an external copy of the user within data from kernel mode.

Why use external memory heap

Improve garbage collection pause
because full gc mean a complete recovery, when complete recovery, all within the allocated heap memory garbage collector will carry out a full scan, which means that an important fact - so garbage collection for Java applications the impact, with the heap size is directly proportional. Heap too much can affect the performance of Java applications. If you use the external heap memory, then external heap memory is directly affected by the management operating system (rather than a virtual machine). The result of this is to maintain a smaller internal heap memory, in order to reduce the impact of garbage collection applications.
In some scenarios program can improve I / O performance manipulation. Less to the copied data from the heap to step outside the heap memory.
Under what circumstances the use of external memory heap

External memory heap for medium or long life cycle of the object. (If it is short lived objects, in YGC when it was recovered, there is no longer affect the performance of large memory and the lifetime of an object caused by the FGC application).
Direct file copy operation, or I / O operation. Heap memory can be used as a small external memory from the user memory to be copied to system memory operation, since I / O operation is communication between the kernel and the device memory, the program directly and not through the peripheral communication.
Meanwhile, + cell stack may also be used combinations of external memory to a short life cycle, but directed to the object I / O operation is reusable outer heap memory. (Netty on the use of the embodiment)
stack memory characteristics of the outer

There are good scalability for large memory
garbage collection pauses can clearly feel the improvement
can be shared between processes, reducing replication between virtual machine
 
heap memory outside of some of the problems

Heap external memory recall problems, as well as heap memory leak outside. This source has been resolved in the above mentioned
data structure issues outside the heap memory: heap biggest problem is your external memory data structures become less intuitive, if the data structure is more complex, it is necessary to serialize (serialization ), while the serialization itself will affect performance. Another problem because you can use more memory, you may begin to worry about the impact of virtual memory (ie hard drive) speed for you.
 

Published 86 original articles · won praise 267 · Views 1.77 million +

Guess you like

Origin blog.csdn.net/javastart/article/details/104798676