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: Marcin Borkowski
Subject: Re: When are unused overlays garbage collected?
Date: Wed, 26 May 2021 06:53:42 +0200
User-agent: mu4e 1.1.0; emacs 28.0.50

On 2021-05-24, at 14:27, Eli Zaretskii <eliz@gnu.org> wrote:

>> From: Marcin Borkowski <mbork@mbork.pl>
>> Date: Mon, 24 May 2021 07:00:54 +0200
>> 
>> --8<---------------cut here---------------start------------->8---
>> The overlay continues to exist as a Lisp object, and its property list
>> is unchanged, but it ceases to be attached to the buffer it belonged to,
>> and ceases to have any effect on display.
>> 
>> A deleted overlay is not permanently disconnected.  You can give it
>> a position in a buffer again by calling ‘move-overlay’.
>> --8<---------------cut here---------------end--------------->8---
>> 
>> So I assume that if I `delete-overlay', it means it cannot be
>> necessarily garbage-collected yet.
>
> Yes, it _can_ be GCed right away, at least in principle.  But Emacs
> will not necessarily trigger GC right away, so the overlay will not
> necessarily be collected immediately, even if it isn't references from
> any other variable or structure anymore.

I realize that GC might not trigger immediately, of course.

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.

>> 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?

>> This would make sense, because even if there is no variable bound to
>> an overlay which is not deleted, you can still get a reference to it
>> using any of the overlay-finding functions (`overlays-at' or
>> `overlays-in').
>
> No, overlays-in and overlays-at will not find a deleted overlay.
> These functions walk the buffer's overlay list, and a deleted overlay
> is unlinked from that list.

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?

> Does that answer your questions?  (I'm not sure what is exactly your
> question, because you didn't describe the context in which this issue
> bothered you.)

Well, this is one of the many questions that I wanted to have answered
for myself while writing my Emacs Lisp book (which I'm not sure I'm
allowed to talk about here) - just so that I have a better understanding
of overlays (note that I've been using them in my code for quite some
time - it turns out that only after you try to explain something, you
start to understand it better/appreciate it more/realize you didn't
understand it really well...).

Best,

-- 
Marcin Borkowski
http://mbork.pl



reply via email to

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