Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to find the memory a String would occupy

 
Chandhrasekar Saravanan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandhrasekar Saravanan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ahetuki Neti
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use getBytes() on the string object and find the length of the byte array.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic