1 ;;; memory-usage.el --- Analyze the memory usage of Emacs in various ways
3 ;; Copyright (C) 2002, 2004, 2012 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; This package provides the command `memory-usage', which lists all
27 ;; buffers and how much memory they use.
31 (defvar memory-usage-word-size (ceiling (/ (log most-positive-fixnum 2) 8))
32 "Size of a Lisp word box in bytes.")
34 (defun memory-usage-buffer-size-bytes (b)
35 "Return total number of bytes in the buffer contents."
36 (with-current-buffer b
39 (- (position-bytes (point-max)) (position-bytes (point-min))))))
41 (defun memory-usage-buffer-gap-bytes (b)
42 "Return total number of bytes in the buffer gap."
43 (with-current-buffer b
46 (defun memory-usage-buffer-total-bytes (b)
47 "Return total number of ralloc bytes used by buffer."
48 (with-current-buffer b
51 (+ (position-bytes (point-max))
52 (- (position-bytes (point-min)))
55 (defun memory-usage-mult-cons (n c)
56 (setq n (* n memory-usage-word-size))
57 (cons (* n (car c)) (* n (cdr c))))
59 (defun memory-usage-format (bytes)
60 (setq bytes (/ bytes 1024.0))
62 "kB" "MB" "GB" "TB")))
63 (while (>= bytes 1024)
64 (setq bytes (/ bytes 1024.0))
65 (setq units (cdr units)))
67 ;; ((integerp bytes) (format "%4d%s" bytes (car units)))
68 ((>= bytes 100) (format "%4.0f%s" bytes (car units)))
69 ((>= bytes 10) (format "%4.1f%s" bytes (car units)))
70 (t (format "%4.2f%s" bytes (car units))))))
73 (defun memory-usage ()
74 "List all buffers and their memory usage."
76 (pop-to-buffer (get-buffer-create "*Buffer Details*"))
78 (let* ((bufs (buffer-list))
80 (gc-stats (garbage-collect))
81 (gc-stats (if (numberp (caar gc-stats))
84 ,(max (* memory-usage-word-size (cadr x))
86 ,@(let ((stat (nth (cddr x) gc-stats)))
88 (list (car stat) (cdr stat))
99 (insert (format "Garbage collection stats:\n%s\n\n =>" gc-stats))
103 (let* ((size (nth 1 x))
104 (xlive (* size (nth 2 x)))
105 (xdead (if (nth 3 x) (* size (nth 3 x)))))
107 (format "\t%s (+ %s dead) in %s\n"
108 (memory-usage-format xlive)
109 (memory-usage-format xdead)
111 (format "\t%s in %s\n"
112 (memory-usage-format xlive)
114 (setq live (+ live xlive))
115 (if xdead (setq dead (+ dead xdead)))))
117 (insert (format "\nTotal in lisp objects: %s (live %s, dead %s)\n\n"
118 (memory-usage-format (+ dead live))
119 (memory-usage-format live)
120 (memory-usage-format dead))))
123 (format "Buffer ralloc memory usage:\n%d buffers\n%s total (%s in gaps)\n"
126 (apply #'+ (mapcar #'memory-usage-buffer-total-bytes bufs)))
128 (apply #'+ (mapcar #'memory-usage-buffer-gap-bytes bufs)))))
129 (insert (format "%10s\t%s\t%s\n\n" "Size" "Gap" "Name"))
132 (format "%10d\t%s\t%s"
133 (memory-usage-buffer-size-bytes b)
134 (memory-usage-buffer-gap-bytes b)
136 (sort bufs (lambda (b1 b2)
137 (> (memory-usage-buffer-size-bytes b1)
138 (memory-usage-buffer-size-bytes b2))))
141 (goto-char (point-min)))
143 (defun memory-usage-find-large-variables ()
144 "Find variables whose printed representation takes over 100KB."
146 (let ((min-size (* 100 1024)))
147 (pop-to-buffer "*Memory Explorer*")
148 (delete-region (point-min) (point-max))
149 ;; First find large global variables.
152 (let ((size (or (and (boundp sym)
153 (length (prin1-to-string (symbol-value sym))))
155 (when (> size min-size)
156 (insert (format "%d\tGlobal\t%s\n"
158 (symbol-name sym)))))))
159 ;; Second find large buffer-local variables.
163 (with-current-buffer buffer
166 (let ((size (or (and (consp var-cons)
167 (length (prin1-to-string (cdr var-cons))))
169 (if (> size min-size)
170 (setq holder (format "%d\t%s\t%s\n"
171 size (buffer-name buffer)
172 (symbol-name (car var-cons)))))))
173 (buffer-local-variables)))
176 (sort-numeric-fields 1 (point-min) (point-max))))
178 (provide 'memory-usage)
179 ;;; memory-usage.el ends here