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?

[603 byte] By [samhebeisena] at [2007-11-26 20:39:53]
# 1
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?
samhebeisena at 2007-7-10 1:57:06 > top of Java-index,Java Essentials,Java Programming...
# 2

> 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

kajbja at 2007-7-10 1:57:06 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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

kajbja at 2007-7-10 1:57:06 > top of Java-index,Java Essentials,Java Programming...
# 4
> characters are a byte each, so is it safe to say the approximate size ischars are two bytes each!
DrLaszloJamfa at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 5
@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)
kajbja at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 6
Why is it important to know the size?
DrLaszloJamfa at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 7

> > 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 ('?').

jschella at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 8

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

DrLaszloJamfa at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 9

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

jschella at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 10
Um... what DrLJ said.Message was edited by: DrClap
DrClapa at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 11
@Op take a look at this article: http://www.javaworld.com/javaworld/javatips/jw-javatip130.html
kikemellya at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 12

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.

ayusman_dikshita at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 13
> 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
kajbja at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 14
>serialVersionUIDstatic, that.
DrLaszloJamfa at 2007-7-10 1:57:07 > top of Java-index,Java Essentials,Java Programming...
# 15
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?
YoungWinstona at 2007-7-21 18:00:24 > top of Java-index,Java Essentials,Java Programming...