Let's also talk about Java's garbage collection (garbage collection)

Source: Internet
Author: User

Garbage collection is a notable feature of the Java language. Unlike the C language, it is always necessary to consider what numbers cross-border or something. What is garbage? "An object is considered garbage when it can no longer be reached from any pointer in the running program." First, you need to understand the memory allocation:

  • Static allocation: the distribution of static variables and global variables.
  • Automatic Allocation: stores basic data and object references, but the object itself is not stored in the stack, but in the heap. Allocate memory for local variables in the stack. The memory in the stack can be automatically released when the code block exits.
  • Dynamic Allocation: dynamically allocates memory space in the heap to store new data.

Therefore, garbage collection targets objects in the heap. The simplest algorithm that comes to mind isReference count(Reference counting), saves a reference count for each object. If the count is zero, it can be deleted. However, the disadvantage is that when a new object is generated, the related count needs to be updated. More importantly, the circular reference cannot be deleted (for example, two objects are referenced to each other ).


Other methods involve the concept of root set. In short, it refers to the object reference in the stack and the set of objects in the heap. From these objects, we can trace all live objects at the first level. "Any object referred to by a root is reachable and is therefore a live object. additionally, any objects referred to by a live object are also reachable. "[3] The following algorithms are used:

  • Mark-Sweep Algorithm: First mark the live objects, which can be collected without being marked.
  • Copy algorithm: divides the memory into two parts. During the collection, the live object is moved from one part to the other, so that the interaction is repeated.
  • Generational collecting algorithm: this is also an algorithm used by Java. Based on the statistical results, it integrates the advantages of the preceding algorithm.

Java garbage collection

First, an observation:Infant mortality. The principle is that many objects have a very short survival time, and few objects will survive for a long time. Therefore, the heap is divided into several parts to store objects of different lifetime.

 

Note: Reference [4]


New objects are first placed in young, where objects are often collected. if they are still alive, they are placed in mongovor. If they are alive, they are placed in tenured. When the space of the young object is getting fast, start minor collection. When tenured generation needs to be collected, it is called major collection. This frequency is lower and slower.

 

Refer:

[1] http://developer.51cto.com/art/201009/225071.htm

[2] http://blog.csdn.net/javafuns/archive/2007/09/04/1771535.aspx

[3] http://www.artima.com/insidejvm/ed2/gcP.html

[4] http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html

Related Article

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.