File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is there a sizeof() in Java?

 
Dan Phan
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you help? How do you check size of a object that you create. For example:

String temp = "abcde";

How many bytes does this string take in the memory?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no sizeof, but you might try this.

- Peter
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the reason for sizeof in C/C++ (and other?) languages is that the size of a type is not fixed on all platforms. For instance, an "int" might be 2 bytes on some old systems, 4 on typical current systems and maybe bigger on super-duper systems.

In Java, the size of each type is fixed by the language specification. A JVM is not free to change the size of any type. For instance, an "int" is always 4 bytes, whatever system it runs on.

Therefore, it is safe to "hard-code" the sizes of types into your code. You may just use a literal 4 to represent the number of bytes in an "int", for instance.

Can someone answer me a more subtle question? I know that a Java "int" is required to always appear to have 4 bytes, from the point of view of Java code. But are they actually required to use 4 bytes of memory? Obviously, they can't use less, but can they use more (padding)? A pure Java program can't find this out, because it can't get real pointers, but perhaps native, debugging and/or profiling interfaces do make such a requirement.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
Can someone answer me a more subtle question? I know that a Java "int" is required to always appear to have 4 bytes, from the point of view of Java code. But are they actually required to use 4 bytes of memory? Obviously, they can't use less, but can they use more (padding)?


Yes, they are allowed to actually use more.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
In Java, the size of each type is fixed by the language specification. [...] Therefore, it is safe to "hard-code" the sizes of types into your code.
Not if you're interested in the memory footprint rather than the size of the data type, for exactly the reason you mention. Although the space occupied by an int primitive is generally 4 bytes - not sure you get any guarantee - the space occupied by an Object varies from JVM to JVM and from version to version.

- Peter
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you don't do direct memory manipulation in Java, there's no need to know.

In C you can for example reassign a variable to be a pointer to the next struct in an array by increasing its value by the size of the struct type, in Java there's no such capability.
 
Dan Phan
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for responding my message. I am currently doing a project. Knowing the size of an object (String) that will take in the memory is important to me. Not because of curious. Please help if you can.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:
As you don't do direct memory manipulation in Java, there's no need to know.
Usually true, but not always. I have been in situations where I was using an LRU cache for objects that varied considerably in size. Under such circumstances you cannot control memory usage by simply keeping the number of cache entries constant. You really want to know the (approximate) footprint of the objects you're caching. A sizeof operator would have been really convenient. Heinz' code does a credible job though.

- Peter
 
Dan Phan
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Peter den Haan, I've read the article that you mentioned. It is what I am looking for. However, I can not run the program. Compile error. Missing junit.framework.TestCase.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are just the unit tests; you don't need them to use the code, just MemoryCounter and MemorySizes (you must learn about unit tests one of these days though, but that's another matter).

- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic