Subject: Re: bug#79023: 30.1.90; Suspicion of memory leak on internal_redisplay (MacOS)

From: Rudolf Adamkovič <[email-masked]>
Date: Tue, 22 Jul 2025 01:33:45 +0200

Rudolf Adamkovič <[email-masked]> writes:

> [...], and indeed, and the code above resulted in 1098 allocations of
> 4.72 MB, totaling ~5.18 GB, and they are all like these: [...]

Okay, I could not let go before sleep and investigated further.  See the
attached patch.  With it, the never-freed 5.18 GB of RAM is fixed for:

(dotimes (x 1000)
  (let ((frame (make-frame-command)))
    (sleep-for 0.01)
    (delete-frame frame)))

P.S. The remaining 5.22 - 5.13 ~= 100 MB is wasted due to bugs elsewhere.

Rudy
>From 99f8178c42b5a511a81be79256d629925375c5ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rudolf=20Adamkovi=C4=8D?= <[email-masked]>
Date: Tue, 22 Jul 2025 01:10:26 +0200
Subject: [PATCH] NS: Release EmacsView and EmacsLayer when deallocating
 EmacsWindow

* src/nsterm.m: (ns_free_frame_resources, [EmacsView dealloc]): When
deallocating EmacsWindow, release both its view (EmacsView) and its
backing layer (EmacsLayer).  When making and deleting 1000 frames in a
loop, using (make-frame-command) and (delete-frame) respectively, along
with (sleep-for 0.01) in between, the never-freed RAM goes from 5.18
gigabytes to "only" about 100 megabytes, which are wasted due to bugs
elsewhere.
---
 src/nsterm.m | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/nsterm.m b/src/nsterm.m
index 003aadb9782..54e5a71f7f6 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1636,7 +1636,10 @@ Hide the window (X11 semantics)
     [f->output_data.ns->miniimage release];
 
   [[view window] close];
+
+  /* Uninstall and release the view.  */
   [view removeFromSuperview];
+  [view release];
 
   xfree (f->output_data.ns);
   f->output_data.ns = NULL;
@@ -6757,6 +6760,11 @@ - (void)dealloc
 
   if (fs_state == FULLSCREEN_BOTH)
     [nonfs_window release];
+
+  /* Release the backing layer.  */
+  EmacsLayer *layer = (EmacsLayer *)[self layer];
+  [layer release];
+
   [super dealloc];
 }
 
-- 
2.39.5 (Apple Git-154)

-- 
"Logic is a science of the necessary laws of thought, without which no
employment of the understanding and the reason takes place."
--- Immanuel Kant, 1785

Rudolf Adamkovič <[email-masked]> [he/him]
http://adamkovic.org