Hello. I need to know how much memory my object takes, and I do it like this:

The object itself:

public class DemoGC { private String fieldOne = "abc"; private String fieldTwo = "cba"; } 

I do it in the following way:

  public static long memoryState() { Runtime runtime = Runtime.getRuntime(); return runtime.totalMemory() - runtime.freeMemory(); } public static void main(String[] args) { long before = memoryState(); DemoGC demoGC = new DemoGC(); long after = memoryState(); System.out.println(after - before); } 

The object itself is clearly not large, just a couple of lines ... But, at the output, I get a terrible number: 335568 . Even considering that this is the size in bytes, where does so much come from? Maybe I'm doing something wrong? Tell me, what is the error? What do I think wrong?

0