File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes SizeOf() in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "SizeOf() in java" Watch "SizeOf() in java" New topic

SizeOf() in java

Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

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();


Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: Dec 17, 2003
Posts: 624

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

Joined: Jul 07, 2008
Posts: 58
With Java 6 SDK Update 7, you also get the Java VisualVM right inside the JDK package.
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

Joined: Apr 06, 2009
Posts: 296
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

Creativity is nothing but Breaking Rules
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10836

Please dont
This thread is almost one year old.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
karimkhan pathan
Ranch Hand

Joined: Jul 14, 2008
Posts: 86
The reply helps......Thanks ..

I agree. Here's the link:
subject: SizeOf() in java
It's not a secret anymore!