2

I was looking into ArrayList.clear() & found that it doesn't free the memory instead made that list null. Why do you want to wait for GC to come & to free that memory (It could have done in clear method itself) when you can tell compiler manually that this particular ArrayList is no longer required.!!

Why do you want to give extra headache to GC Unnecessarily.

I know that System.gc() impacts the application performance since it runs garbage collection for the whole application.

Is there any way out to collect this particular ArrayList garbage instead of waiting GC to come to rescue in case of my server is having limited memory & list sizes are too too big.

Edit:- This problem arises because I have initialized a ArrayList(500000) & lets assume my object size is around 1000bytes due to some pre-initalized variables but due to some exception in forming first object itself I need to return from that function with freeing that memory which I have allocated to that ArrayList.I want my application to consume as low as memory it can. GC is the last resort for me.

cahen
  • 15,807
  • 13
  • 47
  • 78
  • http://stackoverflow.com/a/1567996/4028085 – brso05 Nov 20 '15 at 14:37
  • Possible duplicate of [How to free memory in Java?](http://stackoverflow.com/questions/1567979/how-to-free-memory-in-java) – brso05 Nov 20 '15 at 14:37
  • 1
    The memory in `Java` is managed for you by the system...If it runs out of memory it will first try to reclaim any memory that it can by doing a garbage collection. By de-referencing the object (making it `null`) u are telling the `gc` it is ok to reclaim that memory used by the de-referenced object. – brso05 Nov 20 '15 at 14:40
  • 1
    The is the java memory model. All memory is freed by the garbage collector. Also; what if the items in the array list are used elsewhere, do you want them freed? The array itself could also be used elsewhere (although you might have to use reflection to get hold of it) – Richard Tingle Nov 20 '15 at 14:40
  • @RichardTingle,I am pretty sure that,that list won't be used anywhere else. btw I edited my question. – NameNotFoundException Nov 20 '15 at 14:48
  • @NamingException The items in the array are very likely to be used elsewhere. It is possible (if highly unlikely) that the array will be used elsewhere. The list isn't a question, it isn't even eligible to garbage collection let alone being manually freed – Richard Tingle Nov 20 '15 at 15:10
  • The memory allocated to the ArrayList has nothing to do with the size of the objects you are going to put in. Each slot in the ArrayList just holds a reference, and each reference needs [between 4 and 12 bytes](http://stackoverflow.com/a/11564453/2626593). – abl Nov 20 '15 at 16:20
  • 1
    The garbage collector is the _only_ way to free memory in Java. But frankly, it's so fast you shouldn't care. – Louis Wasserman Nov 20 '15 at 17:17

2 Answers2

4

Why do you want to wait for GC to come & to free that memory (It could have done in clear method itself)

No, it could not. The ArrayList indeed won't be referencing the objects anymore, but the clear() method doesn't know if they are still being referenced somewhere else, so that's why this is a job for the Garbage Collector.

cahen
  • 15,807
  • 13
  • 47
  • 78
2

Why do you want to wait for GC to come & to free that memory (It could have done in clear method itself)

There's a big misunderstanding in the sentence above: all memory is freed by GC in Java, there is no way to manually free memory occupied by objects on the heap.

But to answer the question of why clear() nulls out all the elements rather than just throwing away the old backing array, the simple answer is "because it was written that way".

The slightly longer answer is that you typically use clear() before refilling the list with new elements, so throwing away the old array and allocating a new one (which then might need to be re-allocated several times to reach the same size as the earlier backing array) was seen as too wasteful.

If on the other hand you want to get rid of an ArrayList for good, you don't need to call clear() on it, you just null out any references to the list itself.

biziclop
  • 48,926
  • 12
  • 77
  • 104