This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes how to find the memory a String would occupy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to find the memory a String would occupy" Watch "how to find the memory a String would occupy" New topic
Author

how to find the memory a String would occupy

Chandhrasekar Saravanan
Ranch Hand

Joined: Jun 16, 2002
Posts: 57
Hi ,
I would like to check the memory the string occupies using java.
is there also an way, i could find the memory a file would occupy
Thanks
Chandhrasekar Saravanan
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well the String itself is implemented at a char array, so just count the chars.
Then there is a little overhead elsewhere to track it's location.
A file could be more complex. You could just read it out byte by byte and find out.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Chandhrasekar Saravanan
Ranch Hand

Joined: Jun 16, 2002
Posts: 57
hi cindy,
I really couldnt get your answer. i am able to count the charecters of the string. but i couldnt find any method to find the memeory it would occupy.
please help me with a little more detailed answer
Thanks
Chandhrasekar Saravanan
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
I really couldnt get your answer. i am able to count the charecters of the string. but i couldnt find any method to find the memeory it would occupy.
Do you mean the physical address of the String object in memory?


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If you are referring to the AMOUNT of memory, rather than the LOCATION in memory, each char is 2 bytes long in Java. So if a String has 10 characters, it occupies 20 bytes, plus whatever overhead is needed for the String class itself.
HTH
Layne


Java API Documentation
The Java Tutorial
Ahetuki Neti
Greenhorn

Joined: May 29, 2003
Posts: 20
Use getBytes() on the string object and find the length of the byte array.


Ahetuki, the Constant.
 
 
subject: how to find the memory a String would occupy
 
Similar Threads
object references and memory space requirements
Get size of the collection
How is heap memory allocated?
Memory allocated to reference variables
Doubt in GC