0

Possible Duplicate:
In Java, what is the best way to determine the size of an object?

Is there a way to find how much memory any java object is using? e.g: Consider the class below

class A {
    byte b;
}

When i do: new A(), How much memory is used, assuming that we are running on a 64-bit JVM?

Community
  • 1
  • 1
Chander Shivdasani
  • 9,878
  • 20
  • 76
  • 107
  • 2
    I responded in regards to a question on JRuby and memory footprint, but you should look at jhat, jmap, and visualvm. All are tools that come with the JDK. http://stackoverflow.com/questions/8327049/how-to-get-the-ram-footprint-of-a-specific-object-in-jruby/8327971#8327971 – buruzaemon Dec 07 '11 at 07:59
  • Have you seen this ? http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object – greenLizard Dec 07 '11 at 08:00
  • The best way to determine how much memory you are using is to use a profiler. Instead of worry about how many bytes you are using, you can instead worry about how much the memory is worth. i.e. it likely to not be worth your time worrying about it. http://vanillajava.blogspot.com/2011/11/ever-decreasing-cost-of-main-memory.html – Peter Lawrey Dec 07 '11 at 08:07

1 Answers1

0

No, JVM does not work the classic way. Object initializations are always object-graphs.

But you are able to use the ObjectOutputStream to export that instance to an FileOutputStream.

See also: http://www.javaworld.com/javaworld/javatips/jw-javatip130.html

Grim
  • 1,938
  • 10
  • 56
  • 123
  • An Integer uses 82 bytes in an ObjectOutputStream, two Integers use 92 bytes. In reality, an Integer uses 16 - 24 bytes of memory (depending on whether you are using 64-bit references or not) – Peter Lawrey Dec 07 '11 at 08:09