tags:

views:

88

answers:

3

Hi,

Using Java (1.6) is it better to call the clear() method on a List or just re-instantiate the reference?

I have an ArrayList that is filled with an unknown number of Objects and periodically "flushed" - where the Objects are processed and the List is cleared. Once flushed the List is filled up again. The flush happens at a random time. The number within the List can potentially be small (10s of Objects) or large (millions of objects).

So is it better to have the "flush" call clear() or new ArrayList() ?

Is it even worth worrying about this sort of issues or should I let the VM worry about it? How could I go about looking at the memory footprint of Java to work this sort of thing out for myself?

Any help greatly appreciated.

+11  A: 

The main thing to be concerned about is what other code might have a reference to the list. If the existing list is visible elsewhere, do you want that code to see a cleared list, or keep the existing one?

If nothing else can see the list, I'd probably just clear it - but not for performance reasons; just because the way you've described the operation sounds more like clearing than "create a new list".

The ArrayList<T> docs don't specify what happens to the underlying data structures, but looking at the 1.7 implementation in Eclipse, it looks like you should probably call trimToSize() after clear() - otherwise you could still have a list backed by a large array of null references. (Maybe that isn't an issue for you, of course... maybe that's more efficient than having to copy the array as the size builds up again. You'll know more about this than we do.)

(Of course creating a new list doesn't require the old list to set all the array elements to null... but I doubt that that will be significant in most cases.)

Jon Skeet
Visibility not an issue. I like your reasoning for the clear method, since, it helps describe what is going on. I'd never seen trimToSize() before - I'll check that out
Phil
Also, did you mean the "1.7 implementation"? Surely Eclipse doesn't support Java 1.7 (since it's not out yet) or have I misinterpreted.
Phil
@Phil: I'm using build 1.7.0-ea-b78. It's the only JVM on this system. Even though it hasn't been released, there have been builds out for it for ages, and Eclipse runs on it with no problems that I've seen.
Jon Skeet
+1  A: 

I think if the Arraylist is to be too frequently flushed,like if it's run continuously in loop or something then better use clear if the flushing is not too frequent then you may create a new instance.Also since you say that elements may vary from 10 object to millions you can probably go for an in-between size for each new Arraylist your creating so that the arraylist can avoid resizing a lot of time.

Emil
A: 

The way you are using it looks very much like how a Queue is used. When you work of the items on the queue they are removed when you treat them.

Using one of the Queue classes might make the code more elegant.

There are also variants which handle concurrent updates in a predictable way.

Peter Tillemans