ข้อมูลจาก : http://www.sun.com/training/catalog/courses/CX-310-035.xml
Section 3: Garbage Collection
State the behavior that is guaranteed by the garbage collection system.
Write code that explicitly makes objects eligible for garbage collection.
Recognize the point in a piece of source code at which an object becomes eligible for garbage collection.
Rule
http://www.janeg.ca/scjp/gc.html
- the Java garbage collector consists of three basic activities:
- monitors program objects to determine when they are no longer required
- informs selected objects that they should release any non-memory resources
- destroys objects and reclaims their memory resources
- the gc operates as a seperate asynchronous background thread that tracks all program objects
- an object ceases to be needed by a program when it is no longer reachable
- an object is reachable if a reference to the object exists in any variables of any executing code
- an object is subject to garbage collection when it can no longer be reached but it is not necessarily garbage collected immeadiately
- there are no guarantees as to when the gc will reclaim an object or the order in which objects are reclaimed
- there is no way to tell if and when an object will be collected, you can only tell when an object becomes eligible for garbage collection
- you can request garbage collection by calling one of
Runtime.getRuntime().gc() // no guarantee gc will run
System.gc() // no guarantee gc will run
- you can also request that the finalize() method be run for objects deemed eligible for collection but which have not yet had their finalization code run
Runtime.runFinalization()
System.runFinalization()
|