Subject: bug#56546: 29.0.50; unbounded RAM comsumption when displaying images

From: "Jose A. Ortega Ruiz" <[email-masked]>
Date: Thu, 14 Jul 2022 02:35:46 +0100

It seems to be easy to make emacs (under X) to consume more and more
RAM, which is never released, by making it display images.  A extreme
(in my experience) case is animated GIFs, try:

  - emacs -Q
  - M-x eww RET https://xenodium.com/emacs-zones-to-lift-you-up/
  - RAM consumption grows to ~600Mb
  - R (redisplay page): RAM grows to ~1100Mb
  - R (redisplay page): RAM grows to ~1752Mb
  - R (redisplay page): RAM grows to ~2222Mb
  - rinse and repeat: RAM never goes down
  - (image-cache-size) reports a modest 82Mb
  - Kill buffer:  high RAM consumption is still at its maximum, even
    after (image-cache-size) goes to 0

My impression is that this bad behaviour is not limited to animated
gifts, although for regular images i don't have a solid recipe: what i
observe is that running long sessions in term mode for days of continous
use in an xterm, RAM tops at about 0.5Mb, while running similar sessions
under X (using the same packages and doing the same kind of things
inside of emacs), RAM will steadily increase.  The only difference
between the two scenarios i can think of is that in X i sometimes
display images, mainly in eww and, sometimes, inside HTML email messages
rendered via shr.  I also observe that while it's pretty common for
emacs in an x term (either run directly or connected to a demon) to
release memory (i.e., to have its RSS decrease), that almost never
happens when running under X: there RAM almost never goes down, no
matter what.

In GNU Emacs 29.0.50 (build 16, x86_64-pc-linux-gnu, cairo version 1.16.0)
 of 2022-07-14 built on rivendell
Repository revision: 9a888323c60c60fb37f471ef03f0bcdff91cb850
Repository branch: master
System Description: Debian GNU/Linux bookworm/sid

Configured using:
 'configure --prefix=/usr/local/stow/emacs --with-x-toolkit=no
 --with-imagemagick'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ
IMAGEMAGICK JPEG JSON LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2
M17N_FLT MODULES NOTIFY INOTIFY OLDXMENU PDUMPER PNG RSVG SECCOMP SOUND
SQLITE3 THREADS TIFF WEBP X11 XDBE XIM XINPUT2 XPM ZLIB

Important settings:
  value of $LANG: en_GB.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  telega-root-auto-fill-mode: t
  telega-active-locations-mode: t
  telega-patrons-mode: t
  telega-mode-line-mode: t
  diff-hl-margin-mode: t
  global-diff-hl-mode: t
  eshell-vterm-mode: t
  eshell-syntax-highlighting-global-mode: t
  pdf-occur-global-minor-mode: t
  winner-mode: t
  global-git-commit-mode: t
  magit-auto-revert-mode: t
  global-auto-revert-mode: t
  shell-dirtrack-mode: t
  vertico-mode: t
  global-company-mode: t
  company-mode: t
  marginalia-mode: t
  persistent-scratch-autosave-mode: t
  global-so-long-mode: t
  pulsar-global-mode: t
  pulsar-mode: t
  display-battery-mode: t
  jao-minibuffer-mode: t
  minibuffer-electric-default-mode: t
  minibuffer-depth-indicate-mode: t
  xclip-mode: t
  repeat-mode: t
  savehist-mode: t
  recentf-mode: t
  save-place-mode: t
  override-global-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  column-number-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
/home/jao/lib/elisp/org-static-blog/org-static-blog hides /home/jao/.emacs.d/elpa.29/org-static-blog-20220508.1410/org-static-blog
/home/jao/etc/emacs/site/custom hides /usr/local/stow/emacs/share/emacs/29.0.50/lisp/custom
/home/jao/.emacs.d/elpa.29/transient-20220527.2213/transient hides /usr/local/stow/emacs/share/emacs/29.0.50/lisp/transient

Features:
(shadow mailalias bbdb-message vertico-directory tramp-cmds sort
gnus-cite mm-archive mail-extr textsec uni-scripts idna-mapping
ucs-normalize uni-confusable textsec-check gnus-async gnus-bcklg
gnus-dup qp gnus-ml gnus-topic nnml bbdb-gnus network-stream bbdb-mua
gnus-icalendar icalendar ol-gnus nnselect org-capture gnus-delay
gnus-draft gnus-agent gnus-srvr gnus-score score-mode nnvirtual
gnus-cache gnus-msg gnus-art mm-uu mml2015 mm-view mml-smime smime dig
gnus-sum nndraft nnmh gnus-demon nntp gnus-group gnus-undo gnus-start
gnus-dbus gnus-cloud nnimap nnmail mail-source utf7 netrc nnoo gnus-spec
gnus-int gnus-range gnus-win org-duration org-appear org-agenda
org-refile cal-iso mule-util cal-move bigml bml-logs bml bml-misc
bml-whizzml bml-clojure bml-clj-tests bml-python bml-skels bml-utils
multisession sqlite whizzml-skeletons whizzml-mode lice sieve sieve-mode
sieve-manage sasl sasl-anonymous sasl-login sasl-plain jao-mpc
jao-random-album jao-lyrics jao-mpris telega-obsolete telega
telega-tdlib-events telega-webpage visual-fill-column telega-match
telega-root telega-info telega-chat telega-modes telega-company
telega-user telega-notifications telega-voip telega-msg telega-tme
telega-sticker telega-i18n telega-vvnote bindat telega-ffplay
telega-sort telega-filter telega-ins telega-folders telega-inline
telega-util telega-media telega-tdlib rainbow-identifiers dired-aux
telega-server telega-core cursor-sensor telega-customize emacsbug
jao-mullvad bluetooth enwc enwc-backend json-mode json-snatcher js
cc-mode cc-fonts cc-guess cc-menus cc-cmds cc-styles cc-align cc-engine
cc-vars cc-defs yaml-mode virtualenvwrapper gud ediprolog pie
haskell-doc inf-haskell haskell-decl-scan haskell haskell-completions
haskell-load haskell-commands highlight-uses-mode haskell-modules
haskell-sandbox haskell-navigate-imports haskell-repl haskell-svg
haskell-collapse hideshow haskell-debug haskell-interactive-mode
haskell-presentation-mode haskell-compile haskell-hoogle haskell-process
haskell-session haskell-mode haskell-cabal haskell-utils
haskell-font-lock haskell-indentation haskell-string
haskell-sort-imports haskell-lexeme haskell-align-imports
haskell-complete-module haskell-ghc-support flymake-proc flymake
warnings dabbrev haskell-customize geiser-guile info-look geiser-repl
geiser-compile geiser-debug geiser-image geiser-capf geiser-doc
geiser-menu geiser-edit etags fileloop xref project geiser-completion
geiser-autodoc geiser-eval geiser-connection geiser-syntax scheme
geiser-impl help-fns radix-tree geiser-log geiser-popup view
geiser-custom geiser-base geiser idris-mode idris-commands
idris-hole-list idris-ipkg-mode idris-tree-info idris-warnings-tree
idris-info idris-repl idris-highlight-input idris-prover inferior-idris
idris-warnings idris-log idris-events idris-simple-indent idris-syntax
idris-common-utils idris-settings idris-keys idris-core idris-compat
prop-menu package-lint finder lisp-mnt edit-list outline-minor-faces
git-modes gitignore-mode gitconfig-mode conf-mode gitattributes-mode
git-link git-timemachine diff-hl-margin diff-hl-dired diff-hl log-view
vc-dir ewoc vc jao-eshell-here eshell-autojump em-dirs esh-var eshell-up
git-ps1-mode eshell-vterm em-term eshell-syntax-highlighting em-alias
vterm tramp tramp-loaddefs trampver tramp-integration files-x
tramp-compat ls-lisp face-remap term ehelp vterm-module term/xterm xterm
jao-custom-email bbdb-anniv bbdb-com bbdb bbdb-site timezone randomsig
nov esxml-query saveplace-pdf-view pdf-occur ibuf-ext ibuffer
ibuffer-loaddefs tablist tablist-filter semantic/wisent/comp
semantic/wisent semantic/wisent/wisent semantic/util-modes semantic/util
semantic semantic/tag semantic/lex semantic/fw mode-local cedet
pdf-isearch pdf-misc socks elpher jao-custom-eww ol-eww jao-eww-session
eww xdg url-queue mm-url gnus nnheader range markdown-toc
jao-custom-blog htmlize jao-custom-org jao-org-links jao-doc-view
doc-view pdf-tools pdf-view pdf-cache pdf-info tq pdf-util pdf-macs
image-mode exif ol-info ol-eshell esh-mode eshell esh-cmd esh-ext
esh-opt esh-proc esh-io esh-arg esh-module esh-groups esh-util
jao-org-notes ob-shell ob-scheme ob-python python ob-org ob-ocaml
ob-makefile ob-haskell ob-gnuplot ob-clojure ob-calc calc-store
calc-trail ob-prolog prolog smie align org-tempo tempo ox-texinfo
ox-latex ox-html table ox-ascii ox-publish ox org-fragtog org-element
avl-tree generator jao-afio winner consult-recoll embark-consult
consult-vertico consult compat-28 magit-bookmark bookmark jao-recoll org
ob ob-tangle ob-ref ob-lob ob-table ob-exp org-macro org-footnote
org-src ob-comint org-pcomplete org-list org-faces org-entities
org-version ob-emacs-lisp ob-core ob-eval org-table oc-basic bibtex ol
org-keys oc org-compat org-macs org-loaddefs find-func
jao-custom-completion embark-vc s code-review code-review-actions
code-review-comment code-review-section code-review-bitbucket
code-review-faces shr pixel-fill kinsoku url-file url-dired svg dom
emojify apropos tar-mode arc-mode archive-mode ht code-review-gitlab
code-review-utils code-review-parse-hunk code-review-github
code-review-db uuidgen calc-misc calc-ext calc calc-loaddefs rect
calc-macs a code-review-interfaces deferred forge-list forge-commands
forge-semi forge-bitbucket buck forge-gogs gogs forge-gitea gtea
forge-gitlab glab forge-github ghub-graphql treepy gsexp ghub let-alist
gnutls forge-notify forge-revnote forge-pullreq forge-issue forge-topic
yaml parse-time iso8601 bug-reference forge-post markdown-mode
edit-indirect noutline outline forge-repo forge forge-core forge-db
closql emacsql-sqlite advice emacsql emacsql-compiler url-http url-auth
url-gw nsm magit-submodule magit-obsolete magit-blame magit-stash
magit-reflog magit-bisect magit-push magit-pull magit-fetch magit-clone
magit-remote magit-commit magit-sequence magit-notes magit-worktree
magit-tag magit-merge magit-branch magit-reset magit-files magit-refs
magit-status magit magit-repos magit-apply magit-wip magit-log
which-func magit-diff smerge-mode diff git-commit log-edit message
sendmail yank-media puny rfc822 mml mml-sec gnus-util time-date
mm-decode mm-bodies mm-encode mail-parse rfc2231 rfc2047 rfc2045 mm-util
ietf-drums mail-prsvr mailabbrev mail-utils gmm-utils mailheader
pcvs-util add-log magit-core magit-autorevert autorevert filenotify
magit-margin magit-transient magit-process with-editor shell pcomplete
magit-mode magit-git magit-base magit-section crm dash compat-27
compat-26 embark ffap thingatpt vertico company-keywords company-dabbrev
company-files company-capf company pcase marginalia orderless imenu
jao-skel-latex jao-skel-haskell jao-compilation jao-skel-lisp
jao-skel-geiser jao-skel skeleton autoinsert wgrep grep compile
text-property-search comint ring jka-compr dired-x dired dired-loaddefs
persistent-scratch so-long cal-china lunar solar cal-dst cal-bahai
cal-islam cal-hebrew holidays holiday-loaddefs vc-git diff-mode
vc-dispatcher appt diary-lib diary-loaddefs cal-menu calendar
cal-loaddefs pulsar pulse color tmr jao-tracking tracking shorten
jao-notify alert log4e notifications gntp battery jao-mode-line
jao-minibuffer minibuf-eldef mb-depth xclip diminish
jao-light-term-theme jao-themes ansi-color disp-table server pinentry
epa-file epa derived epg rfc6068 epg-config transient format-spec compat
cus-edit pp cus-load repeat edmacro kmacro jao-shell jao-sleep dbus xml
savehist recentf tree-widget wid-edit saveplace jao-gnus-private
gnu-elpa-keyring-update cl-extra help-mode use-package
use-package-ensure use-package-delight use-package-diminish
use-package-bind-key bind-key easy-mmode use-package-core finder-inf
tex-site bbdb-autoloads cider-autoloads clojure-mode-autoloads
company-autoloads diff-hl-autoloads edit-indirect-autoloads
elpher-autoloads embark-consult-autoloads consult-autoloads
embark-vc-autoloads embark-autoloads code-review-autoloads
forge-autoloads ghub-autoloads git-link-autoloads idris-mode-autoloads
json-mode-autoloads rx link-hint-autoloads magit-autoloads
git-commit-autoloads magit-section-autoloads marginalia-autoloads
markdown-mode-autoloads org-appear-autoloads org-fragtog-autoloads
outline-minor-faces-autoloads paredit-autoloads pdf-tools-autoloads
persistent-scratch-autoloads pulsar-autoloads racket-mode-autoloads
request-autoloads switch-window-autoloads telega-autoloads tmr-autoloads
vertico-autoloads dash-autoloads vterm-autoloads with-editor-autoloads
info compat-autoloads xclip-autoloads yaml-autoloads package browse-url
url url-proxy url-privacy url-expand url-methods url-history url-cookie
generate-lisp-file url-domsuf url-util mailcap url-handlers url-parse
auth-source cl-seq eieio eieio-core cl-macs password-cache json subr-x
map byte-opt gv bytecomp byte-compile cconv url-vars cl-loaddefs cl-lib
rmc iso-transl tooltip eldoc paren electric uniquify ediff-hook vc-hooks
lisp-float-type elisp-mode mwheel term/x-win x-win term/common-win x-dnd
tool-bar dnd fontset image regexp-opt fringe tabulated-list replace
newcomment text-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer nadvice seq
simple cl-generic indonesian philippine cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms
cp51932 hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese composite emoji-zwj charscript charprop case-table
epa-hook jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button
loaddefs faces cus-face macroexp files window text-properties overlay
sha1 md5 base64 format env code-pages mule custom widget keymap
hashtable-print-readable backquote threads dbusbind inotify lcms2
dynamic-setting system-font-setting font-render-setting cairo xinput2 x
multi-tty make-network-process emacs)

Memory information:
((conses 16 1050346 86454)
 (symbols 48 89440 112)
 (strings 32 323218 19301)
 (string-bytes 1 9679236)
 (vectors 16 148160)
 (vector-slots 8 2070697 64427)
 (floats 8 1357 901)
 (intervals 56 4457 7319)
 (buffers 992 29))

-- 
The greatest of faults, I should say, is to be conscious of none.
 -Thomas Carlyle, writer (1795-1881)