File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes is there a sizeof() in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "is there a sizeof() in Java?" Watch "is there a sizeof() in Java?" New topic
Author

is there a sizeof() in Java?

Dan Phan
Greenhorn

Joined: Jul 29, 2004
Posts: 3
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

Joined: Apr 20, 2000
Posts: 3252
There's no sizeof, but you might try this.

- Peter
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Oct 12, 2000
Posts: 5093
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.


42
Dan Phan
Greenhorn

Joined: Jul 29, 2004
Posts: 3
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jul 29, 2004
Posts: 3
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

Joined: Apr 20, 2000
Posts: 3252
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
 
subject: is there a sizeof() in Java?