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 (conses (memory-usage-mult-cons 2 (nth 0 gc-stats)))
82 (symbols (memory-usage-mult-cons 6 (nth 1 gc-stats)))
83 (markers (memory-usage-mult-cons 5 (nth 2 gc-stats)))
84 (chars (nth 3 gc-stats))
85 (vectors (nth 4 gc-stats))
86 (floats (memory-usage-mult-cons 2 (nth 5 gc-stats)))
87 (intervals (memory-usage-mult-cons 7 (nth 6 gc-stats)))
88 (strings (memory-usage-mult-cons 4 (nth 7 gc-stats))))
89 (if (consp vectors) (setq vectors (cdr vectors)))
90 (insert (format "Garbage collection stats:\n%s\n\n =>" gc-stats))
91 (dolist (x `(("cons cells" . ,conses)
92 ("symbols" . ,symbols)
93 ("markers" . ,markers)
95 ("intervals" . ,intervals)
96 ("string headers" . ,strings)))
97 (insert (format "\t%s (+ %s dead) in %s\n"
98 (memory-usage-format (cadr x))
99 (memory-usage-format (cddr x))
101 (insert (format "\t%s of string chars\n" (memory-usage-format chars)))
102 (insert (format "\t%s of vector slots\n" (memory-usage-format vectors)))
103 (let ((live (+ (car conses)
111 (dead (+ (cdr conses)
118 (insert (format "\nTotal in lisp objects: %s (live %s, dead %s)\n\n"
119 (memory-usage-format (+ dead live))
120 (memory-usage-format live)
121 (memory-usage-format dead))))
124 (format "Buffer ralloc memory usage:\n%d buffers\n%s total (%s in gaps)\n"
127 (apply #'+ (mapcar #'memory-usage-buffer-total-bytes bufs)))
129 (apply #'+ (mapcar #'memory-usage-buffer-gap-bytes bufs)))))
130 (insert (format "%10s\t%s\t%s\n\n" "Size" "Gap" "Name"))
133 (format "%10d\t%s\t%s"
134 (memory-usage-buffer-size-bytes b)
135 (memory-usage-buffer-gap-bytes b)
137 (sort bufs (lambda (b1 b2)
138 (> (memory-usage-buffer-size-bytes b1)
139 (memory-usage-buffer-size-bytes b2))))
142 (goto-char (point-min)))
144 (defun memory-usage-find-large-variables ()
145 "Find variables whose printed representation takes over 100KB."
147 (let ((min-size (* 100 1024)))
148 (pop-to-buffer "*Memory Explorer*")
149 (delete-region (point-min) (point-max))
150 ;; First find large global variables.
153 (let ((size (or (and (boundp sym)
154 (length (prin1-to-string (symbol-value sym))))
156 (when (> size min-size)
157 (insert (format "%d\tGlobal\t%s\n"
159 (symbol-name sym)))))))
160 ;; Second find large buffer-local variables.
164 (with-current-buffer buffer
167 (let ((size (or (and (consp var-cons)
168 (length (prin1-to-string (cdr var-cons))))
170 (if (> size min-size)
171 (setq holder (format "%d\t%s\t%s\n"
172 size (buffer-name buffer)
173 (symbol-name (car var-cons)))))))
174 (buffer-local-variables)))
177 (sort-numeric-fields 1 (point-min) (point-max))))
179 (provide 'memory-usage)
180 ;;; memory-usage.el ends here