Why do you want to do garbage collection?
Because the JVM itself implements the memory heap, it cannot have an automatic variable like C + + (a temporary variable), so all objects are collected. Let's take a look at C + + first:
ClassType varname;//automatic variables, created in stacks of memory, disappear automatically as the scope disappears.
ClassType varname = new ClassType ()//created in the memory heap, to be manually released by the programmer, such as:
Delete varname, equivalent to free in C
Because the JVM itself is implemented in the memory heap, so it is not possible to create automatic variables, all objects are manually released, this "manual" means to implement the program, and not necessarily programmer programming, the JVM itself to achieve the release of "normal" objects, this is the JVM garbage collector.
For objects that are not "regular", for example, call local resources directly or indirectly. (Some people think that the image erasure is not a local method call, but it is only AWT indirectly calls the local method), that is, programmers themselves programming to release resources.
How is an object tagged with garbage (recyclable objects)? Some implementations used "reference count", which is when an object is referenced, the reference count plus 1, when the reference handle disappears, the reference count slows 1, and when the reference count is 0, it can be recycled.
Does anyone say that the new object has been recycled?
The lifetime of an object is divided into Cenozoic, old generation, the new generation is divided into Eden and two living space to ensure that there is a space at any time is empty, when the object was newly new in Eden, the garbage collector will not be recycled in Eden, only when the object of Eden is full, is copied to the next space of survival. When the living space is full, only a small recovery occurs. When the object in the living space too long to reach an "aging" value is copied to the old generation, the old generation full of time when the big recovery.
However, Java does not use reference counting to mark whether an object can be recycled. Because objects in Java are likely to be referenced directly or indirectly, which is a property referenced to object B in object A, and object B references a property in a, this may cause a reference count to never be 0.
In Java, the object is tagged with "traversal by Root". Is that the handle begins to look up the object referenced by a handle, and then the object referenced by that object is then searched, so that recursion looks like a leaf from the root to the branch, if an object is not found so that it has no object to reference it.
How do I release non-Java objects? We've already said that regular Java objects are recycled by the JVM's garbage collector, but for local resources (typically local methods call local resources), we're ready to release them manually.
In general we can define code to release local resources in Finalize (), but this method is not sure to be executed, and finalize () does not run when the object exits, but only when the object is garbage collected. It's possible that the JVM has never had to recycle, So this method will never be invoked, and if you want the object to be a little bit before exiting, you have to write the code for releasing the object in the finally block of the try{}finally{}, which is why I have repeatedly stressed that the number of data links must be written here. There is another method in java1.1 that is system.runfinalizersonexit (), but it is not as effective as finally.
The same System.GC () method does not guarantee garbage collection, it is only "recommended", and garbage back to the end when it happened? The small recovery is the living space is full, the big recovery is the old generation full, this is only the prerequisite. Because garbage collection is run in a low-priority way, It begins to release the memory of an object only if the other threads are suspended waiting for memory release to occur.
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