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))))
60 (defun memory-usage ()
61 "List all buffers and their memory usage."
63 (pop-to-buffer (get-buffer-create "*Buffer Details*"))
65 (let* ((bufs (buffer-list))
67 (gc-stats (garbage-collect))
68 (conses (memory-usage-mult-cons 2 (nth 0 gc-stats)))
69 (symbols (memory-usage-mult-cons 6 (nth 1 gc-stats)))
70 (markers (memory-usage-mult-cons 5 (nth 2 gc-stats)))
71 (chars (nth 3 gc-stats))
72 (vectors (nth 4 gc-stats))
73 (floats (memory-usage-mult-cons 2 (nth 5 gc-stats)))
74 (intervals (memory-usage-mult-cons 7 (nth 6 gc-stats)))
75 (strings (memory-usage-mult-cons 4 (nth 7 gc-stats))))
76 (insert (format "Garbage collection stats:\n%s\n\n =>" gc-stats))
77 (insert (format "\t%d+%d bytes in cons cells\n" (car conses) (cdr conses)))
78 (insert (format "\t%d+%d bytes in symbols\n" (car symbols) (cdr symbols)))
79 (insert (format "\t%d+%d bytes in markers\n" (car markers) (cdr markers)))
80 (insert (format "\t%d+%d bytes in floats\n" (car floats) (cdr floats)))
81 (insert (format "\t%d+%d bytes in intervals\n"
82 (car intervals) (cdr intervals)))
83 (insert (format "\t%d+%d bytes in string headers\n"
84 (car strings) (cdr strings)))
85 (insert (format "\t%d bytes of string chars\n" chars))
86 (insert (format "\t%d bytes of vector slots\n" vectors))
87 (let ((live (+ (car conses)
102 (insert (format "\nTotal bytes in lisp objects: %d (live %d, dead %d)\n\n"
103 (+ dead live) live dead)))
105 (insert (format "Buffer ralloc memory usage:\n%d buffers\n%d bytes total (%d in gaps)\n"
107 (apply #'+ (mapcar #'memory-usage-buffer-total-bytes bufs))
108 (apply #'+ (mapcar #'memory-usage-buffer-gap-bytes bufs))))
109 (insert (format "%10s\t%s\t%s\n\n" "Size" "Gap" "Name"))
112 (format "%10d\t%s\t%s"
113 (memory-usage-buffer-size-bytes b)
114 (memory-usage-buffer-gap-bytes b)
116 (sort bufs (lambda (b1 b2)
117 (> (memory-usage-buffer-size-bytes b1)
118 (memory-usage-buffer-size-bytes b2))))
121 (goto-char (point-min)))
124 (provide 'memory-usage)
125 ;; arch-tag: 04e012f0-3c59-4319-8d1a-e86204671ec5
126 ;;; memory-usage.el ends here