This is the 18th day of my participation in the August Challenge

A lot of readers have asked me about the KNOWLEDGE of JVM, so I went online to find some information.

A PDF download of this article is available at the end of the article.

First, the JVM memory region is dependent

1. The memory model of THE JVM and partitioning conditions and functions

As shown below:

The yellow part is common to threads, and the blue part is private to threads.

The method area is used to store data such as class information, constants, and static variables loaded by the VIRTUAL machine.

The heap holds object instances, and all objects and arrays are allocated on the heap. Is the largest area of memory managed by the JVM.

Stack Java method execution memory model: stores information about local variables, operand stacks, dynamic links, method exits, etc. The life cycle is the same as that of a thread.

The function of the local method stack is similar to that of the VIRTUAL machine stack. The local method serves the native method, while the virtual machine stack serves the Java method executed by the VIRTUAL machine.

The program counter is an indicator of the line number executed by the current thread, and is the smallest area of memory in the JVM. When performing bytecode work, the program counter is used to select the next bytecode instruction to be executed.

2.Java memory allocation

Registers: We have no control over static fields: static members defined by static. Constant pool: Symbolic references (fully qualified names of classes and interfaces, field names and descriptors, method and name and descriptors) to final constant values and some text modifications that are determined and stored in.class files at compile time. Non-ram storage: Permanent storage space such as hard disks. Heap memory: Objects and arrays created by new are managed by the Java virtual Machine automatic garbage collector and are accessed slowly. Stack memory: Variables of basic types and reference variables of objects (the access address of the heap memory space) are fast and can be shared, but the size and lifetime must be determined, which is inflexible.

What is the structure of the Java heap? What is Perm Gen space in the heap?

The JVM’s heap is the run-time data area on which all instances and arrays of classes are allocated memory. It is created when the JVM starts. The heap memory occupied by objects is reclaimed by the automatic memory management system, the garbage collector.

Heap memory is made up of living and dead objects. Live objects are accessible to the application and are not garbage collected. Dead objects are objects that are inaccessible to the application and have not yet been collected by the garbage collector. These objects occupy heap memory space until the garbage collector collects them.

3. Memory leaks in Java? Briefly describes the

A memory leak is when an object or variable that is no longer being used by a program remains in memory.

Java has a garbage collection mechanism that ensures that when an object is no longer referenced, that is, orphaned, it is automatically removed from memory by the garbage collector. Because Java uses a directed graph for garbage collection management, it eliminates the problem of reference loops, such as two objects that refer to each other and can be reclaimed as long as they are unreachable from the root process.

Memory leaks in Java: long life cycle of object reference object holds a short life cycle is a memory leak can occur, although a short life cycle object has no longer need to, but because of long life cycle object holds its reference that can’t be recycled, which is the Java memory leaks in the scene, popular said, A programmer may create an object that is never used again, but the object is always referenced, that is, the object is useless but cannot be collected by the garbage collector. This is a possible memory leak in Java, for example: In the cache system, we load an object and put it in the cache (for example, in a global map object) and then never use it again. The object is always referenced by the cache but no longer used.

To check for a memory leak in Java, make sure that the program executes all branches to the end of the program, and then see if an object is used. If not, the object is a memory leak.

If a class method returns an instance of the object of an inner class instance objects, the inner class object was cited for a long time, even if the external class instance objects no longer be used, but as a result of the inner class lasting external instance objects of a class, the outer class object will not be garbage collected, this can cause a memory leak.

Another case of a memory leak: When an object is stored in a HashSet, it cannot modify any of the fields in the object that contributed to the hash. Otherwise, the modified hash value of the object is different from the original hash value stored in the HashSet. In this case, Even if the Contains method retrieves the object from the HashSet collection using the current reference to the object as an argument, the object cannot be found, which would make it impossible to remove the current object separately from the HashSet collection, causing a memory leak.