Dave Tolls wrote:Do you use the last temp ArrayList created outside of that loop?
If not, then why not simply declare it inside the loop:
Jesper de Jong wrote:You are talking about an array, but your code isn't using arrays anywhere. An ArrayList is not the same thing as an array.
When you are programming in Java you almost never have to worry about deallocating objects. Java has automatic memory management, which means that if an object gets out of reach of any live threads in your application, the garbage collector will automatically deallocate it when necessary. You do not need to do anything in your code to deallocate objects.
Sometimes programmers think they can help the garbage collector by setting things to null or by calling System.gc(). In reality, such things are rarely really useful and can even hurt the performance of your application.
So, don't worry and let the garbage collector do its work - it generally knows best how to manage memory.
In your code, using temp.get(c - 1).doubleValue() instead of temp.get(c - 1) will not make any difference at all with regard to deallocation of objects. Each time the loop runs, you allocate a new ArrayList and the one from the previous iteration of the loop will be automatically cleaned up by the garbage collector when necessary.
jacques dusieur wrote:
I thought it might save some execution time to declare it once only and then only do the new definition on each loop.
jacques dusieur wrote:Because indeed, when I get out of each loop, temp is redefined but that doesn't mean it is going to be released by the GC if its elements are still referenced, like I am afraid it might be when I call set method the way I do. Or does the set method makes its own copy of temp.get(c - 1), in which case indeed, temp won't be referenced and automatically freed by the GC.
In some languages: yes. In Java: very rarely. And I've seen far more cases where it was screwed up by someone trying to force the gc to work the way they want. Just program naturally.
jacques dusieur wrote:Well, I have rightly been pointing out in the past in Java (but applies to any language of course) that just by defining some objects slightly differently, it could have a major impact on memory/performances.
Well that sounds wrong right there.
the reason of my concern because my program runs on a 8GB machine and reads a 4GB file, loads its content in lobj and then perform calculations with it,