At Sat, 8 Aug 2020 03:32:57 -0400, George Neuner wrote:
>
> On 8/8/2020 1:55 AM, Sorawee Porncharoenwase wrote:
> > I even saw people doing `collect-garbage` three times, just to be safe
> > I guess. And yet theoretically it's not guaranteed that things will be
> > claimed back properly.
> >
> > Honestly, there should be a function that does this `collect-garbage`
> > until fixpoint or something, so that we don't need to perform this ...
> > uh .... ritual.
>
> There may be no documented guarantee, but I *think* the implementation
> assures that 2 collections, back-to-back, are sufficient to reclaim all
> objects that were garbage at the beginning of the 1st collection. At
> least for BC Racket.
In the absence of finalization, then a single `collect-garbage` would
always be sufficient, and a second `collect-garage` would have no
effect.
For the specific benchmark in this thread as run in plain `racket`, I
think a single `collect-garbage` is sufficient, and that's what I
normally do.
But finalization complicates the picture --- especially finalization
via `register-finalizer`, since the finalizers run in a background
thread. Because of that background thread, in a context that uses a
library like `racket/gui`, I sometimes use repetitions of
(collect-garbage)
(sync (system-idle-evt))
It's difficult to know whether the libraries that you use rely on
finalization. Also, finalization means that there is no simple number
of `(collect-garbage)`s and `(sync (system-idle-evt))`s that are needed
to really collect everything that could potentially be collected;
finalization chains can require an arbitrary number of
`(collect-garbage)` cycles to clean up (so libraries should avoid
finalization chains!). The collector is precise and the compiler is
safe-for-space, so you can reason about the reachability of an
individual value, but reasoning precisely about overall memory use
across many libraries is difficult or impossible.
Using an extra `(collect-garbage)` is not ideal and often not
necessary, but it's a reasonable just-to-be-sure habit.
Matthew