2

I was looking into this and just wondering does Java provide any construct to find out the size of an object?

newSpringer
  • 1,018
  • 10
  • 28
  • 44
  • 1
    possible duplicate of [Calculate size of Object in Java](http://stackoverflow.com/questions/9368764/calculate-size-of-object-in-java) – Kazekage Gaara Aug 22 '12 at 15:49
  • I think it is more meaningful to consider the size of primitive/compound primitive types in an object. – CodeClown42 Aug 22 '12 at 15:51
  • Do you need to know the size during execution or are you looking for a way to measure/monitor your memory usage? If so, ckeck visualVM: http://visualvm.java.net/index.html – kothvandir Aug 22 '12 at 16:01

3 Answers3

2

Unfortunately not. It's relatively complex. e.g. if I create a String object, I have to consider:

  1. the size of the fields of the objects. For primitives etc. that's simple
  2. the size of objects referred to. Each member object is a reference, and not actually contained exclusively within the object under question. e.g. String contains a reference to a char array, but that char array can be shared across multiple Strings (see the source of substring() to understand how - this is known as the flyweight pattern)
  3. the size of any native implementation details in the JVM
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

No. It goes against the concept of the language. In a real Object Oriented Programming language (not a hacked-together OOP support like C++), Objects are abstract concepts, not bits on the computer. Until and unless you serialize the object, it's treated like an actual object and not a sequence of bits.

Hip Hip Array
  • 4,665
  • 11
  • 49
  • 80
  • 1
    Regardless I'd still like to understand how big my Trade object is in bytes – Brian Agnew Aug 22 '12 at 15:51
  • 1
    @Hip, based on what are you making this statement? Can you provide a reference? – Dmitry B. Aug 22 '12 at 15:51
  • @BrianAgnew: that is a very complex metric. If there is one such object, the relatively of shared code segments is more than when there are 2 or 10 such objects. So what counts toward the "size of an object"? – CodeClown42 Aug 22 '12 at 15:54
  • @goldilocks - I don't doubt it's complex. See my answer for more thoughts. But ultimately someone will ask me how much memory a Trade object will consume, and *some* sort of answer is better than none. – Brian Agnew Aug 22 '12 at 15:56
  • @KazekageGaara: I think it is correct in the sense that some parts of an object are not stored in the object at all, they are in stored in the class. However, this is just as true in C++, where the size of an object is considered to be the sum of its *data* segments -- though if some of the "data" is more objects, only their data is considered. So "the size of an object" is just the sum of the primitives inside it, plus the pointer store necessary to a reference. – CodeClown42 Aug 22 '12 at 16:00
0

Actually i think you can get the size of an object with the help of the Instrumentation class, but the process is a little more complex(you have to specify in the manifest file the premain class, define a Instrumetation agent etc.) compared to the one in C++ where you have at your use the sizeof() function.

One disadvantage is you get the size of the object but not the size of the referred objects(if it has any).

Another way would be, but this is rudimentary, to have your object under test serialised and wrote in a file and have the size of the file(use the ObjectOutputStream and get its size).For further documentation related to this subject and not only read a little bit about Java agents in general and Java probing.Probing the JVM is a very helpful technique especially if you want to make an analisys on performance(objects sizes, running threads, memory leaks etc.).

Charles
  • 570
  • 11
  • 29