Link to home
Start Free TrialLog in
Avatar of SeaCosmos
SeaCosmos

asked on

memory usage

who knows the memory usage of a reference whose value is set to null?
and the usage of the primitive type such as int?
Avatar of SeaCosmos
SeaCosmos

ASKER

memory usage
When reference is set null, that object will be garbage collected by garbage collector whenever it runs and based on the algorithm implemented for garbage collection.

So until object is not garbage collected, it would have its allocation in the memory(heap), though reference has been set to null.

int takes 32 bit in memory, it is used for integer values.
Primitive types such as int are 4 bytes apiece, I believe; long and double are 8 bytes each.

Yeah, byte also takes up 4 bytes.  ^^

Also, I believe java takes up memory in 8 byte blocks, so I'm not exactly clear on how the 4 byte usage of most primitives work in that situation . . .

References to nulls only takes up the memory space of the pointer; which I think is eight bytes.  It's been a while since I took that class.
may be this article will help you
http://java.about.com/library/tutorials/bltut-009d.htm
also you can find about non integral types
http://java.about.com/library/tutorials/bltut-009b.htm

best of luck
so it means that if object is not garbage collected,one
reference would have taken up 8 bytes space in the memory though it was set to null?the memory usage equals to two int primitives...
when i created a 100000 members String array,though i don't new the object of String,it can still take up nearly 1M bytes memory space.
so it means that if object is not garbage collected,one
reference would have taken up 8 bytes space in the memory though it was set to null?the memory usage equals to two int primitives...
when i created a 100000 members String array,though i don't new the object of String,it can still take up nearly 1M bytes memory space.
If you define an array of String objects, of size 100000, Java will allocate you enough memory for 100,000 Strings as soon as you define that array - even if you don't instantiate them.
ASKER CERTIFIED SOLUTION
Avatar of allahabad
allahabad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks to allahabad,ksj786 and Rezantis.^^

thanks for the detailed explanation.

This reference takes 1 bytes( i am may be correct), but depends on JVM implementation.

The reference holds the address of the object,so its memory taken depends on JVM implementation.I think it at least takes 1 byte.and if it is like Rezantis said that java takes up memory in 8 byte blocks,the reference set to null will take at least 8 bytes.

at last may i ask u some questions of English grammar?(i m not from united stats.)

to allahabad:
"So until object is not garbage collected, it would have its allocation in the memory(heap), though reference has been set to null." here,why did you use "until object is not garbage collected" but not "until object is garbage collected"?

to Rezantis:
"It's been a while since I took that class."
what did u mean in that sentence?

Yes, my mistake on grammar , line has to be "until object is garbage collected".
SeaCosmos, Thanks for points.
you are welcome.^^
wish to be good friends.
I was saying that it's been a long time since I took that class at university.  A class where I was taught by a lecturer, not a java class.  ^^;
thanks a lot.

i graduated from university two years ago and started to do some java programming.From then on,the first impression to me , when i read the word of class, is that it must mean java class but not the class which students take.^^