• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SizeOf() in java

 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I want to see the Size of the object in java.
i their any API to give that. Like in C++ we have SizeOf();

Thanks
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No there isn't. There are some tricks you can use to determine the size of an object (for example see this rather dated article. But they are not things you would want to do in production code. It's more for the initial development work.

You can also use profilers to look at the size of objects.

Take a look at this google search for more discussion on the subject, as it is a frequently asked question.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p.s. Java 5 comes with a (basic) profiler tool built in. Take a look the SDN article HPROF: A Heap/CPU Profiling Tool in J2SE 5.0 for more info.
 
Sid Murrey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Java 6 SDK Update 7, you also get the Java VisualVM right inside the JDK package.
https://visualvm.dev.java.net/
July 8th, 2008: JDK 6 Update 7 with Java VisualVM released. Starting from JDK 6 Update 7, VisualVM is also available as a JDK tool called Java VisualVM. This way you will get a stable tool tested with respective JDK distribution. Latest Sun JDK 6 update can be downloaded here.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi chiru,

There is no API provided to find the size of an object......
but there is an alternative way to find the size of as object
its in Runtimeclass in lang package....
Runtime obj = Runtime.getRuntime();
System.out.println(obj.totalMemory() - obj.freeMemory());
which gives you the size of an Object


 
Maneesh Godbole
Saloon Keeper
Posts: 11060
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh,
Please dont http://faq.javaranch.com/java/DontWakeTheZombies
This thread is almost one year old.
 
karimkhan pathan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reply helps......Thanks ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic