Java6_garbage collection (garbage collection)

Source: Internet
Author: User
ArticleDirectory
    • Differences between parallelism and concurrency
Now I think that you should review or learn the knowledge from the global perspective, and then sort out the outline, so that you can have a comprehensive and consistent understanding of this knowledge, it is also conducive to future review, which of the following statements are not clear and targeted?

Speaking of garbage collection, before garbage collection, it is necessary to produce valuable things: Java memory application and allocation

I bought something. Of course, I must have a place: Data zone of Java runtime.

Now there is a place to put things, things have been put too long, it will certainly produce garbage, someone needs to recycle: the type of the Garbage Collector

Is it just a bit of water or a cleanup: The garbage collection method

It's hard to figure out that the location is too big: heap generation

Some terrain is complex and a little terrain is simple. Different cleaning methods are used based on different terrain: Garbage CollectionAlgorithm

when to perform health check: When to trigger GC

Where to dispatch persons: select different garbage collectors based on different generations

Why is there a relatively long and frequent time for sanitation in some places: the parameter settings of the young generation, the old generation, and the long-lasting generation often produce full GC?

Health Task failed: OOM (outofmemoryerror) and Java Memory leakage monitoring (find out the cause of Memory leakage)

sometimes you need to check the health records: print GC logs and gcviewer-1.32 of log analysis tools

CLEANING on site: jvisualvm and jprofiler provided by the graphical tool Java, associate hprof files

How to quickly complete the task: Sun JDK 16 GC (Garbage Collector) Bi Xuan

Http://www.docin.com/p-417999249.html)

 

 

Basic collection Algorithm

Copy): Divide the heap into two identical spaces, access each associated active object from the root (threadlocal object, static object), and copy all the active objects of space a to Space B, then, the whole space a is reclaimed at one time.
Because only active objects are accessed, the whole space is cleared after all active objects are copied, and no dead objects are accessed. Therefore, the cost of traversing the space is small, however, it requires a huge copy cost and a large amount of memory.

Mark-sweep ):The collector first accesses all active objects from the root and marks them as active objects. Then, traverse the entire memory area and recycle all unmarked objects. The cost of traversing the entire space by this algorithm is large. The pause time increases linearly with the size of the space, and there are many fragments in the heap after sorting.

Mark-sweep-compact ):Based on the practices and advantages of the two, the system first marks the active object and combines it into a large memory block.

GC collector type

Serial collector(Serial collector):-XX: + useserialgc: Serial replication policy for the young generation, serial marking for the old generation

Parallel collector(Throughput collector):-XX: + useparallelgc: This is the default value of JDK 5-server.

Policy:
Young Generation: suspend applicationProgramMultiple garbage collection threads are concurrently copied and collected. The default number of threads is the number of cpus. When there are many CPUs,-XX: parallelgcthreads = specifies the number of threads.
Old Generation: Pause the application. Like the serial collector, the individual garbage collection thread marks the sorting.
As shown above, the collector is superior to the serial collector when it requires 2 + CPUs. It is suitable for background processing and scientific computing.

Concurrent collector(Concurrent low pause collector-Cms):-XX: + useconcmarksweepgc:

These are upgraded versions of the above two policies.

Policy:
Young Generation: The application is also suspended, and multiple garbage collection threads are concurrently copying and collecting.
Old Generation: only two short pauses are performed, and applications and collection threads are concurrently cleared at other times.

Incremental concurrency collector(Incremental Concurrent-mark-sweep/I-Cms ):

Although the CMS collection algorithm adopts multi-thread concurrent operations in the most time-consuming memory area, the performance is not improved when the server CPU resources are insufficient, instead, the system throughput will decrease,

To avoid this situation as much as possible, the incremental CMS collection algorithm is available, that is, to allow the GC thread and user thread to run in parallel During Concurrent marking and cleaning, minimize the full and exclusive execution of GC threads;

Differences between parallelism and concurrency

Parallel: Multiple garbage collection threads run concurrently. At this time, the user thread does not run;

Concurrency: the user thread and the garbage collection thread run concurrently. The program continues to run, while the garbage collection program runs on another CPU.

At the beginning of concurrent collection, all threads will be stopped for a short time to start marking the root object, then marking the concurrent running of the thread and the application thread, and finally suspending it for a short time, multiple threads run concurrently again,

In order to remark the objects that may be missed due to concurrency, the process of clearing concurrency with the application starts. It can be seen that the longest two traversal processes are executed concurrently with the application,

It is much improved than the previous Serial Algorithm !!!
Serial mark clearing starts collection when the old generation is full. Concurrent collection is performed because it is run with the application. If the collection is full, the application has no memory available, therefore, the system defaults
When 68% is full, data is collected.

The memory has been set to a large size. When the memory consumption is not so fast, you can use-XX: cmsinitiatingoccupancyfraction =
Increase the ratio appropriately.

 

 

GC method

When each generation is full, the collection will be automatically promoted.The conditions triggered by each collector are different. Of course, you can use some parameters to set them forcibly. There are two types:

Minor collection: GC scans and recycles young at a high frequency and adopts a replication algorithm.

Major collection: memory collection for both young and old, also called full GC; because of the cost relationship, the check for old is much less frequently than young, and the mark clearing/marking algorithm is used.

You can callCodeSystem. GC () triggers the major collection and uses-XX: + disableexplicitgc to disable it, or set it to CMS concurrency-XX: + explicitgcinvokesconcurrent.

Note: the recovery of the new generation is usually called minor GC; the recovery of the old generation is called Major GC. However, in addition to the concurrent GC, the major GC must scan and recycle the entire heap, therefore, it is also called full GC.

 

Memory Allocation:

The newly generated object completes memory allocation in the Eden area.

Young Generation GC trigger conditions:

Trigger minor collection when the Eden zone is full

Young Generation memory recycling:The objects that survive in Eden and a certain vertex or space (from space) are moved to another empty vertex or space (to space,

(Of course, the first time is that only the objects living in the Eden area are moved, and you can select a space.) then, the roles from space and to space are reversed.

Then clear the Eden and space (from space) (of course, only Eden is cleared for the first time, because there are no objects in the from space), and always ensure that an empty VOR is empty.

Conditions for the young generation to be promoted to the old age:

When an object is moved between two consumer vor spaces for a certain number of times (set by the XX: maxtenuringthreshold parameter), it is old enough to stay in the old generation.

Of course, if the routing vor space is small enough to accommodate all live objects, some live objects will be directly promoted to the old generation.

Either or
Spaces can be seen as a buffer between Eden and the old generation. Through this buffer, it can be used to check whether the lifecycle of an object is long enough, because some objects have escaped a minor
Collection does not indicate that its lifecycle is long enough. Maybe the next minor
The collection is suspended. To a certain extent, this ensures that the objects entering the old generation are genuine, reduces the growth rate of space usage in the old generation, and reduces the GC frequency in the old generation.

Condition for triggering GC in the old generation or permanent generation:

When the memory usage of the old or permanent generation reaches a threshold, system. GC () is displayed as a call, and a GC based on all generations is triggered.,

It must be in a wide range (large) and time consuming (relatively slow), but the frequency is relatively low.
(A small number of times), called major collection (full
Collection ).

Old Generation or permanent generationMemory recycling:

Generally, GC for the young generation is used first, and then GC for the old and permanent generations is used.

 

 

Generation Division

Generational collection is a highlight of Java garbage collection. Based on the length of the object's lifecycle, the heap is divided into three generations: young, old, and permanent, different collection algorithms are used based on the characteristics of different generations to foster strengths and circumvent weaknesses

Young (Young Generation) replication algorithm

Young is divided into three areas, One Eden, where all new objects exist and two vor regions are used to implement the replication algorithm.Each copy is to copy the live objects of Eden and the first primary or to 2nd, and then clear the Eden and the first primary or. The ratio of Eden to primary VOR is set from-XX: Primary vorratio =. The default value is 32. When vio is too large, it will be a waste. If it is too small, it will cause some young objects to flee to the elderly area, causing anxiety in the elderly area. However, this parameter is not important to performance.

Tenured (aging) marking algorithm

If the objects of the young generation can be collected several times, they will enter the elderly area. The elderly area uses the tag sorting algorithm. Because the objects in the elderly area are not so easy to die, it is necessary to repeatedly copy objects by using the replication algorithm, which is not cost-effective. We have to use the mark clearing algorithm, but the mark clearing algorithm is not easy, every time you traverse all objects in the area, there is still no free lunch.
-XX: maxtenuringthreshold = sets the number of times the young generation has been collected and moved into the old area. The default value of CMS is 0. After the first GC, it is transferred. You can use-XX: + printtenuringdistribution to view it.

Perm (permanent) Permanent generation

The default value for loading basic data such as class information is 64 mb. If it is a service program with many classes, you need to increase the setting-XX: maxpermsize =; otherwise, fullgc () will occur when it is full () or out of memory. Note that frameworks like spring and hibernate that like the dynamic generation of AOP require more persistent generation memory. Generally, the persistent generation does not perform GC unless it is forcibly set through-XX: + cmsclassunloadingenabled-XX: + cmspermgensweepingenabled.

 

Java SE 6 hotspot [Tm] Virtual Machine garbage collection tuning (original ):

Http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html

 

Java SE 6 hotspot [Tm] Virtual Machine garbage collection tuning ):

Http://translate.google.com/translate? Bytes

 

Http://blog.csdn.net/sfdev/article/details/4483442

http://blog.csdn.net/calvinxiu/article/details/1614473

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.