• 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

When objects are elligible for GC

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

In the following example:



I was expecting that once process() is finished, all the objects that are local to it are elligible for collection. (And will be collected when memory is needed. And that is not necessarly at the end of the for loop).

But logging how much memory is used at the beginning and end of each loop iteration. I see No memory is freed.
Anyone has an idea why GC is not kicking in ?
 
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are no objects created inside that loop. Every object referred to by the variable "sample" already existed in the collection referred to by the variable "samples".

Actually it's possible that the "process" method creates objects which could be garbage collected, but as you correctly mentioned, there is no reason why garbage collection should run immediately after the loop.
 
Saloon Keeper
Posts: 27884
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Modern Java VMs do not run GC as a unit. The GC process is handled incrementally according to its own rules.

The old idea that GC could be a major task in itself got a glaring counter-demonstration on one of the demo apps for the Commodore Amiga. It ran 4 animation windows and played music (written in Microsoft BASIC). Every time GC kicked in, the music would pause.
 
T Allay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:There are no objects created inside that loop. Every object referred to by the variable "sample" already existed in the collection referred to by the variable "samples".

Actually it's possible that the "process" method creates objects which could be garbage collected, but as you correctly mentioned, there is no reason why garbage collection should run immediately after the loop.



Indeed, process() creates objects. And these are not collected. Which leads to a memory leak considering samples is too large. And the objects created in the map are also large
 
Marshal
Posts: 79471
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

T Allay wrote:. . . Indeed, process() creates objects.

You didn't show any such objects being created. We can only comment on what we can see, not what we don't see.

And these are not collected. . . .

What makes you think that? You haven't shown any evidence for that assertion.
 
Campbell Ritchie
Marshal
Posts: 79471
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

T Allay wrote:. . . the objects that are local to [process()] are elligible for collection.

Not if escape analysis allows those objects to be created on the stack.

. . . when memory is needed. . . .

You cannot expect a GC cycle (complete or partial) to run until free memory is needed. Your un‑referenced objects do not therefore constitute a memory leak. What's more, objects created on the stack don't show up as heap memory at all, so the logger won't identify them.
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The GC would kick in based on the JVM's/Java's perception of memory pressure.

You can get into trouble with JNI code when some teeny little Java handle is holding on to some enormous native object, because the JVM only perceives the Java side of things, I think.

In pure Java code it doesn't seem likely that you could consume enough heap space to be a problem -- if you did, then GC would kick in by then.

By memory leak did you suspect that process() method is somehow holding on to data intended to be transient or temporary because code is storing references to it somewhere unexpected?

We would indeed need to see more context if that was happening, tho a long (or even very long) delay between going out-of-scope and eventual GC doesn't prove anything by itself.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

T Allay wrote:
I was expecting that once process() is finished, all the objects that are local to it are elligible for collection.


Any new temporary objects allocated by process() will be elligible; however, collection is not tied to scope, as it is in C++ RAII idiom.

T Allay wrote:
(And will be collected when memory is needed. And that is not necessarly at the end of the for loop).



This is correct.

T Allay wrote:
But logging how much memory is used at the beginning and end of each loop iteration. I see No memory is freed.
Anyone has an idea why GC is not kicking in ?



In general you can't really predict when the GC will collect.

Edit: I will add that Java allows you to configure, measure, or even swap out GC's. Still, I don't think its possible to predict exactly when the GC should run, unless by explicitly calling System.gc(), and even then the results will be unpredictable. Quoting from the Java (16) specification:


There is no guarantee that this effort will recycle any particular number of unused objects, reclaim any particular amount of space, or complete at any particular time, if at all, before the method returns or ever.



Some (non-GC) languages, such as C++ and Rust, allow explicit control over memory usage, but GC languages such as Java, Python, or Scheme, generally do not.
 
Campbell Ritchie
Marshal
Posts: 79471
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damon McNeill wrote:. . . Any new temporary objects allocated by process() will be elligible . . .

I have already explained that isn't correct for objects with only local scope.

. . . I don't think its possible to predict exactly when the GC should run, unless by explicitly calling System.gc() . . .

Calling Runtime#gc() doesn't, as you have already said, allow any prediction of when a garbage collection round will occur.

Some (non-GC) languages, such as C++ and Rust, allow explicit control over memory usage, but GC languages such as Java, Python, or Scheme, generally do not.

The advantages of an automatic heap were well established as long ago as the earlier days of Eiffel. It's in Bertrand Meyer's Object‑Oriented Software Construction.
 
Author
Posts: 56
15
Scala IntelliJ IDE Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello T Allay,

Not trying to advertise my book here, but you can find the answer to your questions in my book Java 17 for Absolute Beginners, https://link.springer.com/book/10.1007/978-1-4842-7080-6, chapter 13, page 740. ;)

Cheers,
Iuliana Cosmina
 
Ranch Hand
Posts: 572
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is that objects are eligible for garbage collection when they go "out of scope" in the application . ie. no accessible variable has a reference to the object.
 
Campbell Ritchie
Marshal
Posts: 79471
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really need a whole chapter about garbage collection in a beginners' book? Would it suffice to say that objects no longer required are deleted from memory and leave it at that? Or, rather like Head First Java, which Carey mentioned in a different thread, are you giving lots of hints for certification candidates?
 
iuliana cosmina
Author
Posts: 56
15
Scala IntelliJ IDE Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Campbell,

Of course a full chapter is needed. There is more than one GC and they can also be configured for different purposes. Flags to determine which one is being used by the JVM and how it is configured are covered too. There is also a section covering GC related exceptions, what causes them, how to avoid them. There is a section about the difference between strong and weak references. There is a section about what finalizing an object means, why you should avoid using the finalize method and how to get a similar result sing the new Cleaner class.

A beginner should know more than just that the GC exists and does its job. Also, the chapter is not that big.

Cheers!
 
It's a tiny ad. At least, that's what she said.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic