What is the basic principle of the garbage collector? Can the Garbage Collector recycle memory immediately? Is there any way to proactively notify virtual machines to recycle garbage?

Source: Internet
Author: User

 

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 heap by Directed Graphs. 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. Yes. Programmers can manually execute system. GC () to notify the GC to run, but the Java language specification does not guarantee that GC will be executed. Forcibly execute garbage collection: system. GC (). Runtime. getruntime (). GC ()

Static class: the static class belongs to the class rather than the object, which is equivalent to a global one and cannot be recycled.

Static variables are not recycled, but the objects referenced by static variables can be recycled.

GC only recycles the objects in heap, and the objects are the same. As long as there is no reference to it, it can be recycled (but not necessarily recycled ). object recycling has nothing to do with whether it is static!

For example, static vector pane = new vector (); pane = NULL; if there is no other reference, the object instance that the original pane points to will be recycled.

Java programmers no longer need to consider memory management when writing programs. Because of the garbage collection mechanism, objects in Java do not have the "Scope" concept, and only objects can be referenced with "Scope ". Garbage collection can effectively prevent memory leakage and effectively use available memory. The garbage collector is usually used as a separate low-level thread to clear and recycle objects that have been killed in the memory heap or are not used for a long time, programmers cannot call the Garbage Collector to recycle an object or all objects in real 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.