Java runtime memory region partitioning (image from network)

Above:

  • Program Counter RegisterA program counter is a small memory area that indicates the line number of the bytecode executed by the current thread. It can be understood as a line number indicator for the current thread. The bytecode interpreter works by changing the value of this counter to fetch the next statement instruction. Each program counter is used only to record the line number of one thread, so it is thread private (one program counter per thread). If the program executes a Java method, the counter records the address of the virtual machine bytecode instruction being executed. If you are executing a native method, the counter value is Undefined. Since the program counter only records the current instruction address, there is no memory overflow. Therefore, The program counter is also the only area of any JVM memory region where OutOfMemoryError is not defined.
1, each thread has a program counter, thread-private 2, that executes Java methods, and the counter records the address of the virtual machine bytecode instructions being executed. Execute native method, counter value is Undefined. 3. The counter only records addresses, so it is the only area of JVM memory that does not have OOM defined.Copy the code
  • JVM Stack: When executing each method of a thread, a Statck Frame will be created, which stores local variable tables, operation stations, dynamic links, method exits, etc. When the method is called, the stack Frame will be pushed into the JVM stack, and when the method is executed, the stack Frame will be pushed out of the stack. Local variables table stores local variables related to methods, including various basic data types, object references, return addresses, etc. In the local variable table, only the long and double types occupy two local variable Spaces (Slot, for 32-bit machines, a Slot is 32 bits), and all the others are one Slot. Note that the local variable table is determined at compile time. The amount of space allocated for a method to run is fully determined in the stack frame and does not change during the lifetime of the method. Two exceptions are defined in the virtual machine stack. If the stack depth of the thread call is greater than the maximum depth allowed by the virtual machine, a StatckOverFlowError is raised. Most Java virtual machines, however, allow dynamic expansion of the virtual stack size (a small percentage is fixed), so threads can apply for stacks until they run out of memory, at which point outofMemoryErrors are thrown. Each thread corresponds to a virtual stack, so the virtual stack is also thread-private.
1, each method in a thread will create a stack frame at the same time of execution, method call on the stack, execution end of the stack. 2. The local variable table is determined at compile time, and the space allocated for the method to run is completely determined in the stack frame, which will not change during the lifetime of the method. 3. Two exceptions' StatckOverFlowError 'and' OutOfMemoryError 'are defined in the vm stack. 4. The virtual stack is also thread private.Copy the code
  • Native Method StatckThe local method stack is identical to the virtual machine stack in terms of function, operation mechanism, exception type, etc. The only differences are:The virtual machine stack executes Java methods, andThe native method stack is used to execute native methodsIn many virtual machines (such as the HotSpot VIRTUAL machine, the default of Sun’s JDK), the local method stack is used alongside the virtual machine stack. The local method stack is also thread-private.
1. It has the same function and operation mechanism as the virtual machine stack, except that the virtual machine stack executes Java methods while the local method stack executes native methods. 2. The local method stack is also thread-private.Copy the code
  • Heap area: heap area is understoodThe most important area of the Java GC mechanismThere is no one. The heap is the largest area of memory managed by the JVM, and the main area of memory managed by the Java GC mechanism. The heap is shared by all threads and created at virtual machine startup.The heap area exists to store object instancesIn principle, all objects are allocated memory on the heap (although in modern technology, this is not absolute, but also directly allocated on the stack). In general, according to the Java Virtual Machine specification,Heap memory needs to be logically contiguous (not physically), which can be fixed size or extensible. Currently, most VMS are extensible. OutOfMemoryError:Java Heap Space exception is thrown if, after garbage collection, there is still not enough memory allocated and no further expansion is possible.
1. The heap is shared by all threads and is created when the virtual machine is started. 2. The heap exists to store object instances. 3. Heap memory needs to be logically continuous. 4. If there is not enough memory allocated after GC, OutOfMemoryError:Java Heap Space exception will be raised.Copy the code
  • Method AreaIn the Java Virtual Machine specification, the method area is treated as a logical part of the Heap, but in fact, the method area is not a non-heap. The method area is the area shared by each thread. It is used to store the class information that has been loaded by the VIRTUAL machine (that is, the information that needs to be loaded when a class is loaded, including the version, field, method, interface, etc.), final constants, static variables, and code compiled by the compiler on the spot. Method areas also do not need to be physically contiguous, can be fixed or scalable, and have one more limitation than the heap: you can choose whether to perform garbage collection. In general,Very little garbage collection is performed on the method areaThis is one of the reasons why a method area is called a HotSpot, but it does not mean that there is no garbage collection on the method area at allGarbage collection is mainly about memory reclamation for constant pools and unloading of loaded classes. OutOfMemoryError:PermGen Space exception is defined on the method area, which is thrown when memory is low.Runtime Constant PoolIs a part of the method area, used to store literal constants generated at compile time, symbolic reference, translated direct reference (symbolic reference is the encoding is a string to represent the location of a variable, interface, direct reference is translated according to the symbolic reference address, will be translated in the class link stage); The runtime constant pool can store compile-time constants as well as run-time constants. (For example, the String intern() method maintains a constant pool and returns the String address if the character ABC is already in the constant pool. Otherwise, add a new constant to the pool.) And return the address).
1. The method area is the area shared by each thread, which is used to store the class information that has been loaded by the VIRTUAL machine, final constants, static variables, code compiled by the compiler on the spot, etc. The run-time constant pool is part of the method area used to store literal constants, symbolic references, and translated direct references generated at compile time. 3. In addition to storing compile-time constants, the runtime constant pool can also store constants generated at runtime.Copy the code
  • Direct Memory: For example, if you have 4 gigabytes of memory and the JVM uses 1 gigabyte of memory, the remaining 3 gigabytes are direct memory. The JDK allocates memory based on channels and buffers. Native libraries implemented by C are allocated in direct memory and referenced by DirectByteBuffer stored in the JVM heap. OutOfMemoryError exceptions can also occur because the direct memory is limited by the memory of the local machine.

How Java objects are accessed

Typically, a Java access involves three areas of memory: stack, heap, and method area. In the Java Virtual Machine specification, there is no provision for accessing specific objects by reference type. Currently, there are two main implementation methods:

  • Access via handle (image from network)

As you can see from the diagram, there is a special area in the heap for the handle pool, storing the address of the instance data executed by the relevant handle. This method uses a handle to represent the address, so it is more stable.

  • Access via direct pointer (image from web)

In the direct pointer access mode, reference stores the actual address of the object in the heap, and the object information stored in the heap contains the corresponding type of data in the method area. The biggest advantage of this approach is speed, which is used in the HotSpot VIRTUAL machine.

Java memory allocation mechanism

  • The Java memory allocation and reclamation mechanism is summarized as:Distribution of generational.Generational recycling. Objects will be divided into:Young Generation,The Old Generation,Permanent Generation (also known as method region). As shown below (from the network) :

  • Young Generation: When objects are created, the allocation of memory first occurs in the Young generation (large objects can be created directly in the old generation). Most objects are not used soon after creation, so they quickly become unreachable and are cleaned up by the Young generation’s GC mechanism, known as the Minor OR Young GC. Note that the Minor GC does not mean that the young generation is out of memory, it actually only means that the GC is on the Eden region.

Memory allocation on the young generation looks like this, and the young generation can be divided into three regions: the Eden region (where memory is allocated for the first time) and two Survivor regions (Survivor 0, Survivor 1). As shown below (from the network) :

  • As you can see, the Eden area is a contiguous memory space, so memory allocation is guaranteed to be extremely fast. Most objects are allocated directly to Eden and will die quickly.
  • When Eden’s memory is full, a Minor Gc is performed to clean up the dead objects and copy the remaining objects to Survivor0 (Survivor1 is left blank).
  • The next time Eden is full, perform Minor Gc again to clean up the dead objects, this time copy the surviving objects to Survivor1, and then empty Eden’s pool.
  • Purge dead objects from Survivor0, advance those that can advance to Old, copy surviving objects to Survivor1, and empty Survivor0.
  • After the two live zones have been switched a few times (the HotSpot virtual machine defaults to 15 times, controlled by -xx :MaxTenuringThreshold, beyond this value into the old age, but this is only a maximum, does not necessarily mean this value), the objects that are still alive (actually only a small number of objects, for example, that we define ourselves), Will be copied to the old age.
As you can see from the above procedure, the Eden area is a contiguous space, and one of the Survivor areas is always empty. After one GC and replication, the currently alive objects are saved in Survivor, at which point Eden and another Survivor zone can be emptied directly, and at the next GC the two Survivor zones swap. As a result, this allocation and reclamation approach is extremely efficient, known as the "stop-and-copy" cleanup (copying the Eden region and surviving objects from one Survivor into another Survivor).Copy the code

In the Eden zone, the HotSpot VIRTUAL machine uses two techniques to speed up memory allocation. These are bump-the-pointer and TLAB (Thread-local Allocation Buffers), respectively.

  • Bump-the-pointer: tracks the last object to be created. When an object is created, you only need to check if there is enough memory behind the last object.

  • TLAB (Thread-local Allocation Buffers) : applies to multiple threads. Eden is divided into several segments. Each Thread uses an independent segment to avoid mutual influence. TLAB, in combination with bump-the-Pointer technology, ensures that each thread uses a portion of the Eden block and allocates memory quickly.

  • Old Generation: Objects that survive long enough in the young Generation to not be cleaned up (that is, after a few Minor GC’s) are copied to the Old Generation, which generally has more space, holds more objects, and has fewer GC’s than the young Generation. When the older generation runs out of memory, the Major GC, also known as Full GC, is performed.

If the object is large (such as a long string or a large array) and there is not enough space for the young generation, the large object will be allocated directly to the old generation (large objects may trigger premature GC and should be used sparatively, and short-lived large objects should be avoided). Use - XX: PretenureSizeThreshold to control the size of the object directly to the old s, greater than the value of object allocation on the old s directlyCopy the code

It is possible that the old generation references the young generation, and if MinorGc is executed you may need to query the entire old generation to determine whether it can be cleaned up and collected, which is obviously inefficient. The solution is to maintain a 512byte block called the “card Table” in the tenderly generation, where all the tenderly object references to the younger object are recorded. Query only this part to improve performance.

Java GC mechanism

The basic algorithm of GC mechanism: generation collection.

The young generation

  • In the “stop-copy algorithm” mentioned above, the memory of the new generation is divided into a large Eden region and a small Survivor region (equally divided into two parts). It can be found that the two parts used for replication are not always equal (in the traditional stop-copy algorithm, the two parts of memory are equal, However, the new generation uses 1 large Eden zone and 2 small Survivor zones to avoid this problem). Since most of the young generation objects are short-lived and cannot survive in Survivor zone, the ratio between Eden zone and Survivor zone is large. The default value is 8:1, that is, HotSpot accounts for 80% of the new generation respectively. 10%, 10%. If more than 10% of memory survives Survivor+Eden in a collection, a portion of the objects need to be allocated to the old age.
The -xx :SurvivorRatio parameter is used to configure the capacity ratio of Survivor zones in Eden. The default value is 8, indicating Eden: Survivor0: Survivor1=8:1:1.Copy the code

Old generation

  • The old generation stores many more objects than the young generation, and there are many large objects. It is quite inefficient to use the stop-copy algorithm to clean up the memory of the old generation. In general, the old age substitution algorithm is mark-tidy algorithm.

  • Mark-collation: that is, mark surviving objects (with references) and move all surviving objects towards one end to keep memory contiguous.

When MinorGc occurs, the virtual machine checks whether the size of each promotion to the aged generation is greater than the size of the remaining space of the aged generation. If so, a FullGC is directly triggered. Otherwise, it checks whether -xx :+HandlePromotionFailure is set. If you set -xx :+Handle PromotionFailure, MinorGC will trigger FullGC as well, even if there is a lot of memory left in the older generation. Better not to.

Methods area

  • There are two types of permanent generation recycling: constant in constant pool, useless class information, constant recycling is very simple, no reference can be recycled.
  • To recycle useless classes, you must ensure three things:
  1. All instances of the class have been reclaimed.
  2. The ClassLoder that loaded the class has been reclaimed.
  3. The Class object of the Class object is not referenced (no reflection calls)

– Recycling of the permanent generation is not required. You can set whether to recycle the class using the parameter

Garbage collector

The garbage collector is the specific implementation of GC. The Java Virtual Machine specification does not specify the garbage collector, so different vendors implement different garbage collectors. HotSpot 1.6 version uses the following garbage collector (from the network)

The following is from the Internet

Before introducing the garbage collector, it is important to clarify that in the stop-copy algorithm adopted by the new generation, “stop-the-world” means that the execution of all other threads needs to be suspended while memory is reclaimed. This is inefficient, and newer generations of collectors are increasingly optimizing for this, but still only shortening the stopping time, not eliminating it altogether.

  • Serial [ˈ s ɪ goes ri goes l] collectorA new generation of collectors, using the stop copy algorithm, uses one thread for GC, serial, and other worker threads to suspend.
-xx :+UseSerialGC can run in Serial+Serial Old mode for memory reclamation (this is also the default for VMS running in Client mode)Copy the code
  • ParNew collector: A new generation collector, using the stop copy algorithm, multi-threaded version of Serial collector, GC with multiple threads, parallel, other worker threads paused, focus on reducing garbage collection time.
Use the -xx :+UseParNewGC switch to control the collection of memory using the ParNew+Serial Old collector combination; Use -xx :ParallelGCThreads to set the number of threads that perform memory reclamation.Copy the code

Avenge [ˈ kæ l] Scavenge[ˈ kæ l] a collector that uses the stop copy algorithm and focuses on CPU throughput, the time/total time a user’s code is run, e.g. The JVM runs for 100 minutes, including 99 minutes of user code and 1 minute of garbage collection, with a throughput of 99%. This type of collector is the most efficient use of the CPU and is suitable for running background operations (collectors that focus on reducing garbage collection time, such as CMS, have little waiting time, so it is suitable for user interaction and improves the user experience).

  • Serial Old collector: Old s collector, single thread collector, serial port, use the tag finishing (sorting method is to Sweep (cleaning) and Compact (compression), clean up is eliminated abandoned objects, only the surviving object, compression is moving objects, the space filling to ensure memory is divided into 2 pieces, a piece of all objects, a free) algorithm, using single thread for GC, The Serial Old collector was used with the ParallelScavenge avenge prior to JDK1.5.

  • Parallel Old collector: The older collector, multithreaded, Parallel insane, uses the tag avenge (as opposed to the Serial Old, where the avenge is Summary and Compact, and the avenge is the copying of surviving objects into a pre-prepared area, Rather than Sweep discarded objects like the Sweep algorithm, Parallel Old still needs to suspend other threads while it executes. Parallel Old is useful in multi-core computing. The Parallel Avenge collector is insane and output-first. The Parallel Avenge collector is insane.

  • CMS (Concurrent Mark Sweep) collector: an older collector that aims to obtain the shortest collection pause (i.e., shorten garbage collection time), uses a Mark Sweep algorithm, is multi-threaded, the advantage is Concurrent collection (the user thread can work at the same time as the GC thread), pauses are small.

  • G1 collector