• 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

memory issue

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a code like



Question: Assume everytime the "getMyList[i, arg]" method returns a very big list of Strings so it takes memory, when the code executes the 'for" loop, if the "myList" created in previous iteration still kept in memory ? for example , when i=0, it creates a "myList" and occupies memory, when it goes to i=1, is that previous "myList" created when i=0 still in memory ?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ignoring the syntax errors here, and just answering the question: if those big lists aren't being referenced anywhere else -- i.e., if the variable myList in the loop is the only variable pointing to one of these lists -- then each time around the loop, the only reference to the previous iteration's list is lost, meaning it becomes eligible for garbage collection. When the JVM needs to reclaim some memory, they will be deleted.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, yes this "myList" variable is not referred anywhere else.

It's true that "myList" is available for garbage collect after each iteration. But there is no guarantee it will be collected immediately. And it is likely it will stay in memory for a while, right ? If that's the case then this code can eat up memory quickly. So I think in some book they recommend to set it to null whenever it is not used any more. Is it true for this case ?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing really eats up memory -- when the JVM needs the memory, it's available. Garbage collection is just bookkeeping -- figuring out which parts of memory are actually free.

Anyway, no, explicitly setting things to null by itself does absolutely nothing. What does matter is that if you have a reference to a large object you no longer need, but that reference would otherwise stay in scope for a long time, then it makes sense to set the reference to null, thus removing the reference to the large object so it can be collected. Setting the variable to null doesn't send a signal or anything -- it doesn't make the collection happen any faster. In this loop, the reference disappears almost immediately, so there's no problem with unused references hanging around.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks. Want to clarify a fundamental question ---

when i=0, var "myList" is created in memory and points to a list of objects; when i=1, does JVM create ANOTHER var "myList" which points to a new list of objects ? or is the previous variable "myList" (created when i=0) altered to point to the new list of objects ?

if it is the second case, then I think what you said makes good sense. Please help.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each time around the loop a NEW'myList' variable is created. The scope of myList is bounded by the loop. This list, the way you have it written, will only ever contain a single reference to a an already existing object of type List<String>, it does not contain a copy of the returned list. As the iteration of the loop ends, just before incrementing 'i', the myList variable is marked as available for garbage collection. This has NO effect on the storage as accessed by getMyList(i,arg). If you have memory issues, myList itself is not the culprit, the storage as supplied by getMyList(i,arg) would be the place to start looking for excessive memory allocation.

P.S. You haven't told us what is inside getMyList(). If getMyList() creates a new list and returns that, well, in that case, when myList is garbage collected then all the storage used by the new list will also be collected as long as the content is not referenced anywhere else.
 
That is a really big piece of pie for such a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic