Java memory area and GC mechanism Chapter

Source: Internet
Author: User

Java memory area and GC mechanism
First, Directory
1.Java Garbage Collection Summary
2.Java Memory Area
How 3.Java objects are accessed
4.Java Memory Access mechanism
5.Java GC mechanism
6.Java garbage collector

Ii. Summary of Java garbage collection
1.Java GC Introduction:
A) garbage Collection garbage collection, garbage recovery mechanism;
b) There is no need to write memory recycling and garbage cleanup code in Java, and do not need to consider memory leaks and overflow issues;
c) Because there is an automatic memory management and garbage cleanup mechanism in the Java Virtual machine;
D) This mechanism will flag the memory in the JVM (Java virtual machine) and determine which memory needs to be recycled, automatically reclaim the memory according to the recycling policy, and never cease to guarantee the memory space in the JVM, to prevent memory leaks and overflow problems;
E) Description: About the JVM (Java Virtual machine) refers to the hotspot virtual machine

The 2.Java GC mainly does the following three things:
A) determine which memory needs to be recycled
b) determine when to implement the garbage collection mechanism
c) How to implement the garbage collection mechanism

Third, Java GC mechanism learning
1. The following four learning directions are available:
A) How the memory is allocated;
b) How to ensure that memory is not recycled (that is, which memory needs to be recycled);
c) Under what circumstances the GC is executed and how the GC is executed;
d) How to monitor and optimize the GC mechanism

2.Java Memory Area (Java Runtime memory partitioning)
A) Procedure counter (Program counter Register)
1. The program counter is a small memory area;
2. The byte code used to indicate all execution of the current thread is executed to the first line;
3. Each program counter is used only to record the line number of a thread, so he is "thread-private"
4. Note:
I) If the program executes a Java method, the counter records the executing virtual machine bytecode instruction address;
II) If a local method is being performed, the value of the counter is undefined;
III) Since the program counter only records the current instruction address, there is no memory overflow, so the program counter is the only region in the JVM memory area that does not have a outofmemoryerror defined;

b) Virtual machine stack (JVM stack)
1. Each method of a thread creates a stack frame at the same time it executes;
2. The stack frame is stored in the local variable table, operation station, dynamic link, method export, etc.
3. When the method is called, the stack frame is stacked in the JVM stack, and the stack frame is stacked when the method is executed.
4. Two types of exceptions are defined in the virtual machine stack:
I) Stack overflow: throws a statckoverflowerror if the thread calls a stack depth greater than the maximum allowed depth of the virtual machine;
II) Memory overflow: Because most Java virtual machines can allow the dynamic expansion of the size of the virtual machine stack, the thread can always request the stack until the memory is not enough to throw outofmemoryerror;
5. Each thread corresponds to a virtual machine stack, so the virtual machine stack is also thread-private;

c) Local method stack
1. The local method stack is the same as the "virtual host stack" In terms of "function", "Operation mechanism", "exception type" and so on.
2. The only difference is that the virtual host stack executes the Java method, and the local method stack is used to execute the local (native) method;

d) Heap Area
1. In memory managed by the JVM, the heap area is the largest piece;
2. The heap area is also the main memory area managed by the Java GC mechanism;
3. The heap area is shared by all threads and created when the virtual machine is started;
4. The heap area exists to store "object instances";
5. All objects allocate memory on the heap (there is also memory allocated on "stack")
6. The size of the heap area can be dynamically expanded;
7. Outofmemoryerror:java Heap Sapce exception will be thrown if there is still insufficient memory allocation and cannot be extended after the garbage collection is performed;

E) Method Area
1. In the Java Virtual Machine specification, the "method area" is treated as a logical part, but the "method area" is not "heap";
2. The method area does not need to be continuous in physics, and can choose fixed size or expandable size;
3. You can choose whether to perform garbage collection, but there is very little garbage collection on the method area, which is why the method area is called the "permanent generation";
4. The "" "Garbage Collection" "" in the above method area is mainly for the memory recycle of the solid "" "" "" "and" "" "", and unloading the loaded class;
5. It is difficult to perform garbage collection on the method area, so it is not considered;
6. The Outofmemoryerror:permgen space anomaly is defined on the method area and throws an exception when the memory is low;
7. Run Pool constants:
I) is used to store compile-time constants (literal constants, symbolic references that are generated when compiled)
II) can also store "" "" "" "" "" "" "in order to maintain a constant pool, if the string called" abc "is already in the constant pool, the string address in the pool is returned; otherwise a new constant is added to the constant pool and the address is returned;

f) Direct Memory
1. Direct memory is not a JVM-managed memory, it is a machine memory other than the JVM;
2. For example, the physical memory is 4G,JVM occupies 1G, then the remaining 3G is the direct memory;
3. Because the direct memory is limited by the memory of this machine, there is also the possibility of OutOfMemoryError exception;

Iv. how Java objects are accessed
A reference access to Java involves 3 memory areas: JVM stack, heap, method area
1. Access by handle
2. Access by direct pointer
A) The actual address of the object in the heap is stored in reference, 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.

V. Memory allocation mechanism
1. The memory allocation referred to here refers to the allocation on "" "Heap" ", in general, the memory allocation of objects is carried out on the heap;
The mechanism for 2.Java memory allocation and recycling is, in summary,:
A) allocation of sub-generational
b) Collection of generational
3. The object is divided according to the time of survival:
A) young age
b) Old age
c) Permanent era (i.e., method area)

4. Young age:
A) When an object is created, the allocation of memory occurs first in the young age (big data can be created directly in the old generation);
b) Most of the objects are no longer available after creation and are cleaned up by the young GC mechanism;
c) Memory allocation on younger generations:
1.Eden Memory first allocation area
2.SURVIVOR0/1 Two survival zones
3. Most of the objects just created will be allocated in the Eden area, most of which will soon die out. The Eden Zone is a contiguous memory space, so allocating memory on it is extremely fast;
4. When the Eden area is full, execute the minor GC, clean up the extinct objects, and copy the remaining objects to a surviving area Survivor0 (at this time, Survivor1 is blank, two survivor always have a blank);
5. Thereafter, each time the Eden area is full, the minor GC is executed once, and the remaining objects are added to the Survivor0;
6. When the Survivor0 is full, the objects that are still alive are copied directly to the Survivor1, and after the Eden area executes the minor GC, the remaining objects are added Survivor1 (at this point, the Survivor0 is blank);
7. When two survival zones have been switched several times (the Hotspot virtual machine defaults to 15 times, with-xx:maxtenuringthreshold control, greater than this value entering the old age), the surviving objects (in fact only a small part, for example, our own defined objects) will be copied to the old age.

D) from the above process, it can be seen that the Eden area is a continuous space, and survivor always has a null;
e) After a GC and copy, a survivor holds the currently alive object, while the contents of the Eden and the other survivor areas are no longer needed, and can be emptied directly to the next GC, with the two survivor roles interchanged.
f) As a result, this way of allocating memory and cleaning up memory is extremely efficient, and this garbage collection is the famous "stop-copy (stop-and-copy)" Cleanup method (Copy the Eden area and an object that is still alive in a survivor to another survivor)
g) uses two techniques to speed up memory allocation:
1. The two technologies are bump-the-pointer and Tlab respectively;
2. For Bump-the-pointer:
Since the Eden Zone is contiguous, the core of Bump-the-pointer technology is to track the last object created, and when the object is created, it is necessary to check if there is enough memory behind the last object, which greatly speeds up the memory allocation.

3. For Tlab:
Tlab technology is for multithreading, the Eden area is divided into segments, each thread uses a separate section, to avoid mutual influence. Tlab combined with Bump-the-pointer technology will ensure that each thread uses a segment of the Eden Zone and allocates memory quickly;

4. Older Generations:
A) If the object has survived long enough in the young generation and has not been cleared away, it will be copied to the old age;
b) The old age is characterized by greater space, more objects can be stored, and fewer GC occurrences;
c) When the old generation of memory is low, execute full GC;
d) Dynamic control strategies can be used to dynamically adjust the size of each area in the Java heap and enter the age of older generations;
e) If the object is large, and the young generation of space is not enough, then the large objects will be directly allocated to the old age (but large objects easily trigger GC, should be used sparingly);

Vi. collection methods for each generation (the basic algorithm for GC mechanisms is: generational collection)
1. Young generation
2. The old age
3. Method area (permanent generation)




















Java memory area and GC mechanism Chapter

Related Article

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.