Java garbage Collection

Source: Internet
Author: User

The Java language does not require programmers to directly control memory recycling, and the Java program's memory allocation and recycling is performed automatically by the JRE in the background, and the JRE is responsible for reclaiming memory that is no longer used, a mechanism known as the garbage collection mechanism (garbage COLLECTION,GC ):

One, mainly responsible for two things:

1. Discovery of useless objects;

2. Reclaim the memory space occupied by the useless objects and make it available to the program again ( typically when the CPU is idle or the memory is low ).

Note: In fact, in addition to freeing up memory space used by unused objects, garbage collection can also clear memory-log fragmentation (due to the amount of memory space that the object created and the garbage collector freed up discarded objects)

Second, the characteristics

1. The purpose of the garbage collection mechanism is to reclaim the memory space of the useless objects, which are the memory space in the JVM heap memory (the runtime data area, which holds the instance of the class, that is, the object) , and does not contain other material resources such as database connection, Disk I/O, etc.;

The 2.Java language does not explicitly provide methods for allocating memory and deleting memory, and some developers set the reference object to null or call System.GC () or RUNTIME.GETRUNTIME.GC () to free memory (the latter two methods are only recommendations, Use carefully );

3. Garbage collection is unpredictable, different JVMs use different garbage collection mechanisms and algorithms, it is possible to happen periodically, it may occur when the CPU is idle, or it may occur when memory is exhausted ( The most well-known generational garbage collection below );

Third, the generation of garbage collection (information from http://www.cnblogs.com/yaoyuan23/p/5587548.html)

1. Younger generation (young Generation):

All newly generated objects are first placed in the younger generation. The goal of the young generation is to collect as quickly as possible those objects with short life cycles. The young generation is divided into three districts. One Eden area, two survivor districts (in general). Most objects are generated in the Eden area. When the Eden Zone is full, the surviving objects will be copied to the Survivor area (one of two)(YGC), and when the survivor area is full, the surviving objects of this area will be copied to another survivor area, When the survivor is full, the objects copied from the first survivor area and still alive will be copied "old age (tenured)". It should be noted that the two areas of the survivor are symmetrical and have no relationship, so the same area may exist at the same time from Eden copied objects, and from the previous survivor copied objects, and copied to the old quarter only from the first survivor to come over the object. Moreover, there is always an empty survivor area. At the same time, according to the program needs, the survivor area can be configured as multiple (more than two), which can increase the time of the object in the younger generation, reduce the possibility of being put into the old generation.

2. Old Generation:

Objects that survived after n garbage collection in the younger generation will be placed in the old age. Therefore, it can be considered that older generations are storing objects with longer life cycles.

3. Persistent generation (Permanent Generation):

Used to store static files, now Java classes, methods, and so on. The persistence generation has no significant impact on garbage collection, but some applications may dynamically generate or invoke some classes, such as Hibernate, at which point a large, persistent generation space is required to store the new class in these runs. The persistent generation size is set by-xx:maxpermsize=<n>.

4. What happens when a garbage collection is triggered

Because objects are processed in a generational way, garbage collection areas and times are different. There are two types of GC: Scavenge GC and full GC.

Scavenge GC

In general, when a new object is generated and the Eden application space fails, the scavenge GC is triggered, GC is performed on the Eden Zone, the non-surviving objects are cleared, and the surviving objects are moved to the survivor area. Then tidy up the two districts of survivor. This method of GC is carried out on the young generation of the Eden area and does not affect the old generation. Because most objects start in the Eden area, and the Eden area is not very large, GC in the Eden area is frequent. Thus, it is generally necessary to use fast and efficient algorithms, so that Eden can be free as soon as possible.

Full GC

Organize the entire heap, including young, tenured and perm. The full GC is slower than the scavenge GC because it needs to be recycled for the entire pair, so it should be as low as possible. In the process of tuning the JVM, a large part of the work is to adjust the FULLGC. The full GC may be caused by the following reasons:

The old generation (tenured) was written full

Persistent generation (Perm) is full

System.GC () is displayed call

Dynamic changes in the domain allocation policy of the heap after the last GC

Note: For more information on JVM parameters, refer to: http://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html

Java 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.