Running GC.gc() multiple times


@ararslan said, about freeing memory:

Try running gc() multiple times.

It matches my experience that gc() needs to be called more than once. That was also true in SBCL. But why? Isn’t it a mark-and-sweep GC? All accessible objects are kept, all others are reclaimed?




However, it turns out that gc() only triggers a single sweep, meaning that young objects don’t get freed (just marked, I’m guessing). Doing multiple sweeps every time we manually trigger GC does a much better job of freeing stale objects leftover from previous benchmarks.

That’s interesting. Why would it mark, but not sweep? Isn’t the marking invalidated after gc() returns, since live/dead status of objects can change immediately by changing a reference?


That’s wrong. The only reason I can think of that requires running GC.gc() multiple times is to free objects both finalizers.