Clear Arraylist vs new ArrayList

Discussion in 'Java' started by Philipp, May 28, 2008.

  1. Philipp

    Philipp Guest

    Hello,
    What is the most efficient and preferred way to clear a List in terms of
    speed and memory?

    list = new ArrayList();
    or
    list.clear();

    clear() walks all elements and sets them to null, maybe this is not
    efficient for large lists?

    Phil
    Philipp, May 28, 2008
    #1
    1. Advertising

  2. Philipp wrote:
    > Hello,
    > What is the most efficient and preferred way to clear a List in terms of
    > speed and memory?


    This seems a premature optimization question.
    Google for these terms.

    Do you measured the needs to speed up you program and do you discovered
    that the problem is the array creation?

    >
    > list = new ArrayList();
    > or
    > list.clear();
    >
    > clear() walks all elements and sets them to null, maybe this is not
    > efficient for large lists?


    The best way is the more readable way.
    When you finish to create your program and after you measured that there
    are some bottleneck with a profiler you can solve them.

    >
    > Phil



    --
    Andrea Francia
    http://andreafrancia.blogspot.com/
    Andrea Francia, May 28, 2008
    #2
    1. Advertising

  3. Philipp

    Arne Vajhøj Guest

    Philipp wrote:
    > What is the most efficient and preferred way to clear a List in terms of
    > speed and memory?
    >
    > list = new ArrayList();
    > or
    > list.clear();
    >
    > clear() walks all elements and sets them to null, maybe this is not
    > efficient for large lists?


    Probably the new is slightly faster.

    But I am pretty sure that it does not matter for your app, so
    it is the wrong question.

    The correct question is: does creating a new list or removing
    the elements from the existing lists best model the real world
    your program is simulating ?

    Arne
    Arne Vajhøj, May 28, 2008
    #3
  4. Arne Vajhøj <> wrote:
    > Philipp wrote:
    >> What is the most efficient and preferred way to clear a List in terms of
    >> speed and memory?
    >> list = new ArrayList();
    >> or
    >> list.clear();


    >> clear() walks all elements and sets them to null,


    I'd rather think that the internal array that keeps
    the refs is dumped on .clear(), without any walk-through.

    > The correct question is: does creating a new list or removing
    > the elements from the existing lists best model the real world
    > your program is simulating ?


    E.g. if you pass (a reference to) an ArrayList to another method,
    then it makes a *lot* of difference, whether you .clear() the
    collection or whether you replace it with a new one within the
    called method.
    Andreas Leitgeb, May 28, 2008
    #4
  5. Andreas Leitgeb wrote:
    > Arne Vajhøj <> wrote:
    >
    >>Philipp wrote:
    >>
    >>>What is the most efficient and preferred way to clear a List in terms of
    >>>speed and memory?
    >>>list = new ArrayList();
    >>> or
    >>>list.clear();

    >
    >
    >>>clear() walks all elements and sets them to null,

    >
    >
    > I'd rather think that the internal array that keeps
    > the refs is dumped on .clear(), without any walk-through.


    I looked at the source code (JRE 1.5), and it does walk through the
    array. It might be more efficient to do it the way you say.

    That does create a significant difference between clear() and creating a
    new ArrayList. The new ArrayList will start with a small array, and
    build it up as needed for the elements added after the clearing. Calling
    clear leaves the array at the size it was immediately before.

    Using clear() might be the better choice if a lot of CPU time is going
    on copying in ArrayList as the list rebuilds after the clear. On the
    other hand, new might be the better choice if the ArrayList was very
    long, will have fewer elements in the future, and the program is using a
    lot of memory.

    However, there is an important functional difference. If any copies of
    the "list" variable escaped, the other holders of references will go on
    seeing the old ArrayList if new is used, but will see their list drop to
    zero elements after clear. I would code this however best expresses the
    intent, and be very careful about changing it for performance tuniing.

    Patricia
    Patricia Shanahan, May 28, 2008
    #5
  6. Philipp

    Roedy Green Guest

    On Wed, 28 May 2008 09:22:10 +0200, Philipp <>
    wrote, quoted or indirectly quoted someone who said :

    >What is the most efficient and preferred way to clear a List in terms of
    >speed and memory?
    >
    >list = new ArrayList();
    > or
    >list.clear();
    >
    >clear() walks all elements and sets them to null, maybe this is not
    >efficient for large lists?


    If you need a new array the same size or slightly smaller, clear would
    be preferable. If not, new.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, May 28, 2008
    #6
  7. Philipp

    Arne Vajhøj Guest

    Andreas Leitgeb wrote:
    > Arne Vajhøj <> wrote:
    >> Philipp wrote:
    >>> What is the most efficient and preferred way to clear a List in terms of
    >>> speed and memory?
    >>> list = new ArrayList();
    >>> or
    >>> list.clear();

    >
    >>> clear() walks all elements and sets them to null,

    >
    > I'd rather think that the internal array that keeps
    > the refs is dumped on .clear(), without any walk-through.


    It does loop and set to null.

    >> The correct question is: does creating a new list or removing
    >> the elements from the existing lists best model the real world
    >> your program is simulating ?

    >
    > E.g. if you pass (a reference to) an ArrayList to another method,
    > then it makes a *lot* of difference, whether you .clear() the
    > collection or whether you replace it with a new one within the
    > called method.


    Absolutely.

    Arne
    Arne Vajhøj, May 28, 2008
    #7
    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. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,722
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,313
    Kaidi
    Jan 3, 2004
  3. guess85
    Replies:
    2
    Views:
    354
    guess85
    Dec 20, 2007
  4. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    988
    Mark Fitzpatrick
    Jan 31, 2008
  5. InvalidLastName

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

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

Share This Page