Java Memory recovery mechanism--GC mechanism

Source: Internet
Author: User

First, Java GC concept Description

Java GC (Garbage Collection, garbage collection, garbage collector) mechanism, is one of the main differences between Java and C++/C, as a Java developer, generally do not need to specifically write memory recycling and garbage cleanup code, memory leaks and overflow problems, Nor does it need to be as jittery as C programmers. This is because there is an automatic memory management and garbage cleanup mechanism in the Java Virtual machine. In a nutshell, this mechanism marks the memory in the JVM (Java Virtual machine) and determines which memory needs to be recycled, automatically reclaims memory according to a certain recycling strategy, and never Stops (Nerver Stop) to ensure that the memory space in the JVM Prevent memory leaks and overflow issues.

The Java GC mechanism mainly accomplishes 3 things: Determine what memory needs to be recycled, determine when to perform GC, and how to perform GC. Here we will learn from 4 aspects of the Java GC mechanism, 1, how memory is allocated, 2, how to ensure that the memory is not garbage collection (that is, which memory needs to be recycled), 3, under what circumstances the GC and the way the GC is executed, 4, how to monitor and optimize the GC mechanism.

Second, the Java Memory Area Division

To understand the Java GC mechanism, you must first understand the partitioning of memory areas in the JVM. In the Java Runtime Data area, the memory area managed by the JVM is divided into several modules:

1. Procedure counter (program Counter Register)

The program counter is a small area of memory that indicates the number of bytes executed by the current thread has been executed to the first line, which can be understood as the line numbers indicator of the current thread. When the bytecode interpreter is working, it removes a statement instruction by changing the value of the counter.

Each program counter is used only to record the line number of a thread, so it is thread-private (a thread has a program counter).

If the program executes a Java method, the counter records the virtual machine bytecode instruction address that is executing, and if it is performing a local (native, written by C language) method, the value of the counter is undefined, since the program counter simply records the current instruction address, So there is no memory overflow, so the program counter is the only area in all JVM memory areas where OutOfMemoryError is not defined.

2. Java VM Stack (Java Virtual machine Stacks)

Each method of a thread executes at the same time, will create a stack frame (statck frame), the stack frame stored in a local variable table, operation station, dynamic link, method exit, etc., when the method is called, stack frame in the JVM stack, when the method execution is complete, stack frame out of the stack.

The local variable table stores the relevant local variables of the method, including various basic data types, object references, return addresses, and so on. In a local variable table, only long and double types occupy 2 local variable spaces (slots, for 32-bit machines, one Slot is 32 bits), and the others are 1 slots. It is important to note that the local variable table is determined at compile time, and the space required for the method to run is fully deterministic in the stack frame and will not change during the lifetime of the method.

Two exceptions are defined in the virtual machine stack, which throws a statckoverflowerror (stack overflow) if the thread call has a stack depth greater than the maximum allowable depth for the virtual machine, but most Java virtual machines allow the size of the virtual machine stack to be dynamically extended (with a small number of fixed lengths). So the thread can always request the stack until it is out of memory, and at this point it throws a OutOfMemoryError (memory overflow).

Each thread corresponds to a virtual machine stack, so the virtual machine stack is also thread-private.

3. Local methods Stack (Native method Stacks)

The local method stack is the same as the virtual machine stack in terms of function, operating mechanism, exception type, and the only difference is that the virtual machine stack executes the Java method, and the local method stack is used to execute the native method, in many virtual machines (such as the Sun's JDK default hotspot virtual machine), The local method stack is used together with the virtual machine stack.

The local method stack is also thread-private.

4. Heap Area

The heap area is the most important area to understand the Java GC mechanism, and there is no one. In memory managed by the JVM, the heap area is the largest chunk, and the heap area is the main memory area managed by the Java GC mechanism, which is shared by all threads and created when the virtual machine is started. The heap area exists to store object instances, in principle, all objects are allocated memory on the heap (although in modern technology, it is not so absolute, there are directly allocated on the stack).

In general, according to the Java Virtual Machine specification, heap memory needs to be logically continuous (not physically required), when implemented, can be fixed-size, or extensible, the current mainstream of virtual machines are extensible. If there is still not enough memory allocation and no expansion after the garbage collection is performed, the Outofmemoryerror:java heap space exception will be thrown.

5. Method area

A method area is a zone shared by each thread that stores the class information that has been loaded by the virtual machine (that is, information that needs to be loaded when the class is loaded, including information such as version, field, method, interface, and so on), final constants, static variables, code that the compiler compiles immediately, and so on.

The method area is not physically required to be contiguous, you can choose a fixed size or an extensible size, and the method area has one more limit than the heap: You can choose whether to perform garbage collection. Generally, garbage collection performed on the method area is rare. However, this does not mean that there is no garbage collection on the method area at all, and the garbage collection on it is mainly for the memory reclamation of the constant pool and unloading of the loaded classes.

Garbage collection on the method area, the conditions are harsh and very difficult, the effect is not satisfactory, so generally do not do too much to consider, you can stay for further in-depth study later use.

The Outofmemoryerror:permgen space exception is defined on the method area and is thrown when there is insufficient memory.

Iii. how Java objects are accessed

A reference access to Java involves 3 memory areas: The JVM stack, the heap, the method area.

Refer to the simplest local variable: Object obj = new Object () for example:

    • Object obj represents a local reference, stored in the local variable table of the JVM stack, representing a reference type data;
    • The new object () is stored as instance object data in the heap;
    • The heap also records the address of type information (interface, method, field, object type, and so on) of the object class, and the data executed by these addresses is stored in the method area;

In the Java Virtual Machine specification, there are two main ways to implement a specific object by reference type reference:

1. Access by handle:

In the implementation of access through a handle, the JVM heap will have an area dedicated to the handle pool that stores the instance data addresses (including addresses in the heap and addresses in the method area) that are executed by the related handle. This implementation method is very stable because it represents the address with a handle.

2. Access by direct pointer:

In the way of direct pointer access, reference stores the actual address of the object in the heap, and the object information stored in the heap contains the corresponding type data in the method area. The biggest advantage of this approach is that it is fast and is used in hotspot virtual machines.

The new generation, the old age, the permanent generation: These three types of GC form follow-up summary, is now written here//TODO

There is a need for a closer look: https://www.zhihu.com/question/53613423

Iv. the 5th chapter of Java programming Idea, Java deep Adventures Java garbage Collection mechanism and reference type; deep understanding of Java Virtual machines: JVM advanced effects and best practices, 第2-3; Become a JAVAGC expert part ii-How to monitor Java garbage collection mechanism , http://www.importnew.com/2057.htmlJDK5.0 garbage collection optimization--don ' t pause,http://calvin.iteye.com/blog/91905 "original" Java Memory Area Understanding-a preliminary understanding of the frequent analysis and resolution of HTTP://IAMZHONGYONG.ITEYE.COM/BLOG/1333100 on the application of full GC: http://www.07net01.com/zhishi/383213.html

Java Memory recovery mechanism--GC mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.