Solved

Java Memory

Posted on 2004-04-08
7
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Starting up a Project

623 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