How Java GC (garbage collection) works

Source: Internet
Author: User

Garbage collection is referred to as GC, which means garbage collection.

Memory processing is where programmers are prone to problems, and forgetting or wrong memory recycling can cause programs or systems to become unstable or even crash. The GC functionality provided by the Java language can automatically detect whether an object exceeds the scope to achieve the purpose of auto-reclaiming memory, the Java language does not provide a display method to release allocated memory, the resource recycling is all done by GC, the programmer cannot precisely control the timing of garbage collection.

Here's a brief introduction to how the GC works when garbage collection is implemented.

Java's memory management is actually the management of objects, including the allocation and release of objects. For programmers, the allocation object uses the new keyword, which is called "unreachable" as long as the object is assigned null when the object is freed and the program is not able to access the object again. The GC will be responsible for reclaiming the memory space of all "unreachable" objects.

For GC, when a programmer creates an object, the GC starts to monitor the object's address, size, and usage. Typically, the GC records and manages all objects in the heap in a graph-like manner, in such a way as to determine which objects are "up to" and which are "unreachable". When the GC determines that some objects are unreachable, the GC has a responsibility to reclaim these memory spaces, but in order to ensure that the GC can be implemented on different platforms, the Java specification has no strict rules for many of the GC's behavior. For example, there are no clear rules on what type of recovery algorithm to use and when to recycle. Therefore, different JVM implementations often have different implementation algorithms, which brings a lot of uncertainty to the development of Java programmers.

Depending on how the GC works, there are some tricks and ways to make the GC run more reasonable and efficient, and some Java programming recommendations.

-Release references to useless objects as early as possible, paying particular attention to complex objects such as arrays, queues, and so on. For such objects, GC recycling is generally less efficient. If the program allows, you should assign NULL to unused reference objects as early as possible, which speeds up the work of the GC.

-Use the Finalize function sparingly. Finalize is a function that Java provides to programmers to dispose of objects or resources, but it increases the amount of GC work, and therefore minimizes the use of finalize functions to reclaim resources. When the program has a certain wait time, the programmer can manually execute System.GC (), run through the GC, but the Java language specification does not guarantee that the GC will be executed.


"Reproduced use, please specify the source:http://blog.csdn.net/mahoking"


How Java GC (garbage collection) works

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.