Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Memory

Posted on 2004-04-08
7
Medium Priority
?
230 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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?
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

705 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