Subject: Re: bug#43389: 28.0.50; Emacs memory leaks

From: Jean Louis <[email-masked]>
Date: Mon, 23 Nov 2020 21:55:57 +0300

* Eli Zaretskii <[email-masked]> [2020-11-18 18:04]:
> > Date: Wed, 18 Nov 2020 00:45:48 +0300
> > From: Jean Louis <[email-masked]>
> > Cc: [email-masked], [email-masked], [email-masked],
> >   [email-masked], [email-masked], [email-masked]
> > 
> > Maybe some simple new and automatic function could be temporarily
> > included to spit errors to output on what is Emacs doing when it
> > starts swapping (if it is swapping), then such errors could at least
> > be captured in a file even if I have to reboot computer.

I use now M-x vsize-with-timer for 2GB and M-x good-bye to capture
that basic data.

(defun vsize-value ()
  (let* ((attributes (process-attributes (emacs-pid)))
	 (vsize-name (car (elt attributes 5)))
	 (vsize-value (cdr (elt attributes 5))))
    (list vsize-name vsize-value)))

(defun vsize-check (&optional gb)
  (let* ((vsize (cadr (vsize-value)))
	 (gb (or gb 2))
	 (gb-1 1048576.0)
	 (gb (* gb gb-1)))
    (when (> vsize gb)
	(message "vsize: %.02fG" (/ vsize gb-1)))))

(defun vsize-with-timer (gb)
  (interactive "nGiB: ")
  (let ((timer (run-with-timer 1 30 'vsize-check gb)))
    (message "Timer: %s" timer)))

(defun good-bye ()
  (interactive)
  (let* ((garbage (garbage-collect))
	 (size 0)
	 (buffers-size (dolist (buffer (buffer-list) size)
			(setq size (+ size (buffer-size buffer)))))
	 (uptime (emacs-uptime))
	 (pid (emacs-pid))
	 (vsize (vsize-value))
	 (file (format "~/tmp/emacs-session-%s.el" pid))
	 (list (list (list 'uptime uptime) (list 'pid pid)
		     (list 'garbage garbage) (list 'buffers-size buffers-size)
		     (list 'vsize vsize))))
    (with-temp-file file
      (insert (prin1-to-string list)))
    (message file)))