It's not a secret anymore!*
The moose likes Java in General and the fly likes Size of a empty class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Size of a empty class" Watch "Size of a empty class" New topic
Author

Size of a empty class

Jayavardhan geepi
Ranch Hand

Joined: Jul 21, 2010
Posts: 66
Hi, Can anybody tell me what would be the size of a instance of a class in java that contains no instance variables and methods?.( That is I call it as empty class though it extends java.lang.Object).
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What do you mean by size? The size of the file on disk?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jayavardhan geepi
Ranch Hand

Joined: Jul 21, 2010
Posts: 66
Size mean the amount of memory (in bytes) allocated on heap by JVM.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Using instrumentation (with getObjectSize) it seems an instance of EmtpyClass uses as much heap space as an instance of Object...
-> http://www.javaspecialists.co.za/archive/Issue142.html


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

I am not sure, but this may vary depending on the specific JVM you are using. I don't think there is a single answer of "It will always be X bytes".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jayavardhan geepi
Ranch Hand

Joined: Jul 21, 2010
Posts: 66
Ok. Let me get answer for generalized question.
How to find how much memory an object aquires?
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
I agree with Fred.

You can try to see if Java Visual VM (1.6) can do that.

I am unaware of any methods which can allow you to do through the program itself.


Regards,
Avishkar Nikale
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Avishkar Nikale wrote:I am unaware of any methods which can allow you to do through the program itself.

Did I already mention instrumentation and using method 'getObjectSize'?
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

I dont think getting the size of an object would be possible. All you can do is get the total,max & free memory of the JVM.
You can find those methods in java.lang.Runtime class.


OCPJP 6
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Peter Taucher wrote:
Did I already mention instrumentation and using method 'getObjectSize'?


Peter,

I am aware about it now. I had never used the java.lang.instrument package.

Thanks a lot !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Size of a empty class