Link to home
Start Free TrialLog in
Avatar of chandu33
chandu33

asked on

Java Memory

HI Experts

Can anyone tell me if by doin

HashMap hm = new HashMap(1);

or HashMap hm = new HashMap(100000);

does the second constructor cause a memory bubble greater than the 1st one even though i have not filled in any elements in my Map.

Thanks
- C
ASKER CERTIFIED SOLUTION
Avatar of eching21
eching21

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
Avatar of Mayank S
Yes, of course it does, because you specify "100000" yourself ;-) Filling does not matter - it is still allocated.

However, you cannot force the JVM to do garbage collection. Even calls to methods like runFinalization () and gc () don't guarantee that garbage-collection will be performed when they are called.

If you're facing OutOfMemory errors, then you can increase the amount of memory that the JVM uses by specifying the -Xmx option while running the program.
Avatar of sheltonn
sheltonn

Yes it does.  The point of declaring the size is to make the filling of the elements more efficient.  If you do not declare the size, then it defaults (maybe 7 buckets).  If you are going to put 10K items in, then when you hit 8, everything will hold while a 16 element array is created, the 7 elements copied to the new array, the new element added.  Each time, the current size will double if you exceeds the bounds of the backing array.

With this in mind, declare the initial size to be the number of expected buckets.  Declaring a size larger than what you are expecting will only waste memory in the backing array.
Avatar of chandu33

ASKER

Looks like I have accepted the answer from eching21

I dont see accept button anymore....

I really apologize for my lethargic approach here to all the experts who helped me out!!!.


Thanks again everyone

- C
>> I have accepted the answer from eching21

You didn't do it ;-) the moderator did it forcibly.

>> I dont see accept button anymore

Because its already accepted.

>> I really apologize for my lethargic approach here to all the experts who helped me out

Make it a habit to close questions or to ask for a refund after you find a solution (be it from here or any other way). At least, do respond after a clean-up comment is posted on the page.