• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the size of an object?

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone tell me the size of an object
as premitives got the size why dont objects have some constant value(size)?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean the number of bytes that an object takes up in memory?

There's no way to find that out in the standard Java API. Java does not have a "sizeof" operator, like you have in C and C++.

Note that the size of an object in memory depends on the particular implementation of the JVM you are using. It might be different for the Sun JVM on Windows and the HP JVM on HP-UX, for example.

If you really are desperate to find this out in your Java application, I think there is a way to find out how many bytes an object takes up via the debugger API, but that API is not easy to use.
 
Ela Jha
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can objects have predefined size as it really depends on the number and type of instance variable it holds.
[ August 23, 2006: Message edited by: Ela Jha ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saikrishna cinux:
can anyone tell me the size of an object
as premitives got the size why dont objects have some constant value(size)?


Dear Saikrishna,

You can use the code below:




Here is the Interpreter class :


Now you compile both class and try to run like following



Hope this helps you out.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatevely check this link ..

This may be usefull for you.

Do you know your data size?
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank's a lot for your code
but if any one ask me about the object size what should tell them?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell them the question does not have a simple answer.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic