I have two functions below one is insert() and other one is startGC().
I will call insert() method first which will take some 300MB of heap space. After that I will call startGC() which should release the memory allocated in heap because all the vector objects are local to the function but its not happening.
private void insert()
{
Vector v=new Vector();
Vector v1=new Vector();
Vector v2=new Vector();
String str="Hello";
for (long i = 0L; i < 999999L; i++) {
v.add(str + i);
v1.add(str + i);
v2.add(str + i);
}
v=null;
v1=null;
v2=null;
}
private void startGC()
{
System.gc();
}
My Question:
1) Why Garbage collect is not working in this example.
2) How to make JVM to garbage collect all unused memory blocks.
Any code sample to achieve the same.