• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is there a sizeof() in Java?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no sizeof, but you might try this.

- Peter
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic