Java garbage collection mechanism--Getting Started

Source: Internet
Author: User

Java garbage Collection mechanism (GC) Every object that is created is allocated a certain amount of memory to store the object data during the program's operation.        If you blindly consume memory without releasing it, you will experience a memory overflow problem. In the process of running the program, the GC uses reference counting to count how many other objects are held by the object, and if the object has not been referenced, then the object goes into Resurrection Status(For GC threads, there are three states of objects:

1. Accessible state: There is also a variable reference in the program, then this object is accessible.

2. The Resurrection State: when there are no variables in the program that refer to this object, the object is moved from the accessible state to the resurrected state. CG threads will be ready to call this object's Finalize method at a certain time (the Finalize method inherits or overrides from Object), and the code within the Finalize method may turn the object into a reachable state, otherwise the object becomes inaccessible.

3. Unreachable state: The GC thread can reclaim the memory of this object only if the object is in a non-accessible state.

GC in order to properly dispose of objects, it is necessary to monitor the running state of each object, including application, reference, reference, assignment, etc. Monitoring, so the GC will know whether an object is in any state above. When a GC thread executes a finalize method of a resurrected state object at a certain time, when does it execute? Since different JVM implementations may use different algorithms to manage GC, at any time the developer cannot anticipate the timing of the GC thread's operations (including detecting object state, freeing object memory, and invoking the object's Finalize method). Although you can pass System.GC () and Runtime.getruntime (). GC ()The function reminds the GC thread to do the garbage collection as soon as possible, but there is no guarantee that the GC thread will be recycled immediately. In Java, the garbage collector automatically periodically scans the dynamic memory of Java objects and tags all referenced objects, clears its markup after the end of the object's run (no reference variable is associated with the object), and reclaims all unmarked objects as garbage, freeing up the memory space occupied by the garbage object. for objects that occupy a larger space(such as a large array), it is recommended that when the confirmation is no longer used, the value of NULL,JVM is not as small objects when retrieving large objects in a timely manner, Null to force him to be recovered at the next GC.

Java garbage collection mechanism--Getting Started

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.