3

The JOL tool gives ability to count object's memory layout.

I've noticed, that static fields do not participate in calculating, for example:

public class Foo {

    private static final int i = 1;

    private char value;

    public Foo(char value) {
        this.value = value;
    }
}

then,

System.out.println(ClassLayout.parseClass(Foo.class).toPrintable());

gives the following output:

com.kishlaly.Foo object internals:
 OFFSET  SIZE  TYPE DESCRIPTION                    VALUE
      0    12       (object header)                N/A
     12     2  char Foo.value                      N/A
     14     2       (loss due to the next object alignment)
Instance size: 16 bytes (estimated, the sample instance is not available)
Space losses: 0 bytes internal + 2 bytes external = 2 bytes total

Where does private static final int lies in memory?

Sergey Ponomarev
  • 2,947
  • 1
  • 33
  • 43
Vladimir Kishlaly
  • 1,872
  • 1
  • 16
  • 26
  • `i` clearly isn't stored in object instances (that's what `static` means). Where exactly it gets placed depends on the JVM. (I don't know more details off the top of my head, hence why I'm commenting rather than answering.) – Jeffrey Bosboom May 03 '15 at 17:33
  • yes, the question is where do statics reside =) – Vladimir Kishlaly May 06 '15 at 08:15

1 Answers1

3

The tool is giving the memory layout of an object on the heap. The static content is in the PermGen section of the memory & it's on the JVM implementation whether it is included in the heap or not.

Your tool provided the memory layout of the object, whereas static variable is a class level variable it will be always in the permanent generation section of memory & would not be included in this layout.

underdog
  • 4,447
  • 9
  • 44
  • 89