| 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
|
|
|