• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Size of a empty class

 
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).
 
Bartender
Posts: 10336
Hibernate Eclipse IDE 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.
 
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
 
lowercase baba
Posts: 12954
65
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?
 
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'?
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • 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 !!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic