Java Vamei Quick Tutorial 22 memory management and garbage collection

Source: Internet
Author: User

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

There has been some knowledge of memory management and garbage collection in the entire tutorial. Here's a little summary.

Java is run in a virtual memory environment of the JVM. Memory is divided into stacks (stack) and heaps (heap). We will examine these two areas separately.

Stack

The basic concept of the stack is referenced in the paper: stack. Many languages use stack data structures to record the order of function calls and related variables (refer to Linux from program to process).

In Java, the stack in the JVM records the method invocation of the thread. Each thread has a stack. In the process of running a thread, if there is a new method call, the stack for that thread will add a storage unit, the frame. In a frame, the parameters, local variables, and return addresses of the method call are saved.

Call stack

Java parameters and local variables can only be basic types of variables (such as int), or object references (reference). Therefore, in the stack, only variables and object references with basic types are saved.

The object that the reference points to is saved in the heap. (The reference may be a null value, that is, it does not point to any object)

References and objects

When the called method finishes running, the corresponding frame of the method is deleted, and the space occupied by the parameter and the local variable is freed. The thread goes back to the original method and continues execution. When all the stacks are emptied, the program also ends with the run.

Heap

As mentioned above, stacks can take care of themselves. But the heap has to be treated with care. A heap is an area of the JVM that can be freely allocated to objects. When we talk about garbage collection (garbage collection), we mainly recycle the heap space.

Common objects in Java exist in the heap. Unlike stacks, the heap's space does not empty with the end of the method call. Therefore, an object created in a method can continue to exist in the heap after the method call ends. One problem with this is that if we constantly create new objects, the memory space will eventually be exhausted.

Garbage collection

Garbage collection (garbage collection, referred to as GC) automatically empties objects that are no longer used in the heap. The garbage collection mechanism, which first appeared in 1959, was used to solve problems in the Lisp language. Garbage collection is a major feature of Java. Not all languages have garbage collection capabilities. For example, there is no mechanism for garbage collection in C + +. The programmer needs to manually free the memory in the heap.

Because there is no need to manually free up memory, programmers can reduce the chance of making mistakes in programming. With garbage collection, programmers can avoid some pointers and memory leaks related bugs (this kind of bug is usually very covert). On the other hand, garbage collection takes more time to compute. Garbage collection is actually transferring the responsibility of the programmer to the computer. Programs that use garbage collection require a longer run time.

In Java, objects are used by reference (the object is like a deadly poison, a reference to a forceps used to extract poison). If there is no longer a reference to the object, then we can no longer invoke or manipulate the object. Such objects will not be reachable (unreachable). Garbage collection is used to free up memory occupied by unreachable objects. This is the basic principle of garbage collection.

(Unreachable objects are dead objects, garbage collected by garbage collection)

Early garbage collection takes the mechanism of reference counting (reference counting). Each object contains a counter. When there is a new reference to the object, the counter is incremented by 1. When the reference is removed, the counter is reduced by 1. When the counter is 0 o'clock, it is considered that the object can be garbage collected.

However, one possible problem is that if there are two objects that are circular references (cyclic reference), such as two objects referencing each other, and there is no other reference (point A or point B) At this time, we actually simply cannot reach both objects by reference.

Therefore, we can find all the reachable objects by using the stack and static data as root (root), starting from the root and following all the references. In other words, a reachable object must be referenced by the root, or by another reachable object.

Orange, reachable, green, unreachable

JVM implementation

Garbage collection by the JVM is a mixture of various mechanisms. Depending on the health of the program, the JVM decides which garbage collection to use.

Let's start by knowing "Mark and sweep". Under this mechanism, each object will have tag information that indicates whether the object is reachable. When garbage collection starts, the Java program pauses to run. The JVM starts from the root, finds all reachable objects, and marks (Mark). The JVM then needs to scan the entire heap, find the remaining objects, and empty the memory that these objects occupy.

The other is "copy and Sweep." Under this mechanism, the heap is divided into two regions. The object always survives in one of two regions. When garbage collection starts, the Java program pauses to run. The JVM starts from the root, finds reachable objects, copies the reachable objects into a blank area, and arranges them tightly, modifying the change in the reference address caused by the movement of the object. Finally, empty the entire area that the object originally survived, making it a new blank area.

As you can see, "Copy and Sweep" requires more complex operations, but also allows objects to be tightly arranged to avoid possible gaps in "mark and sweep". When you create a new object, "Copy and Sweep" provides a large contiguous space. Therefore, if the objects are compared to "longevity", then it applies to "Mark and sweep". If the object's "metabolism" is more active, then it applies to "copy and Sweep."

The above two mechanisms are combined by generational recycling (generational collection). Each object record has its generation (generation) information. The so-called generation refers to the number of garbage collections that the object experiences. The older the object, the longer it takes to survive in memory.

According to a statistical observation of Java programs, the longer the generation of objects, the less likely it is to be garbage collected (the richer the rich, the poorer the poor). Therefore, when we are in the garbage collection, we should pay more attention to those young objects.

Now, specifically, look at the heap in the JVM:

We see that the heap is divided into three generations. The permanent generation (permanent generation) survives in the class object. These objects are not garbage collected. As we have learned in the RTTI, each class object represents a class, contains class-related data and methods, and provides code for the class definition. Each object is created with reference to the corresponding class object. Each object contains a reference to its corresponding class object.

The younger generation (young generation) and the mature generation (tenured generation) need to be garbage collected. The object generation in the younger generation is closer, and the object generation in the mature generation is longer.

Generation

The younger generation is further divided into three districts

Eden (Eden): Newborn objects live in the region. The new object refers to the newly created object from the last GC.

The new object lives in the Garden of Eden

From, to: these two regions are equal in size, equivalent to two regions in copy and sweep.

When a new object cannot be placed in the Eden area, minor collection is started. The JVM uses the policy of copy and sweep to copy the Eden Zone and the reachable objects from the from zone to the to area. After a garbage collection, the Eden Zone and the from zone are emptied, and the to area is tightly stocked with surviving objects. Subsequently, the from zone becomes the new to zone and the to zone becomes the new from zone.

If the minor collection is found to be out of the area, then part of the object is put into the mature generation. On the other hand, even if the to area is not full, the JVM will still move long enough to mature generations.

If the mature generation fills the object and cannot move into the new object, the major collection is triggered. The JVM uses the mark and sweep strategy for garbage collection of mature generations.

Summarize

The above is an overview of JVM memory management. In fact, the JVM has many versions. The GC mechanisms implemented in different versions will have a very small difference. Java, on the other hand, does not provide a way for the JVM to implement GC implementations. GC is still a hotspot in the development of the JVM. We can expect many changes in the future of the JVM's GC mechanism.

Reference links

http://www.ibm.com/developerworks/java/library/j-jtp11253/

Http://www.cs.princeton.edu/picasso/mats/HotspotOverview.pdf

Http://www.cs.umd.edu/class/spring2008/cmsc433/lectures/gc.pdf

Java Vamei Quick Tutorial 22 memory management and garbage collection

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.