public class ReferenceTest {
public static void main(String[] args){
final List integerList = new ArrayList();
final List refList = new Vector();
final Map referentNameMap = new Hashtable();
final ReferenceQueue refQueue = new ReferenceQueue();
new Thread() {
public void run() {
while (true) {
try {
Reference ref = refQueue.remove();
String referentName = (String) referentNameMap.get(ref);
ref.clear();
refList.remove(ref);
referentNameMap.remove(ref);
public void actionPerformed(ActionEvent e) {
Random random = new Random();
for (int i = 0; i < 100 && integerList.size() > 0; i++) {
int index = random.nextInt(integerList.size());
integerList.remove(index);
}
System.gc();
}
});
public void actionPerformed(ActionEvent e) {
Random random = new Random();
for (int i = 0; i < 100; i++) {
final Integer t = new Integer(random.nextInt());
integerList.add(t);
PhantomReference ref = new PhantomReference(t, refQueue);
referentNameMap.put(ref, "[" + t + "]");
refList.add(ref);
}
}
});
JFrame f = new JFrame();
JPanel p = new JPanel();
p.add(genBtn);
p.add(gcBtn);
f.getContentPane().add(p, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}