1 ;;; memory-usage.el --- Analyze the memory usage of Emacs in various ways
3 ;; Copyright (C) 2002, 2004 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
8 ;; This file is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
29 (defvar memory-usage-word-size
(ceiling (/ (log most-positive-fixnum
2) 8))
30 "Size of a Lisp word box in bytes.")
32 (defun memory-usage-buffer-size-bytes (b)
33 "Return total number of bytes in the buffer contents."
34 (with-current-buffer b
37 (- (position-bytes (point-max)) (position-bytes (point-min))))))
39 (defun memory-usage-buffer-gap-bytes (b)
40 "Return total number of bytes in the buffer gap."
41 (with-current-buffer b
44 (defun memory-usage-buffer-total-bytes (b)
45 "Return total number of ralloc bytes used by buffer."
46 (with-current-buffer b
49 (+ (position-bytes (point-max))
50 (- (position-bytes (point-min)))
53 (defun memory-usage-mult-cons (n c
)
54 (setq n
(* n memory-usage-word-size
))
55 (cons (* n
(car c
)) (* n
(cdr c
))))
58 (defun memory-usage ()
59 "List all buffers and their memory usage."
61 (pop-to-buffer (get-buffer-create "*Buffer Details*"))
63 (let* ((bufs (buffer-list))
65 (gc-stats (garbage-collect))
66 (conses (memory-usage-mult-cons 2 (nth 0 gc-stats
)))
67 (symbols (memory-usage-mult-cons 6 (nth 1 gc-stats
)))
68 (markers (memory-usage-mult-cons 5 (nth 2 gc-stats
)))
69 (chars (nth 3 gc-stats
))
70 (vectors (nth 4 gc-stats
))
71 (floats (memory-usage-mult-cons 2 (nth 5 gc-stats
)))
72 (intervals (memory-usage-mult-cons 7 (nth 6 gc-stats
)))
73 (strings (memory-usage-mult-cons 4 (nth 7 gc-stats
))))
74 (insert (format "Garbage collection stats:\n%s\n\n =>" gc-stats
))
75 (insert (format "\t%d+%d bytes in cons cells\n" (car conses
) (cdr conses
)))
76 (insert (format "\t%d+%d bytes in symbols\n" (car symbols
) (cdr symbols
)))
77 (insert (format "\t%d+%d bytes in markers\n" (car markers
) (cdr markers
)))
78 (insert (format "\t%d+%d bytes in floats\n" (car floats
) (cdr floats
)))
79 (insert (format "\t%d+%d bytes in intervals\n" (car intervals
) (cdr intervals
)))
80 (insert (format "\t%d+%d bytes in string headers\n" (car strings
) (cdr strings
)))
81 (insert (format "\t%d bytes of string chars\n" chars
))
82 (insert (format "\t%d bytes of vector headers\n" (* 2 memory-usage-word-size
(car vectors
))))
83 (insert (format "\t%d bytes of vector slots\n" (* memory-usage-word-size
(cdr vectors
))))
85 (let ((live (+ (car conses
)
92 (* 2 memory-usage-word-size
(car vectors
))
93 (* memory-usage-word-size
(cdr vectors
))))
101 (insert (format "\nTotal bytes in lisp objects: %d (live %d, dead %d)\n\n"
102 (+ dead live
) live dead
)))
104 (insert (format "Buffer ralloc memory usage:\n%d buffers\n%d bytes total (%d in gaps)\n"
106 (apply #'+ (mapcar #'memory-usage-buffer-total-bytes bufs
))
107 (apply #'+ (mapcar #'memory-usage-buffer-gap-bytes bufs
))))
108 (insert (format "%10s\t%s\t%s\n\n" "Size" "Gap" "Name"))
111 (format "%10d\t%s\t%s"
112 (memory-usage-buffer-size-bytes b
)
113 (memory-usage-buffer-gap-bytes b
)
115 (sort bufs
(lambda (b1 b2
)
116 (> (memory-usage-buffer-size-bytes b1
)
117 (memory-usage-buffer-size-bytes b2
))))
120 (goto-char (point-min)))
123 (provide 'memory-usage
)
124 ;; arch-tag: 04e012f0-3c59-4319-8d1a-e86204671ec5
125 ;;; memory-usage.el ends here