8
I was studying GC and the doubt arose when I ran it 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");
}
}
Exit:
Mem. total: 1808MB
Mem. util after overload: 1690MB
Mem. util after gc: 1686MB
1686 MB is a considerable memory for such a simple program, I checked in the task manager and it does not show abrupt memory usage. I am using Eclipse and the manager says that it uses 430MB, when I run the code there is an extra ~15MB.
Is there something wrong with the code? Because I can’t see this memory usage in the task manager?