-2

I have the following code:

public class Test2 {
    public static void main(String[] args) {

        long ta, after, before;
        Runtime rt;
        rt=Runtime.getRuntime();
        rt.gc();
        NodoPrueba q=new NodoPrueba(1); 
        before=rt.freeMemory();
        q=null;
        rt.gc();
        after=rt.freeMemory();
        ta=after-before;
        System.out.println("Before: "+before+"\nAfter: "+after);
        System.out.println("\nDifference: "+ta);
    }

}

Well, the class NodoPrueba only has an int attribute. But i have the following output:

Before: 127991976

After: 128673800

Difference: 681824

If the theory says that NodoPrueba class only takes 16 bytes on memory: 8 bytes from the object, 4 bytes for int and 4 bytes for padding (object's size is always a multiple of 8), where does the difference come from?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
José María
  • 229
  • 1
  • 4
  • 10
  • 1
    The call to `freeMemory` returns "an **approximation** to the total amount of memory *currently available for future allocated objects*, measured in bytes." You can't use it to determine the size of objects. – Erwin Bolwidt Mar 22 '17 at 04:37

1 Answers1

0

from what I understand java also stores in the heap memory the classes that are being used as well as other threads not only what you are coding, if you want to take a look about what is really happening in your heap I recommend you to give a try to visualvm, is is a java utility which allows you to explore the content of any Java app when it is running https://visualvm.github.io in an accurate way