Skip to main content

primitive type declaration memory allocation sizes

6 replies [Last post]
jeremygwa
Offline
Joined: 2006-01-17

by looking at a peice of code, can I predict or calculate, how much memory may be allocated in (bytes or KB), at a givin line. (this will exclude vm runtime garbage collection)

eg .
int [] data = new int[100];
String test = "this is a test";
boolean T = false;
char [] c = new char[10];
... etc
what are the memory allocations estimations for all primitive types in arrays or singlys?

-Jer A.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jeremygwa
Offline
Joined: 2006-01-17

> On a 32bit Sun Hotspot VM, each object takes an 8
> byte header + fields.
> Arrays take 12 byte header + contents. I am not
> positive how well fields are
> packed in an object, i.e. whether two char fields
> take 4 or 8 bytes - but for arrays
> I believe they are well packed.

What does it mean by packed?

> > eg .
> > int [] data = new int[100];
>
> 12 + 100*4 = 412
>

> > String test = "this is a test";
>
> String(14) = object(char[], int, int, int: 8 + 4*4) +
> char[14](12+14*2) = 64

I do not understand the above equation.

once we have 32 bit allocation estimation equations figured out, what do we have to change in each equation for the 64 bit JVM estimation?

Sorry about these questions. I find thinking in bits a bit confusing, so please put up with me;)

Thanks in advance,
-Jer A.

jeremygwa
Offline
Joined: 2006-01-17

memst,

"On a 32bit Sun Hotspot VM, each object takes an 8 byte header + fields.
Arrays take 12 byte header + contents. I am not positive how well fields are
packed in an object, i.e. whether two char fields take 4 or 8 bytes - but for arrays
I believe they are well packed."

What do you mean by "packed'?

also, if lets say:
float f = 1.53f;

a book says that float takes 4 bytes storage. so does this mean, a float of *any* value would always take 4 bytes?

mernst
Offline
Joined: 2005-02-19

> by looking at a peice of code, can I predict or
> calculate, how much memory may be allocated

On a 32bit Sun Hotspot VM, each object takes an 8 byte header + fields.
Arrays take 12 byte header + contents. I am not positive how well fields are
packed in an object, i.e. whether two char fields take 4 or 8 bytes - but for arrays
I believe they are well packed.

> eg .
> int [] data = new int[100];

12 + 100*4 = 412

> String test = "this is a test";

String(14) = object(char[], int, int, int: 8 + 4*4) + char[14](12+14*2) = 64

> boolean T = false;

not on heap.

> char [] c = new char[10];
12 + 10*2 = 32

linuxhippy
Offline
Joined: 2004-01-07

>> boolean T = false;
> not on heap.

Why should this boolean not be allocated on heap? As far as I know its allocated on heap and uses 1-byte + alligning troubles it causes.

lg Clemens

steved
Offline
Joined: 2005-11-30

> >> boolean T = false;
> > not on heap.
>
> Why should this boolean not be allocated on heap? As
> far as I know its allocated on heap and uses 1-byte +
> alligning troubles it causes.
>
> lg Clemens

Assuming that the variables being assigned to in the examples in the original question were
local variables, then in the statement:

boolean T = false;

ineither the variable nor the value assigned are allocated on the stack. If this were changed to:

Boolean T = new Boolean(false);

then the value assigned is allocated on the stack. The Boolean object would
be 8 bytes on a 32-bit VM and 24 bytes on a 64-bit VM (objects are rounded
to a multiple of 8 bytes.)

Steve Dever

jeremygwa
Offline
Joined: 2006-01-17

Steve,

so if i use a 64-bit vm, how would the memory of primitive types, be calculated.

eg .
int [] data = new int[100];
String test = "this is a test";
boolean T = false;
char [] c = new char[10];
... etc

thanks in advance for your help.