logic of Garbage collector in java
As we know Garbage collector is Thread in java. And every thread will have its logic to execute. 开发者_开发技巧So i wanted to know what logic does this Garbage collector use which maintains the memory so well.
thanks
An object is considered garbage when it can no longer be reached from any pointer in the running program. The most straightforward garbage collection algorithms simply iterate over every reachable object. Any objects left over are then considered garbage. The time this approach takes is proportional to the number of live objects, which is prohibitive for large applications maintaining lots of live data.
Taken from Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine
This thread wakes up every so often and reclaims unused memory. You might look into Mark-and-Sweep algorithm. It is also a daemon thread.
EDIT : Garbage collector finds and deletes object which are not reachable from the main thread.
The JVM automatically re-collects the memory which is not used any more. The memory for objects which are not referred any more will be automatically released by the garbage collector.
To see then the garbage collector starts working add the command line argument "-verbose:gc" to your virtual machine.
An in-depth article about the garbage collector can be found here: Tuning Garbage Collection with the 5.0 Java Virtual Machine
精彩评论