Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does this form of memory management make sense at all in Java?

In the life-cycle of my application, I have to re-create an ArrayList that contains other ArrayLists of objects (reading them from storage). The ArrayList is always assigned to the same data member in a class, essentially leaving the older ArrayList-of-ArrayLists dangling (unreference-able or inaccessible).

My understanding is that, unlike in C++, this doesn't result in a memory leak because the system knows when an object becomes unreference-able and frees the memory when/as it sees fit.

Still, old habits are hard to overcome and I am almost instinctively drawn to "help" the system by doing some cleanup myself before re-creating that ArrayList-of-ArrayLists:

for (InnerArray aList : outerArray)
  aList.clear();
outerArray.clear();

My questions:

  1. Is this redundant?
  2. Am I doing more harm than good?
  3. Is this good in any way?
  4. Why?
like image 373
ateiob Avatar asked Sep 01 '11 02:09

ateiob


2 Answers

It is redundant. You are not helping those objects get freed, because garbage collection will already determine them to be unreachable. You can't make them more unreachable than they already are. So, you waste a little bit of processing time on something that accomplishes nothing.

The main problem, though, is that you are confusing other developers who will be wondering why you're clearing that array. They may waste their time trying to figure out why you were doing that, if there's some hidden reason that they didn't see at first.

A similar problem that C++ developers tend to have with Java code is making finalizers; in Java you almost never want to do this, as finalizers add an additional cost to garbage collection and they aren't a reliable way to free resources because they may not get called for a long time, or ever.

like image 175
Nate C-K Avatar answered Sep 28 '22 18:09

Nate C-K


  1. yup - Honestly I just don't get it. if you don't need an object anymore just make sure that you cannot access it anymore and you're good!
  2. yup - invoking might use processing power, garbage collection would probably use less to get rid of an object. It might also be confusing as Nate suggested.
  3. nope - I don't see anything good, sorry.
  4. See above and welcome to Java! You can concentrate on the fun stuff now :)

P.S. I'm not sure about what I'm going to say now, but the clear() function might remove elements from an ArrayList just by making sure that you cannot access them anymore through the ArrayList variable. Probably it won't 'destroy' them but it will just make them eligible for garbage collection. It would be nice to know how that function is implemented...

P.P.S. You might be tempted to invoke GC yourself, don't do it! See related question.

EDIT:

Here is the clear() from ArrayList:

public void clear()
{
    if (size > 0)
    {
        modCount++;
        // Allow for garbage collection.
        Arrays.fill(data, 0, size, null);
        size = 0;
    }
}
like image 30
Marsellus Wallace Avatar answered Sep 28 '22 18:09

Marsellus Wallace