• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Size of a empty class

 
Jayavardhan geepi
Ranch Hand
Posts: 66
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by size? The size of the file on disk?
 
Jayavardhan geepi
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Size mean the amount of memory (in bytes) allocated on heap by JVM.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Jayavardhan geepi
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Let me get answer for generalized question.
How to find how much memory an object aquires?
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic