|
|
Garbage Collection Certification - Behaviour
- 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()
Also see
Traps
- a question that targets a specific object for garbage collection (can't be done)
- a question that presumes to force the gc to run (can only suggest it run)
|