Subject: Re: bug#51763: 27.2; Displaying many images take all memory

From: Stefan Kangas <[email-masked]>
Date: Thu, 11 Nov 2021 01:50:59 -0800

Thierry Volpiatto <[email-masked]> writes:

> I see you changed image-dired to use image-mode in emacs-29 instead of using like
> before image-magick.  That's fine I used the same approach two years ago
> in Helm, but switched back quickly to image-dired because it was taking
> all memory and it was not recoverable until I kill emacs. I thought it
> was my fault but I see new image-dired in emacs-29 have same problem:
>
> 1) emacs -Q
> 2) Open a large image directory with dired
> 3) Open files one by one with C-t i until memory is full (use f3 C-t i
> C-n f4 etc...). Memory starts to grow seriously after around 70 files
> for me.
>
> Killing the image-dired buffer changes nothing, I have to restart emacs
> to recover memory.

Thanks for the bug report!

In principle there should be no difference between the two: in both
cases we need to cache the same upscaled image.

However, we currently have an issue with our built-in image scaling that
we cache the image both before resizing and after.  I suspect that this
is the explanation for the higher memory usage you see.

See this comment in `image--scale-within-limits-p':

            ;; Note: `image-size' looks up and thus caches the
            ;; untransformed image.  There's no easy way to
            ;; prevent that.

and the relevant code in image.c that verifies this.

I believe that we could fix this in image.c.  I don't think it's
necessarily very hard, but it does take some coding.

However, I'm curious what you mean when you say that it "never" frees
the memory.  What happens if you set `image-cache-eviction-delay' to
some very low value like 5 seconds?

AFAIU, calling `clear-image-cache' should also the free memory unless we
have a memory leak.  You could also call this function from your code.
Does calling this function free the memory for you?