I was studying GC and the question came up when I rode the below:
public class Garbage
{
public static long carregarMemoria()
{
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 100000; i++)
{
list.add(i);
}
return Runtime.getRuntime().freeMemory();
}
public static void main(String[] args)
{
Runtime rt = Runtime.getRuntime();
int MB = 1_048_576; //Bytes em um MB;
long total = rt.maxMemory() / MB;
long memUtil = total - carregarMemoria() / MB;
System.out.println("Mem. total: " + total + "MB");
System.out.println("Mem. util após sobrecarga: " + memUtil + "MB");
rt.runFinalization();
rt.gc();
memUtil = total - rt.freeMemory() / MB;
System.out.println("Mem. util após gc: " + memUtil + "MB");
}
}
Output:
Mem. total: 1808MB
Mem. useful after overhead: 1690MB
Mem. useful after gc: 1686MB
1686 MB is a considerable memory for a program so simple, I checked the task manager and it does not accuse an abrupt use of memory. I'm using Eclipse and the manager says it uses 430MB, when I squeeze the code there is an extra ~ 15MB.
Is there something wrong with the code? Why can not I see this memory usage in the task manager?