• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to make sure an array is deallocated ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have the following code that works fine and I am trying to see if it is optimized as lobj is going to be pretty large :

As per my comment in the code states, I am not sure if the garbage collector will not free the memory if it considers that there is still a reference on each temp.get(c - 1) within lobj. That's why I am wondering if using temp.get(c - 1).doubleValue() wouldn't get rid of the issue (if there is one).
Thanks for your Help,
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you use the last temp ArrayList created outside of that loop?
If not, then why not simply declare it inside the loop:

?
 
jacques dusieur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

?


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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


I was too quick to write, indeed it is not the same.
I was also not thinking of adding a call to the GC as indeed, the GC does it certainly better than me.
But I know that the GC releases the objects that are not referenced and my question is : do I leave a reference to temp.get(c - 1) when I set lobj.get(c).set(a, temp.get(c - 1) ? 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.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



It'll save you nothing at all.
Indeed, if the last ArrayList is not used, all it achieves is leaving an object on the heap that might be large (I have no idea) that is not any use.

And from a code point of view, it makes it look like that last ArrayList created is important, when it apparently isn't.

Generally, it's not worth trying to second guess the compiler, or JVM.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


You're overthinking this by a country mile. Jesper's absolutely right: 99% of the time you DON'T need to help the gc do it's job - create objects; use 'em; let 'em go out of scope. It really is as simple as that.

And on that point, there IS one change you could make to your code:Now temp can't exist outside your loop, so you know that it will be gc'd (or at least eligible for it) as soon as the loop exits.
Not that it would have made much of a difference because it will be eligible as soon as the method exits anyway.

But if you really want to help the gc: define variables ONLY where they're used.

HIH

Winston
 
jacques dusieur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. That's 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, so that's why I am trying to free all the memory possible asap and cannot wait to exit the method otherwise it might not even complete and I can get a message that I am out of memory when I will launch it with real data. But if you confirm that using temp.get(c - 1) as an argument to ArrayList.set(,) won't "stick it", then I am fine with the code indeed. And I will look into the remaining. Thank you.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

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.

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,

Well that sounds wrong right there.

Are you honestly saying that every single byte in that 4Gb "blob"
(a) might be used in the calculation
(b) needs to be randomly accessible
Because that's basically what you're doing by ploughing the whole thing into memory.

Programs (and memory) aren't databases, and you shouldn't treat them as such. Indeed rendering your 4Gb into a database may well be a far better solution, since that's the sort of thing they were designed for.

Winston
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your time might be better spent designing an algorithm that doesn't require you to load the full 4GB file. Processing it a piece at a time might do wonders...

Granted, that is not always possible. But it is usually possible, and is almost always a better solution.
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic