• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how to find the memory a String would occupy

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use getBytes() on the string object and find the length of the byte array.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic