And look back on the evil JVM GC

Source: Internet
Author: User
Tags xms

GC (Common understanding): Used to dynamically reclaim memory space occupied by objects without any references in an idle time, in an irregular manner.


1.Distribution of generations in theJava Heap

Young: It is mainly used to store new objects.

Old: Primarily stores memory objects that have a long life cycle in the application.

Permanent: Refers to the memory of the permanent storage area, the main storage class and meta information, class in the load is loaded into the PermGen space area. Unlike the heap area where instance is stored, GC (garbage Collection) does not clean up permgen space during the main program run time, so if your app will load many classes, it will likely appear permgen Space error.


2. What is the GC algorithm used by the JVM?

Collection of generations.

The memory is divided into several regions, and the objects of different life cycles are placed in different regions;

Frequent collection of areas with short life cycles when GC is collected (young area);

Areas with a relatively small collection life cycle (old area);

The base does not collect the permanent zone (Perm area).


3. What is the difference between GC and full GC?

GC (or minor GC): Collects areas with short life cycles.

Full GC (or major GC): Collects areas with a short life cycle (young area) and a long life cycle (old area).

Their collection algorithms are different, so the time used is different. GC efficiency is also high, we want to minimize the number of full GC. When the display calls System.GC (), GC does a full collection (both young generation and tenured generation).


4. When will the garbage collector be created:

The garbage collector is created when the following two assumptions (hypotheses) are established:

1. Most objects will soon become unreachable

2. There are very few references to the new object from the old object (long-time object)




Instead of cleaning up memory directly in the program code, developers are automatically looking for unwanted garbage objects by the garbage collector and clearing them off. In general it is possible to try to set the object to null or call System.GC (), however, setting null is a good habit, however, calling System.GC () is only explained to the JVM, you can come to collect as to whether the GC or not, can say no practical significance, instead, Will call FULLGC, making the GC more inefficient


New Generation (young generation): The vast majority of newly created objects are assigned here, and since most objects become inaccessible soon after they are created, many objects are created in the Cenozoic and then disappear. The process by which objects disappear from this area is what we call "minor GC".


Old generation: objects have not become unreachable and survived from the Cenozoic and are copied here. It occupies more space than the Cenozoic. Because of its relatively large space, the GC that occurs in the old age is much less than that of the Cenozoic. The process of disappearing an object from the old age, which we call "major GC" (or "full GC")


The persistent generation (permanent generation) is also known as the method area. He is used to save class constants as well as string constants, Class,jar packets, and so on. Therefore, this area is not used to permanently store objects that survived from the old age. GC may also occur in this area. and GC events that occur in this area are also counted as major GC.


The composition of the Cenozoic


To better understand the GC, we are now learning the new generation, which is used to preserve the objects that were created for the first time, and he can be divided into three spaces.


1. An Eden Space (Eden)

2. Two survivor Space (Survivor)

There are three spaces, including two survivor space. The order of execution for each space is as follows:


1. Most of the objects that have just been created will be stored in the Eden space.

2. After the first GC was performed in Eden Space, the surviving objects were moved to one of the survivor spaces.

3. Thereafter, after the GC is performed in Eden Space, the surviving objects are stacked in the same survivor space.

When one survivor is saturated with space, the surviving object is moved to another survivor's space. This will then empty the survivor space that is already saturated.

In the above steps, repeated several times the surviving objects will be moved to the old age.

If you look closely at these steps, you will find that one survivor space must remain empty. If two survivor spaces have data, or two spaces are empty, that must indicate some sort of error in your system.


JAVA2 enhances the memory management feature by adding a JAVA.LANG.REF package that defines three reference classes. These three reference classes are SoftReference, WeakReference, and Phantomreference, respectively. By using these reference classes, programmers can interact with the GC to some extent to improve GC productivity. These reference classes have a reference strength between the unreachable object and the unreachable object.



5. Heap Settings

-XMS: initial Heap Size

-XMX: Maximum Heap Size

-xx:newsize=n: Setting the young generation size

-xx:newratio=n: Sets the ratio of the younger generation to the older generation. such as: 3, the ratio of the young generation and the old generation is 1:3, the young generation of the entire young generation of old generation and 1/4

-xx:survivorratio=n: The ratio of Eden in the young generation to the two survivor districts. Note that there are two survivor districts. such as: 3, indicating Eden:survivor=3:2, a Survivor area accounted for the entire young generation of 1/5

-xx:maxpermsize=n: Setting the persistent generation size

Collector Settings

-XX:+USESERIALGC: Setting up the serial collector

-XX:+USEPARALLELGC: Setting up a parallel collector

-XX:+USEPARALLEDLOLDGC: Setting up a parallel old generation collector

-XX:+USECONCMARKSWEEPGC: Setting the concurrency Collector

Garbage collection Statistics

-xx:+printgc

-xx:+printgcdetails

-xx:+printgctimestamps

-xloggc:filename

Parallel collector settings

-xx:parallelgcthreads=n: Sets the number of CPUs to use when the parallel collector is collected. The number of parallel collection threads.

-xx:maxgcpausemillis=n: Set maximum pause time for parallel collection

-xx:gctimeratio=n: Sets the percentage of time that garbage collection takes to run the program. The formula is 1/(1+n)

Concurrent collector Settings

-xx:+cmsincrementalmode: Set to incremental mode. Applies to single CPU conditions.

-xx:parallelgcthreads=n: Set the concurrency collector the number of CPUs used by the young generation collection method for parallel collection. The number of parallel collection threads.


6. Example: Heap size setting

Scenario: Under Java_home, execute the following command under demo/jfc/swingset2/directory:

java-jar-xmn4m-xms16m-xmx16m Swingset2.jar


7. Common Memory leak errors

Many developers have encountered Java.lang.OutOfMemoryError errors. This error is divided into two types: Java.lang.OutOfMemoryError:Java heap space and java.lang.OutOfMemoryError:PermGen space. The cause of this error can be a program problem or a JVM parameter configuration problem. For parameter problems, the former can be configured with the-XMS and-XMX parameters, which can be set by configuring-xx:permsize and-xx:maxpermsize.


This article is mainly to collect and organize:

Http://www.importnew.com/1993.html

http://blog.csdn.net/winniepu/article/details/4829087

And look back on the evil JVM GC

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.