Solved

Java Memory

Posted on 2004-04-08
7
217 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:mayankeagle
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:mayankeagle
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now