help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: When are unused overlays garbage collected?


From: Eli Zaretskii
Subject: Re: When are unused overlays garbage collected?
Date: Wed, 26 May 2021 15:30:17 +0300

> From: Marcin Borkowski <mbork@mbork.pl>
> Cc: help-gnu-emacs@gnu.org
> Date: Wed, 26 May 2021 06:53:42 +0200
> 
> But what would happen if I (delete-overlay my-overlay), then it gets
> garbage collected, and then I (move-overlay my-overlay ...)?
> 
> I just performed a simple experiment - I manually created an overlay in
> a temporary buffer, bound a variable to it, deleted it, and called
> (garbage-collect) - and C-h v'ing that variable still said:
> 
> aqq’s value is #<overlay in no buffer>
> 
> so I assume it was /not/ GCed.

It wasn't GCed because it was referenced from a variable.

> >> My guess would be that if the overlay is "deleted" (so it is not
> >> attached to any buffer, either by means of `delete-overlay' or when its
> >> buffer is killed) /and/ it can't be referenced from Elisp (e.g., there
> >> is no variable bound to it).
> >
> > That's not entirely true.  An overlay (like any other Lisp object)
> > that was deleted will not be collected as long as some variable
> > _actually_references_ it.  That could be a Lisp variable or a C
> > variable not exposed to Lisp.  The difference between what I wrote and
> > what you wrote is that the reference must actually exist, not only be
> > possible.
> 
> I am not sure if I grasp that difference.  Can you provide an example of
> a situation when an object does not have an "actual reference" but still
> "can be referenced"?  Do you mean e.g. it being an element of a list
> bound to some variable?

No, that's not the essence of the difference.  You said, above, "it
can't be referenced from Lisp".  My point is that "can't" doesn't cut
it, because an object can be GCed even if it _can_ be referenced.
What matters is that it _is_not_ referenced, even though it _can_be_.

IOW, any object can potentially be referenced by some variable, but GC
only avoids recycling an object if some other object actually (not
potentially) references it.  The GC's "mark" phase scans all the live
Lisp objects and marks any other objects referenced by those live
objects, recursively.  Any marked object will not be recycled by the
"sweep" phase.

> Well, I meant something different - an overlay that is "live" in some
> buffer, but no variable is bound to it.  Such an overlay /can/ be
> referenced with `overlays-in' / `overlays-at', so obviously cannot be
> GCed, right?

No live overlay in a live buffer will ever be GCed, because when GC
marks live buffers, it also marks the overlays in that buffer by
walking the linked list of the buffer's overlays.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]