Approximate size of object
Could someone approximate the size of this object
privatestaticclass PacketTypeimplements Serializable{
int type;
int SeqNum;
int length;
String data;
}
The string will not exceet 65 characters
Also, is there a way to make the ints unsigned? will it save space?
i now knowints are 4 bytescharacters are a byte each, so is it safe to say the approximate size is65 + 12 + overhead 8 = 85?making ints unsigned wont change the space, it will just change the range of allowed numbers.Can anyone concurr?
> Could someone approximate the size of this object
>
> private static class PacketType implements
> Serializable{
> int type;
> int SeqNum;
> int length;
> String data;
> }
>
> The string will not exceet 65 characters
I don't know. About 200 bytes?
> Also, is there a way to make the ints unsigned?
No
> will
> it save space?
No
Kaj
> characters are a byte each, so is it safe to say the
> approximate size is
Wrong, a char is 16 bits, two bytes, but unicode characters can be even more than one char.
> 65 + 12 + overhead 8 = 85?
There's more to it. It depends on the memory model of the machine and how it can address the memory.
Kaj
> characters are a byte each, so is it safe to say the approximate size ischars are two bytes each!
@Op. You can write some code that uses instrumentation and getObjectSize to get an estimate of the size on the local machine: http://java.sun.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html#getObjectSize(java.lang.Object)
Why is it important to know the size?
> > characters are a byte each, so is it safe to say
> the
> > approximate size is
>
> Wrong, a char is 16 bits, two bytes, but unicode
> characters can be even more than one char.
>
I don't believe that java uses a multibyte char set for unicode. Every char is 2 bytes. Conversions from multibyte char sets into that set would either produce an exact 16 bit mapping or will result in the unmapped char ('?').
> I don't believe that java uses a multibyte char set
> for unicode. Every char is 2 bytes. Conversions
> from multibyte char sets into that set would either
> produce an exact 16 bit mapping or will result in the
> unmapped char ('?').
The javadoc for String states:
<quote>
A String represents a string in the UTF-16 format in which supplementary
characters are represented by surrogate pairs (see the section Unicode
Character Representations in the Character class for more information).
Index values refer to char code units, so a supplementary character uses two
positions in a String.
</quote>
is that what you mean?
> > I don't believe that java uses a multibyte char set
> > for unicode. Every char is 2 bytes. Conversions
> > from multibyte char sets into that set would either
> > produce an exact 16 bit mapping or will result in the
> > unmapped char ('?').
>
> The javadoc for String states:
>
> <quote>
> A String represents a string in the UTF-16 format in
> which supplementary
> characters are represented by surrogate pairs (see
> the section Unicode
> Character Representations in the Character class for
> more information).
> Index values refer to char code units, so a
> supplementary character uses two
> positions in a String.
> /quote>
>
> is that what you mean?
Err no....that would suggest the opposite of what I said. So I stand corrected.
Um... what DrLJ said.Message was edited by: DrClap
@Op take a look at this article: http://www.javaworld.com/javaworld/javatips/jw-javatip130.html
int 4 bytes each; so 3 * 4 =12 bytes.
String with max 65 chars (considering 2 bytes each); 65 * 2 = 130.
Total, 130 + 12 = 142 bytes.
but the actual size will be more then this; because over head is also associated with these variables.
So you can assume it around 150 bytes approx.
Is there any specific reason to know the size of the object?
hope this helps.
> So you can assume it around 150 bytes approx.You forget that the String class also has attributes, e.g. offset, count, value, hash and serialVersionUIDKaj
>serialVersionUIDstatic, that.
If your only concern is to optimize size, seeing that your String object is potentially by far the largest, why not just provide custom serialization and zip the output/unzip the input? Or do you need to know exactly how much memory one of your objects is going to be taking?