GC Garbage Collector Overview

Source: Internet
Author: User

A good Java programmer must understand how GC works, how to optimize GC performance, and how to interact effectively with GC, because some applications have high performance requirements, such as embedded systems and real-time systems. Only by comprehensively improving the memory management efficiency can the performance of the entire application be improved. This article first briefly introduces the working principle of GC, then discusses several key issues of GC in depth, and finally puts forward some java programming suggestions to improve the performance of Java programs from the GC perspective.

Basic Principles of GC
Java memory management is actually the management of objects, including the allocation and release of objects. For programmers, The New Keyword is used to allocate objects. when an object is released, if you assign null values to all references to an object, the program will not be able to access this object any more. We call this object "inaccessible ". GC recycles the memory space of all "inaccessible" objects.
For GC, when a programmer creates an object, GC starts to monitor the address, size, and usage of the object. Generally, GC records and manages all objects in the heap in a directed graph. In this way, GC determines which objects are "reachable" and which objects are "inaccessible ". When GC determines that some objects are "inaccessible", GC has the responsibility to recycle the memory space. However, in order to ensure that GC can differentiate platform implementation, many GC behaviors are not strictly defined by Java standards. For example, there are no clear rules on the types of recycling algorithms used and when to recycle them. Therefore, the implementers of different JVM often have different implementation algorithms. This also brings a lot of uncertainty to the Development of Java programmers. This article studies several issues related to GC and strives to reduce the negative impact of such uncertainty on Java programs.

Incremental GC (incremental GC)
GC is usually implemented by one or more processes in the JVM. It also occupies heap space and CPU usage during running, and when the GC process is running, the application stops running. Therefore, when GC runs for a long time, the user can feel the pause of the Java program. On the other hand, if the GC run time is too short, the object recovery rate may be too low, this means that many objects that should be recycled are not recycled and still occupy a large amount of memory. Therefore, we must weigh the pause time and recovery rate when designing GC.

A good GC implementation allows users to define the settings they need. For example, a device with limited memory is very sensitive to memory usage, and GC is expected to accurately recycle the memory, it does not care about the slow speed of the program. In other real-time online games, the program cannot be interrupted for a long time. Incremental GC divides a long interrupt into many small interruptions through a certain collection algorithm, which reduces the impact of GC on user programs. Although the overall performance of incremental GC may be less efficient than that of normal GC, it can reduce the maximum pause time of the program.
The hotspot JVM provided by Sun JDK supports incremental GC. By default, hotspot JVM does not use incremental GC. To start incremental GC, we must add the-xincgc parameter when running Java programs. Hotspot JVM incremental GC adopts the train GC algorithm. Its basic idea is to group (layer) all objects in the heap according to creation and usage ), put frequently-used and correlated objects in a group. As the program runs, the group is constantly adjusted. When the GC runs, it always recycles the oldest (rarely accessed recently) Objects first. If the whole group is recyclable objects, GC recycles the entire group. In this way, only a certain percentage of inaccessible objects can be recycled during each GC operation, ensure the program runs smoothly.

Finalize () function
Finalize is a method of thinking in the object class. The access modifier of this method is protected. Because all classes are subclasses of objects, it is easy for the user class to access this method. Because the finalize function does not automatically implement chained calls, we must implement them manually. Therefore, the finalize function's last statement is usually super. Finalize (). In this way, we can implement finalize calling from bottom to top, that is, releasing our own resources first and then releasing the parent class resources.
According to Java language standards, JVM ensures that this object is inaccessible before the finalize function is called. However, JVM does not guarantee that this function will be called. In addition, the standard ensures that the finalize function can run at most once.
Many Java beginners will think that this method is similar to the destructor in C ++, and release many objects and resources in this function. In fact, this is not a good method, for the following reasons: first, GC needs to perform a lot of additional work on the objects that overwrite this function to support the finalize function; second, after finalize is run, the object may become reachable. GC checks whether the object is reachable again. Therefore, using finalize will reduce the Running Performance of GC, since the time for GC to call finalize is uncertain, releasing resources in this way is also uncertain.
In general, finalize is used to release very important resources, such as I/O operations and data connections. The release of these resources is critical to the entire application.In this case, programmers should primarily manage (including release) these resources through the program itself, supplemented by the finalize function to release resources, to form a double-insurance management mechanism, instead of relying solely on finalize to release resources.
How the program interacts with GC
(Do not understand ...)
Java2 Enhances memory management and adds a java. Lang. Ref package, which defines three reference classes. These three reference classes are softreference, weakreference, and phantomreference. By using these reference classes, programmers can interact with GC to a certain extent to improve GC efficiency. The reference strength of these reference classes is between reachable objects and inaccessible objects.
Some Java coding suggestions
According to the working principle of GC, we can use some tips and methods to make GC run more efficiently and better meet the requirements of applications. The following are some suggestions for programming:
1. The most basic suggestion is to release reference of useless objects as soon as possible. When using temporary variables, most programmers set reference variables to null automatically after they exit the scope. When using this method, we must pay special attention to some complex object graphs, such as arrays, queues, trees, and graphs. These objects are referenced from each other and their relationships are more complex. For such objects, GC is generally less efficient to recycle them. If the program permits it, assign null to unnecessary reference objects as soon as possible. This will accelerate GC.
2. Use the finalize function as little as possible. The finalize function is a Java function that provides programmers with an opportunity to release objects or resources. However, it will increase the GC workload, so we should use finalize to collect resources as little as possible.
3. Pay attention to the collection data types, including arrays, trees, graphs, and linked lists. The collection of these data structures is more complex for GC. In addition, pay attention to some global variables and static variables. These variables often cause hanging objects (dangling reference), resulting in a waste of memory.
4. When the program has a certain waiting time, the programmer can manually execute the system. GC (), notifies GC to run, but the Java language standard does not guarantee that GC will be executed. In this case, incremental GC can shorten the Java program suspension time.

 

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.