0

I just write a program which contains a self-defined class---Event. It has 5 variable member with get and set method. Code is here.

public class Event {
    private String CaseID;
    private String Activity;
    private int StartTime;
    private int CompleteTime;
    private String Notes;
}

Now I am confused because every object of Event class takes 32 Bytes in the IDE profiler result. But actually it should be larger than that. Every String object takes at least 18 bytes in my computer. I think you may not trust the calculation of string object so I add a link which contains the explanation. Memory usage of String in Java.

Community
  • 1
  • 1
Adam Lyu
  • 431
  • 1
  • 5
  • 17

1 Answers1

0

Event has a fixed size, including references to Strings. References take a constant number of bytes (fixed for your VM implementation).

The String might be somewhere else and be arbitrarily large, but the Event object itself has a fixed size, which apparently, according to your IDE profiler, is 32 bytes.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • object references should be represented as 64 bits because my computer is 64-bit system. Also, it should be bit rather that byte, right? Any ideas? – Adam Lyu Jan 26 '16 at 02:55
  • here is the link.http://stackoverflow.com/questions/981073/how-big-is-an-object-reference-in-java-and-precisely-what-information-does-it-co – Adam Lyu Jan 26 '16 at 02:56
  • @SHUYULYU As that link specifies, your JVM may be using Compressed Oops, in which case they'd be 32 bits or 4 bytes. That's actually consistent with what you're seeing: 8 bytes for an object header, 12 bytes for three `String` references, and 8 bytes for two `int` fields makes 28 bytes for alignment reasons, and then the JVM has to make it a multiple of 8 bytes, so it rounds up to 32 bytes. – Louis Wasserman Jan 26 '16 at 02:59
  • Hey, it is really a good and detailed comment. And I find that the object header should be at least 12bytes if I use compressed oops. It make sense in that way. Really good to me as a newbie. – Adam Lyu Jan 26 '16 at 03:20
  • And sorry for not voting it up cause I have zero reputation :( – Adam Lyu Jan 26 '16 at 03:21