Solved

Java Memory

Posted on 2004-04-08
7
220 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:chandu33
7 Comments
 

Accepted Solution

by:
eching21 earned 50 total points
ID: 10785750
Hi C,

Yes it does.
Java will allocated x number of buckets for your HashMap as specified in the parameter to the constructor.

Hope this helps!
Eugene.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10789550
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.
0
 
LVL 1

Expert Comment

by:sheltonn
ID: 10798920
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.
0
 

Author Comment

by:chandu33
ID: 11052794
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11056192
>> 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.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question