GC principles that must be understood by excellent Java programmers

Source: Internet
Author: User

A good Java programmer must understand GC (garbage collection)
Garbage Collection) working principle, how to optimize GC performance, and how to perform limited interaction with GC, because some applications have high performance requirements, such as embedded systems and real-time systems, only within comprehensive improvement
Storage Management Efficiency
To improve the performance of the entire application. 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,
Improve the Performance of Java programs from the GC perspective.

Basic Principles of GC

Java memory management is actually object management, including object allocation and release.

For programmers, The New Keyword is used to allocate objects. When releasing an object, they only need to assign null values to all references of the object so that the program cannot access this object, 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 the locations in the heap by Directed Graphs.
Object (For details, refer to reference 1.
). This method is used to determine 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, to ensure
GC can be implemented on different platforms. Many GC behaviors are not strictly defined in Java specifications. For example, there are no important questions about the types of recycling algorithms used and when to recycle them.
There are clear provisions. Therefore, the implementers of different JVM often have different implementation algorithms. This also brings many uncertainties to the Development of Java programmers. This article studies several issues related to GC
Reduces the negative impact of such uncertainty on Java programs.

Incremental GC (incremental GC)

GC is usually implemented by one or a group of processes in JVM. It also occupies heap space like the user program, and CPU usage during runtime. the application stops running when the GC process is running.
Line. Therefore, when GC runs for a long time, you can feel
Java program pauses. On the other hand, if the GC running time is too short, the object recovery rate may be too low, which means that many objects that should be recycled are not recycled and still occupy a large amount of memory. Therefore
During GC design, the pause time and recovery rate must be weighed. A good GC implementation allows users to define their desired settings. For example, some devices with limited memory are very sensitive to memory usage.
Hope that GC can accurately recycle the memory, it does not care about the slow speed of the program. In addition, some real-time online games cannot allow long periods of program interruptions. Incremental GC is calculated based on a certain amount of recovery.
To reduce 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 a program.

The hotspot JVM provided by Sun JDK supports incremental GC. hotspot
By default, the jvm gc method does not use incremental GC. To start incremental GC, we must add the-xincgc parameter when running the Java program. Hotspot
The implementation of JVM incremental GC adopts train
GC algorithm. The basic idea is to group (stratified) all objects in the heap by creation and usage, and put frequently used and correlated objects in a queue. As the program runs, constantly
Group. When GC is run, it always recycles the oldest (rarely accessed recently) Objects first. If the entire group is recyclable, GC recycles the entire group. In this way, only a certain percentage
Inaccessible objects to ensure smooth running of programs.

Detailed description of finalize Functions

Finalize
Is a method located 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. By
In, the finalize function does not automatically implement chained calls, so 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 the Java language specification, the JVM ensures that the object is reachable before the finalize function is called, but the JVM does not guarantee that the function will be called. In addition, the finalize function can run at most once.

Many Java beginners will think that this method is similar to the destructor in C ++ and put the release of many objects and resources in this function. In fact, this is not a good method. There are three reasons,
First, in order to support the finalize function, GC requires a lot of additional work on the objects that overwrite the function. Second, after the finalize operation is complete, the object may become reachable.
, GC also checks whether the object is reachable. Therefore
Finalize reduces the Running Performance of GC. Third, the time for GC to call finalize is uncertain, so releasing resources in this way is also uncertain.

In general, finalize is used to release very important resources that are not easily controlled, 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
You can only use finalize to release resources.
The following example shows that after the finalize function is called, it may still be reachable. It can also be said that the Finalize of an object can only run once.

  1. Class myobject {
  2. Test main; // record the test object, which is used to restore accessibility in finalize
  3. Public myobject (test T)
  4. {
  5. Main = T; // Save the test object
  6. }
  7. Protected void finalize ()
  8. {
  9. Main. ref = This; // restore the object so that the object can be reached
  10. System. Out. println ("this is finalize"); // used to test finalize only once
  11. }
  12. }
  13. Class test {
  14. Myobject ref;
  15. Public static void main (string [] ARGs ){
  16. Test test = new test ();
  17. Test. ref = new myobject (test );
  18. Test. ref = NULL; // If the myobject object is an inaccessible object, finalize will be called.
  19. System. GC ();
  20. If (test. Ref! = NULL) system. Out. println ("My object is still alive ");
  21. }
  22. }

Running result:

This is finalize

Myobject is still alive

In this example, it is worth noting that, although the myobject object becomes an reachable object in finalize, finalize will not be called the next time it is recycled, because the finalize function can only be called once at most.

How the program interacts with GC

Java2
Enhanced the memory management function and added a java. Lang. Ref package, which defines three reference classes. The three reference classes are softreference and,
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 the image and the inaccessible object.

It is also very easy to create a reference object. For example, if you need to create a soft
Create an object and use the normal reference method (reachable object). Then create a softreference to reference the object.
Set to null. In this way, this object has only one soft reference. At the same time, we call this object a soft reference object.

Soft
The main feature of reference is that it has a strong reference function. This type of memory is recycled only when the memory is insufficient. Therefore, when the memory is sufficient, it is usually not recycled. In addition
Objects can also ensure that outofmemory is thrown in Java.
Before an exception occurs, it is set to null. It can be used to cache some common images and implement the cache function to ensure maximum memory usage without causing outofmemory.
Use pseudo code for this reference type;

  1. // Apply for an image object
  2. Image image = new image (); // create an image object
  3. ...
  4. // Use Image
  5. ...
  6. // After image is used up, set it to soft reference type and release strong reference;
  7. Softreference sr = new softreference (image );
  8. Image = NULL;
  9. ...
  10. // Next time
  11. If (SR! = NULL) image = Sr. Get ();
  12. Else {
  13. // Due to the low memory usage of GC, the image has been released and therefore needs to be reloaded;
  14. Image = new image ();
  15. Sr = new softreference (image );
  16. }

Weak
The biggest difference between the referenced object and the soft referenced object is that GC needs to check whether to recycle the soft referenced object during collection. For the weak referenced object, GC always returns
. Weak reference objects are easier and faster to be recycled by GC. Although the weak object must be recycled during GC running, the weak object group with complex relationships often needs several GC operations to complete.
. Weak reference objects are often used in the map structure to reference objects with a large amount of data. Once the strong reference of this object is null, GC can quickly recycle the object space.

Phantom
References are rarely used to assist in the use of finalize functions. Phantom objects refer to some objects. They run the finalize function and are non-reachable objects, but they
It has not been recycled by GC. This type of object can assist finalize in some subsequent recycling work. We will enhance the resource recycling mechanism by overwriting the clear () method of reference.
Flexibility.

Some Java coding suggestions

Based on the working principle of GC, we can use some techniques and methods to make GC run more efficiently and better meet the requirements of applications. The following are some suggestions for program design.

1.
The most basic suggestion is to release reference of useless objects as soon as possible. When using temporary variables, most programmers automatically set the reference variables to null after they exit the scope.
When using this method, you must pay special attention to some complex object graphs, such as arrays, queues, trees, and graphs. These objects have complex reference relationships. For such objects, GC usually recycles them.
Low. If the program permits, the unused reference object is assigned null as early as possible, which can accelerate GC.

2. Use the finalize function as little as possible. The finalize function is an opportunity that Java provides to programmers to release objects or resources. However, it will increase the GC workload, so use finalize as little as possible to recycle resources.

3. If you need to use frequently used images, you can use the soft application type. It can store images in the memory as much as possible for the program to call without causing outofmemory.

4. 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 some static variables. These variables are often prone to dangling reference, resulting in a waste of memory.

5. When the program has a certain waiting time, the programmer can manually execute system. GC () to notify the GC to run, but the Java language specification does not guarantee that GC will be executed. Incremental GC can shorten the pause time of Java programs.

Link: http://developer.51cto.com/art/200808/85099_1.htm

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.