unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Konstantin Kharlamov <hi-angel@yandex.ru>
To: Stefan Monnier <monnier@iro.umontreal.ca>, Eli Zaretskii <eliz@gnu.org>
Cc: carlos@redhat.com, fweimer@redhat.com, 45200@debbugs.gnu.org,
	dj@redhat.com
Subject: bug#45200: [PATCH] Force Glibc to free the memory freed
Date: Wed, 03 Feb 2021 00:17:02 +0300	[thread overview]
Message-ID: <331805c74fc5d3d412dd2065030b11fa3343710d.camel@yandex.ru> (raw)
In-Reply-To: <jwvczxrg1ls.fsf-monnier+emacs@gnu.org>

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

On Tue, 2021-01-26 at 10:30 -0500, Stefan Monnier wrote:
> > That'd be easy to implement, but I'm not yet sure it's the best
> > alternative.  In particular, I'd like to have some idea regarding how
> > much time such a call could take.  In some usage patterns Emacs calls
> > GC very frequently, which slows down command execution and makes Emacs
> > less responsive.
>
> The most important case where this happens is during phases of creation
> of large new structures (e.g. while loading a large package like Org),
> where a lot of memory is allocated without generating much garbage, so
> every time we run the GC that time is mostly wasted (since we don't
> recover any garbage) and calling malloc_trim would also be useless in
> those cases.

So, I just did some benchmarking; the patch I bencharked it with was modified per Stefan's comment (i.e. `malloc_trim(0)` is moved to the end of `garbage_collec()`). I'm attaching it here as well.

Benchamarking was done as follows: I ran built emacs as `src/emacs -Q`; and used this code

    (benchmark-run
        (let ((i 1000000))
          (while (> i 0)
            (print "hello")
            (setq i (- i 1)))))

which I ran 3 times. First with the patch; then I git-checked out HEAD^ (i.e. to the code without my patch), built it, and ran 3 times again.

This code you may recognize, it was the reproducer to show Emacs taking 200M of memory, except I cut out the line disabling GC, and the line that calls GC manually. IOW I used a known memory-heavy testcase, and ran it with vanilla Emacs configuration.

Results are:

    with malloc_trim:
        (8.920371394 232 2.106283245)
        (9.038083601 231 2.060810826)
        (9.140798641 231 2.0594013240000004)

    without malloc_trim:
        (8.987097209 232 2.070143482)
        (8.700478084 231 1.7745506179999997)
        (8.781121056 231 1.7870093610000004)

The difference is just 3-4% (8.7 / 9 ≈ 0.9666666667). It looks to me insignificant enough to not show up anywhere during interactive work with Emacs.


[-- Attachment #2: 1.patch --]
[-- Type: text/x-patch, Size: 1348 bytes --]

From bc09ce4e25ff2c361d19ace35e556b738651c0a2 Mon Sep 17 00:00:00 2001
From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
Date: Sun, 24 Jan 2021 17:55:23 +0300
Subject: [PATCH] Force Glibc to free the memory freed

configure.ac: check whether malloc_trim is suported
src/alloc.c (garbage_collect): call malloc_trim() if possible
(bug#45200)
---
 configure.ac | 3 +++
 src/alloc.c  | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/configure.ac b/configure.ac
index 08f3c0cd85..416cfbe389 100644
--- a/configure.ac
+++ b/configure.ac
@@ -4556,6 +4556,9 @@ AC_DEFUN
 dnl the current CFLAGS etc.
 AC_CHECK_FUNCS(snprintf)
 
+
+AC_CHECK_FUNCS(malloc_trim)
+
 dnl Check for glib.  This differs from other library checks in that
 dnl Emacs need not link to glib unless some other library is already
 dnl linking to glib.  Although glib provides no facilities that Emacs
diff --git a/src/alloc.c b/src/alloc.c
index b86ed4ed26..3124378fb8 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -6146,6 +6146,9 @@ garbage_collect (void)
       if (tot_after < tot_before)
 	malloc_probe (min (tot_before - tot_after, SIZE_MAX));
     }
+#ifdef HAVE_MALLOC_TRIM
+  malloc_trim(0); /* work around for high memory consumption, see bug 45200 */
+#endif /* HAVE_MALLOC_TRIM */
 }
 
 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
-- 
2.30.0


  reply	other threads:[~2021-02-02 21:17 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-12 18:43 bug#45200: Memory leaks: (garbage-collect) fails to reclaim memory Konstantin Kharlamov
2020-12-12 20:15 ` Eli Zaretskii
2020-12-12 22:44   ` Konstantin Kharlamov
2020-12-12 22:59     ` Lars Ingebrigtsen
2020-12-13  6:08       ` Eli Zaretskii
2020-12-13  5:53     ` Eli Zaretskii
2020-12-13 12:07       ` Konstantin Kharlamov
2021-01-24 15:24 ` bug#45200: [PATCH] Force Glibc to free the memory freed Konstantin Kharlamov
2021-01-24 15:40   ` Eli Zaretskii
2021-01-25 22:17     ` DJ Delorie
2021-01-25 22:28       ` Konstantin Kharlamov
2021-01-26 14:55         ` Eli Zaretskii
2021-01-26 15:02           ` Konstantin Kharlamov
2021-01-26 15:30           ` Stefan Monnier
2021-02-02 21:17             ` Konstantin Kharlamov [this message]
2021-02-03  4:45               ` Stefan Monnier
2021-02-03  4:50                 ` Stefan Monnier
2021-02-03  6:04                   ` Konstantin Kharlamov
2021-02-03  7:07                     ` Eli Zaretskii
2021-02-03  7:15                       ` Konstantin Kharlamov
2021-02-03  7:39                     ` martin rudalics
2021-02-03  8:23                       ` Konstantin Kharlamov
2021-02-03  9:35                         ` martin rudalics
2021-02-03  9:49                           ` Konstantin Kharlamov
2021-02-03 10:35                             ` Konstantin Kharlamov
2021-02-03 11:06                             ` martin rudalics
2021-02-03 11:08                               ` Konstantin Kharlamov
2021-02-03 11:16                                 ` Konstantin Kharlamov
2021-02-03 12:56                                 ` martin rudalics
2021-02-03 13:00                                   ` Konstantin Kharlamov
2021-02-03 15:14                                     ` martin rudalics
2021-02-03 15:15                                     ` Stefan Monnier
2021-02-03 15:29                                       ` Konstantin Kharlamov
2021-02-03 16:02                                         ` Stefan Monnier
2021-02-03 16:35                                           ` Konstantin Kharlamov
2021-02-03 16:51                                             ` Stefan Monnier
2021-02-03 19:30                                               ` DJ Delorie
2021-02-03 19:36                                             ` DJ Delorie
2021-02-03 20:28                                               ` Konstantin Kharlamov
2021-02-03 20:51                                                 ` DJ Delorie
2021-05-18 20:12                                           ` Konstantin Kharlamov
2021-05-19  4:11                                             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-05-19  4:26                                               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-05-19  6:46                                                 ` Konstantin Kharlamov
2021-05-19  9:47                                                   ` Eli Zaretskii
2021-05-19  9:55                                                     ` Konstantin Kharlamov
2021-05-19 10:09                                                       ` Eli Zaretskii
2021-02-03 14:51                             ` Eli Zaretskii
2021-02-03 15:01                               ` Konstantin Kharlamov
2021-02-03 14:44                         ` Eli Zaretskii
2021-02-03 15:12                           ` Andreas Schwab
2021-02-03 19:25                           ` DJ Delorie
2021-02-03 19:49                             ` Eli Zaretskii
2021-02-03 21:00                               ` DJ Delorie
2021-02-03 20:24                             ` Stefan Monnier
2021-02-03 20:42                               ` DJ Delorie
2021-02-03 22:07                                 ` Stefan Monnier
2021-02-03 22:21                                   ` DJ Delorie
2021-02-03 23:32                                     ` Stefan Monnier
2021-02-04  0:31                                       ` DJ Delorie
2021-02-04  3:26                                         ` Stefan Monnier
2021-02-04  3:38                                           ` DJ Delorie
2021-02-04  3:55                                             ` Stefan Monnier
2021-02-04  4:02                                               ` DJ Delorie
2021-02-04  4:19                                                 ` Stefan Monnier
2021-02-04  4:26                                                   ` DJ Delorie
2021-02-04  4:04                                               ` DJ Delorie
2021-02-03 15:15                     ` Stefan Monnier
2021-01-26 14:49       ` Eli Zaretskii
2021-01-26 16:13         ` DJ Delorie
2021-12-04 23:20     ` bug#45200: Memory leaks: (garbage-collect) fails to reclaim memory Lars Ingebrigtsen
2021-12-05  6:23       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-12-05  8:26         ` Eli Zaretskii
2022-05-01  9:43           ` bug#45200: Wishlist: There should be a `malloc-trim' function Lars Ingebrigtsen
2021-12-05 19:59         ` bug#45200: Memory leaks: (garbage-collect) fails to reclaim memory Lars Ingebrigtsen
2021-12-05  7:07       ` Eli Zaretskii
2021-01-24 18:51 ` Stefan Monnier
2021-01-24 19:00   ` Konstantin Kharlamov
2021-01-24 19:06     ` Konstantin Kharlamov
2021-01-24 19:55       ` Eli Zaretskii
2021-01-24 19:12     ` Stefan Monnier
2021-01-24 20:00       ` Konstantin Kharlamov
2021-01-24 20:11         ` Eli Zaretskii
2021-01-24 20:21           ` Konstantin Kharlamov
2021-01-24 21:20             ` Stefan Monnier
2021-01-24 21:26               ` Konstantin Kharlamov
2021-01-24 21:41                 ` Stefan Monnier
2021-01-24 21:55                   ` Konstantin Kharlamov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=331805c74fc5d3d412dd2065030b11fa3343710d.camel@yandex.ru \
    --to=hi-angel@yandex.ru \
    --cc=45200@debbugs.gnu.org \
    --cc=carlos@redhat.com \
    --cc=dj@redhat.com \
    --cc=eliz@gnu.org \
    --cc=fweimer@redhat.com \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).