all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* When are unused overlays garbage collected?
@ 2021-05-24  5:00 Marcin Borkowski
  2021-05-24  5:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Marcin Borkowski @ 2021-05-24  5:00 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all,

the Elisp reference says that the `delete-overlay' function does not
entirely delete the overlay (in a sense):

--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.  So, when /can/ an overlay be really
"deleted", meaning the memory it occupied is freed?

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).  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').

Am I correct?

--
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-24  5:00 When are unused overlays garbage collected? Marcin Borkowski
@ 2021-05-24  5:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-24  6:33   ` Marcin Borkowski
  2021-05-24 12:27 ` Eli Zaretskii
  2021-05-24 17:07 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2 siblings, 1 reply; 19+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-05-24  5:05 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski wrote:

> So I assume that if I `delete-overlay', it means it cannot
> be necessarily garbage-collected yet. So, when /can/ an
> overlay be really "deleted", meaning the memory it occupied
> is freed?

(setq inhibit-default-init t)
(setq garbage-collection-messages nil)
(setq gc-cons-threshold 3000000)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-24  5:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-05-24  6:33   ` Marcin Borkowski
  0 siblings, 0 replies; 19+ messages in thread
From: Marcin Borkowski @ 2021-05-24  6:33 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


On 2021-05-24, at 07:05, Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> Marcin Borkowski wrote:
>
>> So I assume that if I `delete-overlay', it means it cannot
>> be necessarily garbage-collected yet. So, when /can/ an
>> overlay be really "deleted", meaning the memory it occupied
>> is freed?
>
> (setq inhibit-default-init t)
> (setq garbage-collection-messages nil)
> (setq gc-cons-threshold 3000000)

How is that supposed to help me?

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-24  5:00 When are unused overlays garbage collected? Marcin Borkowski
  2021-05-24  5:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-05-24 12:27 ` Eli Zaretskii
  2021-05-26  4:53   ` Marcin Borkowski
  2021-05-24 17:07 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2 siblings, 1 reply; 19+ messages in thread
From: Eli Zaretskii @ 2021-05-24 12:27 UTC (permalink / raw)
  To: help-gnu-emacs

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

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

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

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.)



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-24  5:00 When are unused overlays garbage collected? Marcin Borkowski
  2021-05-24  5:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-24 12:27 ` Eli Zaretskii
@ 2021-05-24 17:07 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-05-26  5:01   ` Marcin Borkowski
  2 siblings, 1 reply; 19+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-05-24 17:07 UTC (permalink / raw)
  To: help-gnu-emacs

> 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).  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').

Sounds right, yes.  `delete-overlay` just disconnects the overlay from
the buffer in which it was placed.  As long as an overlay is placed in
a buffer, then it is still reachable (via internal variables inside the
buffer, and via `overlays-at' or `overlays-in') and it can have visible
effects on screen.

Markers share some of their implementation with overlays, but because
markers do not affect the display and because there is nothing
corresponding to `overlays-at' or `overlays-in', Emacs goes through the
trouble of GC'ing those markers which are still reachable via the
buffer's internal variables (its list of markers) as long as they're not
reachable some other way, of course.

So it's OK to create markers are then forget about them, but it's not
OK to create overlays and then forget about them: you need to manually
`delete-overlay` when you're done with them otherwise they'll accumulate
in the buffer.


        Stefan




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-24 12:27 ` Eli Zaretskii
@ 2021-05-26  4:53   ` Marcin Borkowski
  2021-05-26  7:23     ` tomas
  2021-05-26 12:30     ` Eli Zaretskii
  0 siblings, 2 replies; 19+ messages in thread
From: Marcin Borkowski @ 2021-05-26  4:53 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs


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



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-24 17:07 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-05-26  5:01   ` Marcin Borkowski
  2021-05-26 12:34     ` Eli Zaretskii
  2021-05-26 14:48     ` Stefan Monnier
  0 siblings, 2 replies; 19+ messages in thread
From: Marcin Borkowski @ 2021-05-26  5:01 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2021-05-24, at 19:07, Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

>> 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).  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').
>
> Sounds right, yes.  `delete-overlay` just disconnects the overlay from
> the buffer in which it was placed.  As long as an overlay is placed in
> a buffer, then it is still reachable (via internal variables inside the
> buffer, and via `overlays-at' or `overlays-in') and it can have visible
> effects on screen.

Thanks!

> Markers share some of their implementation with overlays, but because
> markers do not affect the display and because there is nothing
> corresponding to `overlays-at' or `overlays-in', Emacs goes through the
> trouble of GC'ing those markers which are still reachable via the
> buffer's internal variables (its list of markers) as long as they're not
> reachable some other way, of course.

Thanks, too, this is pretty interesting.  I assume one possible reason
for not having `markers-in' (analogous to `overlays-in') is that an
overlay (even without any variable pointing to it, IOW, a "name" for it)
has some "semantics" (properties), so it can be "guessed" what it is
for; for markers, we would only know that "someone wanted to remember
this position, but we have no idea why/what for", so why even bother
keeping it?  (Technically, there is also the insertion type, but that
doesn't tell much, either.)

> So it's OK to create markers are then forget about them, but it's not
> OK to create overlays and then forget about them: you need to manually
> `delete-overlay` when you're done with them otherwise they'll accumulate
> in the buffer.

And that is probably the most important takeaway here - but it raises
another question.  If I create an overlay in some buffer, keep no
references to it, and then kill the buffer - is the overlay eligible for
GC?  (Again, I performed an experiment, and the results suggest that its
state is similar to what happen after `delete-overlay', so I assume
yes.)

TIA,

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-26  4:53   ` Marcin Borkowski
@ 2021-05-26  7:23     ` tomas
  2021-05-26 15:36       ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-05-26 12:30     ` Eli Zaretskii
  1 sibling, 1 reply; 19+ messages in thread
From: tomas @ 2021-05-26  7:23 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1082 bytes --]

On Wed, May 26, 2021 at 06:53:42AM +0200, Marcin Borkowski wrote:
> 
> 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’.

[...]

> But what would happen if I (delete-overlay my-overlay), then it gets
> garbage collected, and then I (move-overlay my-overlay ...)?
                                              ^^^^^^^^^^

You are still holding a reference to it. It would be an extremely
impolite action on part of the garbage collector to even look at
your overlay. I'm sure it would bring about a severe reprimand :)

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-26  4:53   ` Marcin Borkowski
  2021-05-26  7:23     ` tomas
@ 2021-05-26 12:30     ` Eli Zaretskii
  2021-05-27 16:20       ` Marcin Borkowski
  1 sibling, 1 reply; 19+ messages in thread
From: Eli Zaretskii @ 2021-05-26 12:30 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-26  5:01   ` Marcin Borkowski
@ 2021-05-26 12:34     ` Eli Zaretskii
  2021-05-27 16:23       ` Marcin Borkowski
  2021-05-26 14:48     ` Stefan Monnier
  1 sibling, 1 reply; 19+ messages in thread
From: Eli Zaretskii @ 2021-05-26 12:34 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Date: Wed, 26 May 2021 07:01:48 +0200
> Cc: help-gnu-emacs@gnu.org
> 
> > Markers share some of their implementation with overlays, but because
> > markers do not affect the display and because there is nothing
> > corresponding to `overlays-at' or `overlays-in', Emacs goes through the
> > trouble of GC'ing those markers which are still reachable via the
> > buffer's internal variables (its list of markers) as long as they're not
> > reachable some other way, of course.
> 
> Thanks, too, this is pretty interesting.  I assume one possible reason
> for not having `markers-in' (analogous to `overlays-in') is that an
> overlay (even without any variable pointing to it, IOW, a "name" for it)
> has some "semantics" (properties), so it can be "guessed" what it is
> for; for markers, we would only know that "someone wanted to remember
> this position, but we have no idea why/what for", so why even bother
> keeping it?

I think we don't have markers-in because it isn't needed.  overlays-in
is needed when you want to find the next or the previous overlay, or
when you need to know what faces contribute to the appearance of a
character at certain position.  There's no such need for markers.

> > So it's OK to create markers are then forget about them, but it's not
> > OK to create overlays and then forget about them: you need to manually
> > `delete-overlay` when you're done with them otherwise they'll accumulate
> > in the buffer.
> 
> And that is probably the most important takeaway here - but it raises
> another question.  If I create an overlay in some buffer, keep no
> references to it, and then kill the buffer - is the overlay eligible for
> GC?

Yes, killing a buffer deletes all of its overlays (and all of its
markers).



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-26  5:01   ` Marcin Borkowski
  2021-05-26 12:34     ` Eli Zaretskii
@ 2021-05-26 14:48     ` Stefan Monnier
  2021-05-27 16:26       ` Marcin Borkowski
  1 sibling, 1 reply; 19+ messages in thread
From: Stefan Monnier @ 2021-05-26 14:48 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs

> Thanks, too, this is pretty interesting.  I assume one possible reason
> for not having `markers-in' (analogous to `overlays-in') is that an
> overlay (even without any variable pointing to it, IOW, a "name" for it)
> has some "semantics" (properties), so it can be "guessed" what it is
> for; for markers, we would only know that "someone wanted to remember
> this position, but we have no idea why/what for", so why even bother
> keeping it?  (Technically, there is also the insertion type, but that
> doesn't tell much, either.)

There's been proposals to add a `markers-in` function or
something similar.  The main motivation was to better preserve some
marker's positions when replacing a chunk of text with another.

But the need is not very compelling, and there are some technical
issues, one of them being that internally overlays are contain two
markers, so this would risk exposing those markers, making it possible
for an overlay to have a start and an end in two different buffers.
None of this is terribly difficult to solve, but compared to the
usefulness it just doesn't seem worth the trouble.

>> So it's OK to create markers are then forget about them, but it's not
>> OK to create overlays and then forget about them: you need to manually
>> `delete-overlay` when you're done with them otherwise they'll accumulate
>> in the buffer.
>
> And that is probably the most important takeaway here - but it raises
> another question.  If I create an overlay in some buffer, keep no
> references to it, and then kill the buffer - is the overlay eligible
> for GC?

Yes, of course, there's nothing very unusual going here, really: when an
overlay is placed in a buffer, it creates a reference from that buffer
to the overlay, so the overlay is reachable as long as the buffer itself
is reachable.


        Stefan




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-26  7:23     ` tomas
@ 2021-05-26 15:36       ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-05-26 16:52         ` tomas
  0 siblings, 1 reply; 19+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-05-26 15:36 UTC (permalink / raw)
  To: help-gnu-emacs

>> But what would happen if I (delete-overlay my-overlay), then it gets
>> garbage collected, and then I (move-overlay my-overlay ...)?
>                                               ^^^^^^^^^^
> You are still holding a reference to it. It would be an extremely
> impolite action on part of the garbage collector to even look at
> your overlay. I'm sure it would bring about a severe reprimand :)

Oh, the GC will definitely look at it, but it will be discrete, you
won't notice it.


        Stefan




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-26 15:36       ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-05-26 16:52         ` tomas
  0 siblings, 0 replies; 19+ messages in thread
From: tomas @ 2021-05-26 16:52 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 325 bytes --]

On Wed, May 26, 2021 at 11:36:16AM -0400, Stefan Monnier via Users list for the GNU Emacs text editor wrote:

> > [...] even look at
> > your overlay. I'm sure it would bring about a severe reprimand :)
> 
> Oh, the GC will definitely look at it, but it will be discrete, you
> won't notice it.

:-)

thanks
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-26 12:30     ` Eli Zaretskii
@ 2021-05-27 16:20       ` Marcin Borkowski
  2021-05-27 16:41         ` Eli Zaretskii
  0 siblings, 1 reply; 19+ messages in thread
From: Marcin Borkowski @ 2021-05-27 16:20 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs


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

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

Stupid me, of course.

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

I think we misunderstood each other.  By "can't be referenced from Lisp"
I meant "there was no object - like a variable - referencing it".  IOW,
no possible Lisp code could get me a reference to that overlay.  So we
agree, we just used words in a different way.

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

Yes, that was what I meant.  Thanks.

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-26 12:34     ` Eli Zaretskii
@ 2021-05-27 16:23       ` Marcin Borkowski
  0 siblings, 0 replies; 19+ messages in thread
From: Marcin Borkowski @ 2021-05-27 16:23 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs


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

>> From: Marcin Borkowski <mbork@mbork.pl>
>> Date: Wed, 26 May 2021 07:01:48 +0200
>> Cc: help-gnu-emacs@gnu.org
>> 
>> > Markers share some of their implementation with overlays, but because
>> > markers do not affect the display and because there is nothing
>> > corresponding to `overlays-at' or `overlays-in', Emacs goes through the
>> > trouble of GC'ing those markers which are still reachable via the
>> > buffer's internal variables (its list of markers) as long as they're not
>> > reachable some other way, of course.
>> 
>> Thanks, too, this is pretty interesting.  I assume one possible reason
>> for not having `markers-in' (analogous to `overlays-in') is that an
>> overlay (even without any variable pointing to it, IOW, a "name" for it)
>> has some "semantics" (properties), so it can be "guessed" what it is
>> for; for markers, we would only know that "someone wanted to remember
>> this position, but we have no idea why/what for", so why even bother
>> keeping it?
>
> I think we don't have markers-in because it isn't needed.  overlays-in
> is needed when you want to find the next or the previous overlay, or
> when you need to know what faces contribute to the appearance of a
> character at certain position.  There's no such need for markers.

Well, that obviously doesn't contradict what I was saying - but thanks
for the explanation (mine was of course just hypothesizing).

Best,

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-26 14:48     ` Stefan Monnier
@ 2021-05-27 16:26       ` Marcin Borkowski
  2021-05-27 21:42         ` Stefan Monnier
  0 siblings, 1 reply; 19+ messages in thread
From: Marcin Borkowski @ 2021-05-27 16:26 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2021-05-26, at 16:48, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

>> Thanks, too, this is pretty interesting.  I assume one possible reason
>> for not having `markers-in' (analogous to `overlays-in') is that an
>> overlay (even without any variable pointing to it, IOW, a "name" for it)
>> has some "semantics" (properties), so it can be "guessed" what it is
>> for; for markers, we would only know that "someone wanted to remember
>> this position, but we have no idea why/what for", so why even bother
>> keeping it?  (Technically, there is also the insertion type, but that
>> doesn't tell much, either.)
>
> There's been proposals to add a `markers-in` function or
> something similar.  The main motivation was to better preserve some
> marker's positions when replacing a chunk of text with another.
>
> But the need is not very compelling, and there are some technical
> issues, one of them being that internally overlays are contain two
> markers, so this would risk exposing those markers, making it possible
> for an overlay to have a start and an end in two different buffers.
> None of this is terribly difficult to solve, but compared to the
> usefulness it just doesn't seem worth the trouble.

I see.

>>> So it's OK to create markers are then forget about them, but it's not
>>> OK to create overlays and then forget about them: you need to manually
>>> `delete-overlay` when you're done with them otherwise they'll accumulate
>>> in the buffer.
>>
>> And that is probably the most important takeaway here - but it raises
>> another question.  If I create an overlay in some buffer, keep no
>> references to it, and then kill the buffer - is the overlay eligible
>> for GC?
>
> Yes, of course, there's nothing very unusual going here, really: when an
> overlay is placed in a buffer, it creates a reference from that buffer
> to the overlay, so the overlay is reachable as long as the buffer itself
> is reachable.

Right, I didn't say that it's unusual - I only wanted to clarify that
I don't need to e.g. bother with `remove-overlays' before killing the
buffer (unless I keep a reference to some of them, of course).

Thanks,

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-27 16:20       ` Marcin Borkowski
@ 2021-05-27 16:41         ` Eli Zaretskii
  2021-05-28 19:38           ` Marcin Borkowski
  0 siblings, 1 reply; 19+ messages in thread
From: Eli Zaretskii @ 2021-05-27 16:41 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Cc: help-gnu-emacs@gnu.org
> Date: Thu, 27 May 2021 18:20:46 +0200
> 
> I think we misunderstood each other.  By "can't be referenced from Lisp"
> I meant "there was no object - like a variable - referencing it".

Well, "can't" means "cannot", not "doesn't".



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-27 16:26       ` Marcin Borkowski
@ 2021-05-27 21:42         ` Stefan Monnier
  0 siblings, 0 replies; 19+ messages in thread
From: Stefan Monnier @ 2021-05-27 21:42 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs

> I only wanted to clarify that I don't need to e.g. bother with
> `remove-overlays' before killing the buffer.

Indeed, you doubly don't:
- when you kill a buffer, its overlays are automatically disconnected
  from the buffer.
- even if `kill-buffer` were changed not to do that, when the buffer
  is not reachable any more, the overlays to which it point would also
  become GC'able (unless you keep a reference to them, of course).


        Stefan




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: When are unused overlays garbage collected?
  2021-05-27 16:41         ` Eli Zaretskii
@ 2021-05-28 19:38           ` Marcin Borkowski
  0 siblings, 0 replies; 19+ messages in thread
From: Marcin Borkowski @ 2021-05-28 19:38 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs


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

>> From: Marcin Borkowski <mbork@mbork.pl>
>> Cc: help-gnu-emacs@gnu.org
>> Date: Thu, 27 May 2021 18:20:46 +0200
>> 
>> I think we misunderstood each other.  By "can't be referenced from Lisp"
>> I meant "there was no object - like a variable - referencing it".
>
> Well, "can't" means "cannot", not "doesn't".

Well, I'm not a native speaker of English - maybe that's why I didn't
understand you.  My understanding of "can't" was more or less "the
programmer can't write any Lisp code to get the reference to it", and
I assume that you meant "there is no object attainable from Lisp code
referencing it" - so basically the same.

But let's not split that hair anymore.  Anyway, thanks for you answers.

Best,

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2021-05-28 19:38 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-24  5:00 When are unused overlays garbage collected? Marcin Borkowski
2021-05-24  5:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-24  6:33   ` Marcin Borkowski
2021-05-24 12:27 ` Eli Zaretskii
2021-05-26  4:53   ` Marcin Borkowski
2021-05-26  7:23     ` tomas
2021-05-26 15:36       ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-26 16:52         ` tomas
2021-05-26 12:30     ` Eli Zaretskii
2021-05-27 16:20       ` Marcin Borkowski
2021-05-27 16:41         ` Eli Zaretskii
2021-05-28 19:38           ` Marcin Borkowski
2021-05-24 17:07 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-26  5:01   ` Marcin Borkowski
2021-05-26 12:34     ` Eli Zaretskii
2021-05-27 16:23       ` Marcin Borkowski
2021-05-26 14:48     ` Stefan Monnier
2021-05-27 16:26       ` Marcin Borkowski
2021-05-27 21:42         ` Stefan Monnier

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.