Garbage Collection
Lesson:
Main Points:
- Garbage collection is a form of memory management in which memory being
used by objects which are no longer referenced is freed.
- In the statement "String s = new String("EXAMPLE");" the s is the
reference and the actual string ("EXAMPLE") is the object.
- To dereference the object referenced by s, use this statement: "s =
null;"
- The methods, System.gc() and Runtime.gc() suggests that the Java
Virtual Machine perform garbage collectin routines.
- There is no guarantee about when garbage collection will occur.
The literature reviewed for the writing of this lesson left this question
unanswered: Is an object available for garbage collection when it goes
out of scope?
Consider this example:
public void myMethod(){
String s = new String("EXAMPLE");
System.out.println(s);
}
Should it be rewritten like this?
public void myMethod(){
String s = new String("EXAMPLE");
System.out.println(s);
s = null;
}
To answer this question we need to know that answer to this question: Is an
object available for garbage collection when it goes out of scope? If
not, then we must explicitly call the statement:
s = null;
If so, then this statement is not necessary.
Here's a better example. Suppose you have an array which you are using as a
stack (a type of data structure) and you have your pop method written like
this:
public Object pop(){
return myStack[index--];
}
Although you have decremented the counter, the object at the end of the
stack is still referenced. A better approach is to do the following:
public Object pop(){
Object returnValue = myStack[index];
myStack[index--] = null;
return returnValue;
}
This way the object at the end of the stack is dereferenced. If we go on the
theory that objects which are out of scope are automatically eligible for
garbage collection, then it is not necessary to assign null to returnValue
(otherwise, this step should be done also).
Assignment:
In this assignment you will see an implementation of a Stack data structure.
You will be able to enter items into this stack. You will be able to delete
items from this stack. Also you will be able to see a list of items on the
stack. Here's the code. All you have to do is get it up and running (display
it using Appletviewer). Make sure that the applet area you provide is just
wide enough to fit the textfield and make sure the two buttons line up on
the same row.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ex08 extends Applet{
String[] myStack = new String[10];
int c; //stack counter
TextArea view;
TextField input;
Button Add, Delete;
Label title;
public void init(){
setBackground(Color.yellow);
view = new TextArea("Stack Contents:\n", 12, 12, TextArea.SCROLLBARS_NONE);
input = new TextField(25);
Add = new Button("ADD");
Delete = new Button("DELETE");
title = new Label("STACK DEMO");
add(title);
add(view);
add(input);
add(Add);
add(Delete);
Add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
if(c<9){
myStack[c]=input.getText();
c++;
makeDisplay();
}
else{
input.setText("STACK FULL");
}
}
});
Delete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
if(c>0){
myStack[c]=null;
c--;
makeDisplay();
}
else{
input.setText("STACK EMPTY");
}
}
});
}
public void makeDisplay(){
String output = "Stack Contents:\n";
for(int i = 0; i