aspose file tools*
The moose likes Java Micro Edition and the fly likes size of a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "size of a string" Watch "size of a string" New topic
Author

size of a string

amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
Hello,
How to calculate the size of a string. I have a requirement wherein to calculate the size of an object. The closest that i would come up with is:

say for eg: a string is "amal"

now considering that a character is 2 bytes....so the size of above string is 8 bytes. Is my assumption right?

help appreciated
cheers
amal shah
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by amal shah:
Hello,
How to calculate the size of a string. I have a requirement wherein to calculate the size of an object. The closest that i would come up with is:

say for eg: a string is "amal"

now considering that a character is 2 bytes....so the size of above string is 8 bytes. Is my assumption right?

help appreciated
cheers
amal shah


How about this one
STRING LENGTH
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

If you need to know how many bytes exactly an object takes up in memory: There is no (easy) way to know that in Java. And it's also dependent on the implementation of the JVM; for example, a String object with a certain content might be 20 bytes when you're using Sun's JVM, and it might be 24 bytes when using IBM's JVM, and something different on someone else's JVM.

If you really need to know, then there is maybe (I'm not sure) a way to find out how much memory an object takes using the JVM's debugger API.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

Generally speaking, Java was designed in such a way that you don't care about memory management, the size of objects or stuff like that.

An important question is WHY do you feel you need to know? What are you trying to accomplish?

If it's nothing more than a theoretical exercise for a class or for fun, that's one thing. But in the real world, this is seldom, if ever, an issue.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by amal shah:
...I have a requirement wherein to calculate the size of an object...

When I see questions like this, I always wonder if there's a professor out there who has thrown together a Java course using old material from other languages.

In any case, you might be interested in this article: JavaWorld - Sizeof for Java: Object sizing revisited.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
When I see questions like this, I always wonder if there's a professor out there who has thrown together a Java course using old material from other languages.



No..it's not for the above thing above. It has a purpose behind it.

An important question is WHY do you feel you need to know? What are you trying to accomplish?

If it's nothing more than a theoretical exercise for a class or for fun, that's one thing. But in the real world, this is seldom, if ever, an issue.


My purpose is for the use in J2ME RMS.

Say for example if i want to store 2 strings one int data and a short data in a record. In order to differentiate between this kind of data while retrieval i have to place some Unique Identifier. But in rarest of the rare case this Unique Identifier's also appear in the data,so
all things start going wrong then.

So the best idea possible was to store data in following format:

[size data size data]

Help in this regard would be highly appreciated
cheers
amal shah
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

You can find the length of a String by calling its length() method. So I don't see any need for storing the length of the String as well as the String itself. That's just redundant. And you don't want to be storing redundant data in small configurations like J2ME.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

Agreed. If all you want to do is store/retrieve the string in/from a text file, then the writing/reading the length of the string (using the length() method) is good enough. You don't need to know the actual size of the string object, which includes implementation and JVM details.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
Hello,
In J2ME RMS, storage of data is always in byte format. So the concept of "length of a string" won't work.

help appreciated
cheers
amal shah
amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
Hello,
In J2ME RMS, storage of data is always in byte format. So the concept of "length of a string" won't work.

help appreciated
cheers
amal shah
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Since you posted in the Java in General (beginner) forum I assumed you were asking a question about Java. But it appears you were asking about something completely different. You should have made that clear long ago.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

I don't know what J2ME RMS format is, but...:

You convert characters to bytes and vice versa using a certain character set encoding, for example ASCII or UTF-8. Class String has several getBytes() methods that convert the characters in the string to an array of bytes using a certain character set. Have a look at the API documentation of class String.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16246
    
  21

RMS is Record Management Store (i. e., database). The question appears to be "how much memory does a string occupy in RMS store? Which, of course, is totally different than how much memory a Java String object occupies, since among other things, a RMS record may not reside in primary RAM - it may be stored on a memory card.

I'm not a major expert in RMS, but I got the impression it's a lot like the Palm Database format, and strings in PDBs I believe were normally in C format (text with trailing null), for a total length (non-Unicode) of n+1 bytes, where n is the string length. This is disregarding any compression that might be applied.

Also note that String.getBytes doesn't simply dump bytes out of the string's internal buffer - it does a code conversion, and that may result in the Java String (16-bit characters) being returned in something like UTF-8 (8-bit character) format.

The storage occupancy of Java Strings is, as was mentioned, quite another matter, and it's virtually impossible to predict, since while on the one hand, each java.lang.String object takes up a certain amount of RAM, but the actual string text is subject to all sorts of optimizing tricks (since Strings are immutable).


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: size of a string