Subject: bug#45200: [PATCH] Force Glibc to free the memory freed

From: Eli Zaretskii <[email-masked]>
Date: Sun, 24 Jan 2021 17:40:29 +0200

> From: Konstantin Kharlamov <[email-masked]>
> Date: Sun, 24 Jan 2021 18:24:02 +0300
> 
> configure.ac: check whether malloc_trim is suported
> src/alloc.c (lisp_free): 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 bcc0be7de0..3e0459a0e2 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -4544,6 +4544,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 c0a55e61b9..97e3ceb52c 100644
> --- a/src/alloc.c
> +++ b/src/alloc.c
> @@ -1047,6 +1047,9 @@ lisp_free (void *block)
>  
>    MALLOC_BLOCK_INPUT;
>    free (block);
> +#ifdef HAVE_MALLOC_TRIM
> +  malloc_trim(0); /* work around for high memory consumption, see bug 45200 */
> +#endif /* HAVE_MALLOC_TRIM */
>  #ifndef GC_MALLOC_CHECK
>    mem_delete (mem_find (block));
>  #endif

Thanks, but is it really a good idea to call malloc_trim each time we
free some chunk of memory?

Carlos, Florian, DJ: any thoughts or comments on this proposal?  (Let
me know if you need some background about the code involved in this.)