|
return to computer science and java
Section 1: Declarations and Access Control
Section 2: Flow Control, Assertions, and Exception Handling
Section 3: Garbage Collection
Section 4: Language Fundamentals
Section 5: Operators and Assignments
Section 6: Overloading, Overriding, Runtime Type and Object Orientation
Section 7: Threads
Section 8: Fundamental Classes in the JAVA.LANG Package
Section 9: The Collections Framework
|
|
- State the behaviour that is guaranteed by the garbage collection system.
- Write code that explicitly makes objects eligible for garbage collection.
- Recognise the point in a piece of source code at which an object becomes eligible for garbage collection.
Garbage Collection
- Memory Allocation
- at run-time
- method/local variables and method arguments(including references to objects) are allocated space on the stack
- discarded when method returns
- objects (when created with
new)
- go on the heap
- have a longer lifetime - only collected when nothing is referencing it anymore
- eventually cleaned up by the garbage collector, which is a low-priority thread, straight after the JVM calls the object's
finalize() method
System.gc() and Runtime.gc()
- suggests to the JVM to run the garbage collector
- garbage collection algorithm is platform dependant
- Setting References to
null
- when no longer using a reference, else can cause a memory leak - gc can't removed an object even though nothing is using it anymore
- eg. popping an object off a stack (implemented as an array) - don't just want to decrement the index to the array, but also set
myStack[oldIndex] = null;
|