HashMap.clear()

K

koluwa

Hi all,

Does the HashMap.clear() method deletes all the existing content? Can I
reuse this object by clearing out the HashMap content in a loop? So I can
eliminate object creation. Or is the clear() process is expensive to be used
in a loop?

thanks...
koluwa
 
O

Oscar kind

koluwa said:
Does the HashMap.clear() method deletes all the existing content? Can I
reuse this object by clearing out the HashMap content in a loop? So I can
eliminate object creation. Or is the clear() process is expensive to be used
in a loop?

The HashMap.clear() method clears the map, so you can reuse the object.
Personally, I prefer it to creating a new HashMap. Especially in loops.
And unless the implementation of HashMap.clear() is extremely inefficient,
I believe it's cheaper to run HashMap.clear() than to cleanup the HashMap
objects with the garbage collector.


Oscar
 
D

Doug Pardee

Oscar kind said:
And unless the implementation of HashMap.clear() is extremely inefficient,
I believe it's cheaper to run HashMap.clear() than to cleanup the HashMap
objects with the garbage collector.

By what logic does "tramp through the entire HashMap setting things to
null, then let the garbage collector clean up" seem more efficient
than "let the garbage collector clean up"?

Trying to outguess the Java runtime environment is risky. Different
JVMs behave differently. That kind of "optimization" is something that
should be done only if you find that you need it, and only if you try
it both ways and see which way works better on YOUR JVM.

For most Sun JVMs, the most efficient practice is to discard objects
as soon as you no longer need them. This will keep them from having to
be copied into a survivor space, or worse yet, promoted to the tenured
generation. Allocating objects when you need them again is dirt-cheap.

One of the more recent articles discussing this can be found at
http://www-106.ibm.com/developerworks/library/j-jtp01274.html

Some quotes from that article:

"Sun estimates allocation costs at approximately ten machine
instructions."
"The JIT compiler can perform additional optimizations that can
reduce the cost of object allocation to zero."
"The cost of a minor garbage collection is proportional to the
number of LIVE objects in the young generation" (emphasis added)
"many clever tricks were developed to reduce these costs, such as
object pooling and nulling. Unfortunately, in many cases these
techniques can do more harm than good to your program's performance."
"Walking the list takes CPU cycles and will have the effect of
visiting all those dead objects and pulling them into the cache --
work that the garbage collector might be able to avoid entirely,
because copying collectors do not visit dead objects at all."
"For most applications, explicit nulling, object pooling, and
explicit garbage collection will harm the throughput of your
application, not improve it"
"Before you muck up your program's design to improve its
performance, first make sure you HAVE a performance problem and that
following the advice will SOLVE that problem." (emphasis added)
 
K

koluwa

My HashMap size would vary from 4-6 items. However the HashMap is used
inside a loop which could result in nearly 2000 iterations. So I need the
HashMap to be reused. Wondering whether to use the clear() method instead of
creating new Objects over and over again...

koluwa

***********************
 
C

Christophe Vanfleteren

koluwa wrote:

My HashMap size would vary from 4-6 items. However the HashMap is used
inside a loop which could result in nearly 2000 iterations. So I need the
HashMap to be reused. Wondering whether to use the clear() method instead
of creating new Objects over and over again...

Why don't you try both, and see what performs best?
You say you *need* the Map to be reused, but unless you've run a benchmark,
hwo can you be sure you *need* to reuse it. And if you've allready tested
it, you know the answer, don't you.
 
J

Jim

Hi all,

Does the HashMap.clear() method deletes all the existing content? Can I
reuse this object by clearing out the HashMap content in a loop? So I can
eliminate object creation. Or is the clear() process is expensive to be used
in a loop?

thanks...
koluwa
Be careful! Certain classes may need to be explicitly "closed" before
you cast them loose.

Jim
 
C

Chris Smith

koluwa said:
My HashMap size would vary from 4-6 items. However the HashMap is used
inside a loop which could result in nearly 2000 iterations. So I need the
HashMap to be reused. Wondering whether to use the clear() method instead of
creating new Objects over and over again...

It depends. I'd try it both ways. On the plus side, you have fewer
object allocations. On the minus side, you're artificially causing that
HashMap to last longer than it otherwise would, which might cause it to
be moved into an older generation where it takes many, many times the
effort to garbage collect it.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Michael Borgwardt

koluwa said:
My HashMap size would vary from 4-6 items. However the HashMap is used
inside a loop which could result in nearly 2000 iterations.

In that that case the HashMap itself is an unnecessary overhead, since
just keeping the contents in an array and scanning through the entire
array is much faster than looking something up in a HashMap for such
a ridiculously small number of elements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top