6

I am experimenting with inheritance and for educational purposes want to examine the addresses allocated for various objects and the fields within the object. Is there a tool which will let me see what memory the JVM is using and what it is using it for.

For example if I have two classes:

class A { int i,j; int f { ...} }
class B extends A { int c; /* more methods, overriding f and declaring new ones as well */ }

and instantiate these classes in objects a and b.

Is there a tool I can use to profile the memory usage and see exactly what memory is allocated for these?

Thank you!

Don Roby
  • 40,677
  • 6
  • 91
  • 113
Froskoy
  • 2,967
  • 5
  • 20
  • 21

3 Answers3

15

Since the original question was posted the situation changed a bit.

The jol tool ("java object layout", by Aleksey Shipilev), is now part of OpenJDK allows you to check actual memory layout and usage of classes. http://openjdk.java.net/projects/code-tools/jol/

Example output looks like this:

$ java -jar jol-cli/target/jol-internals.jar java.util.HashMap
  Running 64-bit HotSpot VM.
  Using compressed references with 3-bit shift.
  Objects are 8 bytes aligned.
  Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
  Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

  java.util.HashMap object internals:
   OFFSET  SIZE       TYPE DESCRIPTION                    VALUE
        0     4            (object header)                01 00 00 00 (00000001 00000000 00000000 00000000)
        4     4            (object header)                00 00 00 00 (00000000 00000000 00000000 00000000)
        8     4            (object header)                0f 0f 3e e0 (00001111 00001111 00111110 11100000)
       12     4        Set AbstractMap.keySet             null
       16     4 Collection AbstractMap.values             null
       20     4        int HashMap.size                   0
       24     4        int HashMap.threshold              16
       28     4      float HashMap.loadFactor             0.75
       32     4        int HashMap.modCount               0
       36     4        int HashMap.hashSeed               0
       40     4    Entry[] HashMap.table                  []
       44     4        Set HashMap.entrySet               null
  Instance size: 48 bytes (estimated, add this JAR via -javaagent: to get accurate result)
  Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
Konrad 'ktoso' Malawski
  • 13,102
  • 3
  • 47
  • 52
  • 1
    Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] what is that? – ilw Feb 18 '17 at 14:35
  • 6
    @Andreyua sizes of: object reference, boolean, byte, char, short, int, float, long, double – Sergey Khudyakov Jul 05 '17 at 04:22
2

I guess is instructive first to have an image about how function JVM over some operating system, so take a look on The Java Virtual Machine. Also, a related question is https://softwareengineering.stackexchange.com/questions/151076/approaching-java-jvm-internals

Community
  • 1
  • 1
Mihai8
  • 3,113
  • 1
  • 21
  • 31
0

No, no such tool exists, though this article explains the basics of how the memory layout works in e.g. OpenJDK. (Notably, additional methods in a class take zero overhead in instances of that class.)

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413