• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Garbage being collected?

 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
S'posin' I've got an object with only one reference to it-> in a List. Then, say there is a method within the object which, among other things, removes itself from the list. When the method terminates, there will no longer be any references to that object. Will it be garbage collected, even though it removed itself from the list? Been readin' some random side notes about memory leak, and I certainly wouldn't want something like that to happen to my program.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joseph,

I'm not able to understand your scenario.....very sorry for that !!

If possible, please provide some code snippet to watch out here.

Thanks
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it will be garbage collected as it is no longer reachable by any thread. Who or what removed it from the list is immaterial.

- Peter
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding is that it is ELLIGIBLE for garbage collection. Whether or not it actually IS collected is another story.
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pedant
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aight, so I'm safe from this memory leak? I'm making thousands of particles in my graphics, and it wouldn't be good to have thousands of objects takin' up space.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether your application will run out of memory or not depends on whether garbage collection will run and how frequently(here the truck comes along once a week which is usually enough for me ).
Remember it's not guaranteed to run.
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeroen Wenting:
Remember it's not guaranteed to run.

A common misconception. It is guaranteed to run before an OutOfMemoryError is thrown, as the javadoc states that it is "thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector." (my emphasis). You cannot run out of memory simply through creating lots of object churn.

- Peter
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you CAN run out of memory by creating a load of objects, the garbage collector just makes it less likely and take longer.
If the objects are somehow not loosing references they will never be garbage collected (luckily the Java GC is smart enough to catch entire groups of objects with no outside references so that leak option has been plugged).

There is also the possibility that the garbage collector will fail to run if memory is used up so quickly that the JVM doesn't have enough resources left to run the GC in.

Or maybe the programmer thought to be smart and run a thread at maximum priority, leaving it at a scheduling priority higher than the GC itself thus preventing the GC from running before it's too late.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter den Haan:
Pedant



My wife does call me a "pedantic literalist"... how'd you know?
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeroen Wenting:
you CAN run out of memory by creating a load of objects, the garbage collector just makes it less likely and take longer.

What use is a bald assertion? Prove it. Produce a reference, show the entry in the bug database, provide demonstration code, and/or explain why the javadoc I quoted above does not imply that the garbage collector is runs before the error is thrown.

If the objects are somehow not loosing references they will never be garbage collected (luckily the Java GC is smart enough to catch entire groups of objects with no outside references so that leak option has been plugged).

Then they are not "no longer reachable by any thread", are they? I don't think anyone is unclear on this.

Object churn is not enough. Even creating moderately bulky objects at high thread priority in a tight loopIs not enough. Try it.

- Peter
[ July 28, 2004: Message edited by: Peter den Haan ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeroen Wenting:
There is also the possibility that the garbage collector will fail to run if memory is used up so quickly that the JVM doesn't have enough resources left to run the GC in.

Or maybe the programmer thought to be smart and run a thread at maximum priority, leaving it at a scheduling priority higher than the GC itself thus preventing the GC from running before it's too late.



Where do you get this from???

I don't think that the specification allows such behaviour; and it would be rather easy to prevent, so I would be surprised if there were a JVM in which the above could happen... uzzled:
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic