HashMap.clear()

Discussion in 'Java' started by koluwa, Feb 23, 2004.

  1. koluwa

    koluwa Guest

    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
    koluwa, Feb 23, 2004
    #1
    1. Advertising

  2. koluwa

    Oscar kind Guest

    koluwa <> wrote:
    > 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

    --
    Oscar Kind http://home.hccnet.nl/okind/
    Java/J2EE Developer email available on website
    Oscar kind, Feb 23, 2004
    #2
    1. Advertising

  3. koluwa

    Doug Pardee Guest

    Oscar kind <> wrote:
    > 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)
    Doug Pardee, Feb 23, 2004
    #3
  4. koluwa

    koluwa Guest

    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

    ***********************

    "Oscar kind" <> wrote in message
    news:4039e2d1$0$133$...
    > koluwa <> wrote:
    > > 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
    >
    > --
    > Oscar Kind http://home.hccnet.nl/okind/
    > Java/J2EE Developer email available on website
    koluwa, Feb 23, 2004
    #4
  5. koluwa wrote:


    >
    > "Oscar kind" <> wrote in
    > message news:4039e2d1$0$133$...
    >> koluwa <> wrote:
    >> > 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.
    >>

    > 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.

    --
    Kind regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Feb 23, 2004
    #5
  6. koluwa

    Jim Guest

    On Mon, 23 Feb 2004 10:52:19 +0100, "koluwa" <>
    wrote:

    >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
    Jim, Feb 24, 2004
    #6
  7. koluwa

    Chris Smith Guest

    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...


    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
    Chris Smith, Feb 27, 2004
    #7
  8. 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.


    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.
    Michael Borgwardt, Feb 27, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sanjay Kumar

    HashMap

    Sanjay Kumar, Jul 4, 2003, in forum: Java
    Replies:
    2
    Views:
    3,488
    Sanjay Kumar
    Jul 5, 2003
  2. Vince Darley
    Replies:
    4
    Views:
    4,420
    emilchacko
    Mar 2, 2010
  3. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,019
    Mark Fitzpatrick
    Jan 31, 2008
  4. Rakesh
    Replies:
    10
    Views:
    12,175
    Mike Schilling
    Apr 8, 2008
  5. InvalidLastName

    Unrecognized element 'add' after <clear></clear>

    InvalidLastName, Feb 26, 2007, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    957
    Steven Cheng[MSFT]
    Mar 6, 2007
Loading...

Share This Page