< eating dumplings;: Summary of simple learning about garbage collection technology in Java

Source: Internet
Author: User

Knowledge introduction from the network, the following will be based on continuing learning to supplement and appropriate changes, thank you! Original address: http://www.importnew.com/26821.html#comment-578355

The garbage collection mechanism in Java frees developers from the need to focus on the creation and release of space, which automatically reclaims garbage in the background as a daemon. This not only improves the efficiency of development, but also improves the use of memory.

First, the main issues involved:

1) What is heap memory?

2) What is garbage?

3) What are the ways to recycle rubbish?

4) What is the generational recycling mechanism?

Ii. Brief answers to questions

1. What is heap memory?

It is created at the start of the JVM and is primarily used to maintain runtime data (Dynamic Data), such as those created during the run (new) objects and arrays that are present in this area.

The heap in Java is very important, if we dynamically create objects that are not collected in a timely manner, continue to accumulate, it will cause the heap space is full, memory overflow. As a result, Java provides a garbage collection mechanism that creates a daemon in the background, automatically jumping out of space when the memory is tight, and reclaiming all the garbage from the heap.

To ensure the normal operation of the program.

  Add : http://www.cnblogs.com/zhengzeze/p/6519760.html (< eat dumplings;: Simple summary of memory parts in Java)

2. What is garbage?

It refers to an object that is no longer alive.

Common methods of judging are: reference counting method and accessibility analysis method.

  Reference Counting : Assigns a reference counter to each created object to store the number of times the object is referenced. When the number is zero, it means that no one can use the object again, and that the object is "dead". However, there is a serious problem with this scenario, which is that the "circular reference" cannot be detected: When two objects are referenced to each other, neither of them is referenced by anything in the world, and neither of them is counted as zero, so they are never recycled. In fact, for developers, these two objects are completely useless.

       therefore , this scenario is not used in Java to detect the survivability of objects.

 Accessibility Analysis Method : This scheme is the current way to determine the survival of the mainstream language Judgment object. The basic idea is to think of all the referenced objects as a tree, starting from the root node GC Roots of the tree, and continuously traversing to find all the connection objects, which are called "reach" or "live" objects. The remaining objects are considered "unreachable" objects of "death", or "garbage".

        Supplement :What is GC roots? ( reference: http://www.importnew.com/26821.html#comment-578355)

We can guess that GC Roots itself must be accessible, so that the objects traversed from them are guaranteed to be certain. So what are some of the objects in Java that can be reached? There are mainly the following four kinds:

The object referenced in the virtual machine stack (the local variable table in the frame stack).
The object referenced by the static property in the method area.
The object referenced by the constant in the method area.
The object that is referenced by JNI in the local method stack.

3. What are the ways to recycle rubbish?

 

As already known, all GC Roots unreachable objects are called garbage, reference, black for garbage, gray for living objects, green for white space.

So, how do we recycle the rubbish?

Mark-Clean Up

The first step, the so-called "tag" is the use of accessibility to traverse the heap memory, "survival" Objects and "garbage" objects tagged, the results are as follows;
The second step, since the "garbage" has been marked, then we go through again, all the "garbage" object occupied by the space directly emptied.

The results are as follows:

This is the tag-clean scheme, which is simple and convenient, but it is prone to memory fragmentation.

Mark-Organize

Since the above method will produce memory fragmentation, well, when I clean up, I get all the surviving objects in the same place and keep them together so that there is no memory fragmentation.

The results are as follows:

These two schemes are suitable for the survival of many objects, less garbage, it only need to clean up a small amount of garbage, and then move the surviving objects can be.

Copy

This method is rough, directly divides the heap memory into two parts, for a period of time only allow allocation in one piece of memory, when the memory is allocated, then garbage collection, all the surviving objects are copied to another piece of memory, the current memory is all directly emptied.

Reference:

Initially, only the upper part of the memory is used until the memory is used, garbage collection, all the surviving objects to the lower part, and the upper part of the empty.

It's not easy to fragment, it's simply rude, but it means that you can only use a portion of your memory for a period of time, more than that memory, which means the frequent duplication of memory in the heap is emptied.

This scheme is suitable for less surviving objects and more garbage, so that there is no need to replicate many objects in the past and most of the rubbish is emptied directly.

  

4, what is the generational recovery mechanism?

(PostScript, first learn the next ...) )

   

  

< eating dumplings;: Summary of simple learning about garbage collection technology in Java

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.