I have written this following code to check the garbage collection and expecting that garbage collection will not occur since I'm maintaining the reference of objects.
import java.util.ArrayList;
public class HeapConsumer
{
public static void main(String args[])
{
ArrayList<Integer> list = new ArrayList<Integer>();
int i = 0;
while(true)
{
list.add(new Integer(i));
System.out.println(i);
i++;
try
{
Thread.sleep(500);
}catch(InterruptedException e){}
}
}
}
But when watching the memory graph in JConsole it was sawtooth like graph indicating garbage collection.Will there any garbage collection for this code ?