Working principle of Java GC (garbage collection), gc garbage collection

Source: Internet
Author: User

Working principle of Java GC (garbage collection), gc garbage collection

Garbage Collection is short for GC, which indicates Garbage Collection.

Memory Processing is a problem that programmers are prone to. memory recovery that is forgotten or incorrect may cause instability or even crash of programs or systems. The GC function provided by Java can automatically detect whether the object has exceeded the scope, so as to automatically recycle the memory. Java does not provide a display operation to release allocated memory, all the resource recycling work is done by GC, so programmers cannot precisely control the timing of garbage collection.

The following describes the basic working principles of GC in garbage collection.

Java memory management is actually object management, including object allocation and release. For programmers, The new Keyword is used to allocate objects. when an object is released, they only need to assign a null value so that the program cannot access this object. This object is called "inaccessible ". GC will reclaim 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 by Directed Graphs. In this way, it 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, but to ensure that GC can be implemented on different platforms, the Java specification does not strictly regulate many GC behaviors. For example, there are no clear rules on the types of recycling algorithms used and when to recycle them. Therefore, different JVM implementers usually have different implementation algorithms, which also brings a lot of uncertainty to the Development of Java programmers.

According to the working principle of GC, some techniques and methods can be used to make GC running more reasonable and efficient. Here are some suggestions for Java programming.

□Release reference of useless objects as soon as possible. Pay special attention to some complex objects, such as arrays and queues. For such objects, GC is generally less efficient to recycle them. If the program permits it, the unused reference object should be assigned to null as soon as possible to accelerate GC.

□Use the finalize function as little as possible. Finalize is a function provided by Java to programmers to release objects or resources, but it will increase the GC workload, so use finalize function to collect resources as little as possible. When the program has a certain waiting time, the programmer can manually execute System. gc () and run it through GC, but the Java language specification does not guarantee that GC will be executed.


[For more information, see http://blog.csdn.net/mahoking]


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.