xdevel2000
xdevel2000

Reputation: 21444

java memory usage

I know I always post a similar question about array memory usage but now I want post the question more specific.

After I read this article: http://www.javamex.com/tutorials/memory/object_memory_usage.shtml

I didn't understand some things:

Upvotes: 2

Views: 585

Answers (2)

Stephen C
Stephen C

Reputation: 719426

An int will always be 32 bits. However, the JVM spec does not mandate that the fields of an object are stored contiguously in memory. So it is possible that a 64 bit JVM might align int fields on 64 bit boundaries. (Certainly, Sun 32 bit JVM's align 8 and 16 bit fields on 32 bit boundaries!)

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503200

Firstly, yes, an int will always be 32 bits, as per the language specification.

You shouldn't (IMO) include the reference itself in memory usage for the class itself, because it's not part of the object. In particular, you don't know how many places there will be references to the same object: if 10 different objects each store a reference to your object, you'll end up paying the reference cost 10 times. However, you should take account of the reference when computing the cost of whatever is storing it - so if you have a class with a field which is a reference, then count the cost there instead. (Likewise if you're computing stack space, consider local variables.)

Upvotes: 1

Related Questions