unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: joakim@verona.se
To: Lars Magne Ingebrigtsen <larsi@gnus.org>
Cc: Eli Zaretskii <eliz@gnu.org>,
	Stefan Monnier <monnier@iro.umontreal.ca>,
	emacs-devel@gnu.org
Subject: Re: Memory leak due to bidi?
Date: Tue, 02 Aug 2011 22:37:38 +0200	[thread overview]
Message-ID: <m3wrevv9bh.fsf@verona.se> (raw)
In-Reply-To: <m3aabrzi12.fsf@stories.gnus.org> (Lars Magne Ingebrigtsen's message of "Tue, 02 Aug 2011 22:15:53 +0200")

Lars Magne Ingebrigtsen <larsi@gnus.org> writes:

> joakim@verona.se writes:
>
>> memory-usage.el makes it human readable.
>
> Is that something that could be put into GNU ELPA?
>
>> I used to have lots of memory issues. At the time I made the following
>> notes:
>>
>> - create a memory meter in the modeline, then it would at least be
>>   possible to see when memory is consumed, and maybe get a clue.
>
> That sounds useful.


gnu elpa would be a good place:

;;; memory-usage.el --- Analyze the memory usage of Emacs in various ways

;; Copyright (C) 2002, 2004  Free Software Foundation, Inc.

;; Author: Stefan Monnier <address@bogus.example.com>
;; Keywords: maint

;;  minor mods by joakim verona

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; 

;;; Code:

(defun buffer-size-bytes (b)
  "Return total number of bytes in the buffer contents."
  (with-current-buffer b
    (save-restriction
      (widen)
      (- (position-bytes (point-max)) (position-bytes (point-min))))))

(defun buffer-gap-bytes (b)
  "Return total number of bytes in the buffer gap."
  (with-current-buffer b
    (gap-size)))

(defun buffer-total-bytes (b)
  "Return total number of ralloc bytes used by buffer."
  (with-current-buffer b
    (save-restriction
      (widen) 
      (+ (position-bytes (point-max))
         (- (position-bytes (point-min)))
         (gap-size)))))

;;;###autoload
(defun memory-usage ()
  "List all buffers and their memory usage."
  (interactive)
  (pop-to-buffer (get-buffer-create "*Buffer Details*"))
  (erase-buffer)
  (let* ((bufs (buffer-list))
         (num (length bufs))
         (gc-stats (garbage-collect))
         (conses (nth 0 gc-stats))
         (symbols (nth 1 gc-stats))
         (markers (nth 2 gc-stats))
         (strings (nth 3 gc-stats))
         (vectors (nth 4 gc-stats))
         (floats (nth 5 gc-stats))
         (intervals (nth 6 gc-stats))
         (lisptotal (+ (* 8 (+ (car conses) (cdr conses)))
                       (* 24 (+ (car symbols) (cdr symbols)))
                       (* 20 (+ (car markers) (cdr markers)))
                       strings
                       vectors
                       (* 12 (+ (car floats) (cdr floats)))
                       (* 28 (+ (car intervals) (cdr intervals)))))
         (buffertotalbytes (apply #'+ (mapcar #'buffer-total-bytes bufs)))
         )
    
    (insert (format "Garbage collection stats:\n%s\n\n =>" gc-stats))
    (insert (format "\t%d bytes in cons cells\n" (* 8 (+ (car conses) (cdr 
conses)))))
    (insert (format "\t%d bytes in symbols\n" (* 24 (+ (car symbols) (cdr 
symbols)))))
    (insert (format "\t%d bytes in markers\n" (* 20 (+ (car markers) (cdr 
markers)))))
    (insert (format "\t%d bytes of string chars\n" strings))
    (insert (format "\t%d bytes of vector slots\n" (* 4 vectors)))
    (insert (format "\t%d bytes in floats\n" (* 12 (+ (car floats) (cdr 
floats)))))
    (insert (format "\t%d bytes in intervals\n" (* 28 (+ (car intervals) (cdr 
intervals)))))

    (insert (format "\nTotal bytes in lisp objects (not counting string and 
vector headers): %d\n\n"
                    lisptotal))

    (insert (format "Buffer ralloc memory usage:\n%d buffers\n%d bytes total (%d in gaps)\n"
                    num
                    buffertotalbytes
                    (apply #'+ (mapcar #'buffer-gap-bytes bufs))))
    (insert (format "%10s\t%s\t%s\n\n" "Size" "Gap" "Name"))
    (insert (mapconcat
            (lambda (b)
               (format "%10d\t%s\t%s"
                       (buffer-size-bytes b)
                       (buffer-gap-bytes b)
                       (buffer-name b)))
             (sort bufs (lambda (b1 b2)
                          (> (buffer-size-bytes b1) (buffer-size-bytes b2))))
             "\n"))
    (insert "\n")
    (insert (format "total:%s" (+ lisptotal buffertotalbytes)))

    (insert "\n"))
  (goto-char (point-min)))

(provide 'memory-usage)
;; arch-tag: 04e012f0-3c59-4319-8d1a-e86204671ec5
;;; memory-usage.el ends here

-- 
Joakim Verona



  reply	other threads:[~2011-08-02 20:37 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-02  4:42 Memory leak due to bidi? Eli Zaretskii
2011-08-02 19:12 ` Stefan Monnier
2011-08-02 19:16   ` Lars Magne Ingebrigtsen
2011-08-02 19:24     ` Andreas Schwab
2011-08-02 19:45       ` Lars Magne Ingebrigtsen
2011-08-02 20:12     ` joakim
2011-08-02 20:15       ` Lars Magne Ingebrigtsen
2011-08-02 20:37         ` joakim [this message]
2011-08-02 19:42   ` Andreas Röhler
2011-08-02 20:49   ` James Cloos
2011-08-03  1:10     ` Stefan Monnier
2011-08-03 13:30       ` Eli Zaretskii
2011-08-04 19:22         ` Antoine Levitt
2011-08-05  6:10           ` Eli Zaretskii
2011-08-03  4:29     ` Kenichi Handa
2011-08-03 13:16       ` Óscar Fuentes
2011-08-03 13:47         ` Eli Zaretskii
2011-08-04 16:00           ` Antoine Levitt
2011-08-04 16:30             ` Eli Zaretskii
2011-08-04 17:04               ` Antoine Levitt
2011-08-04 17:48                 ` Eli Zaretskii
2011-08-05  3:43                 ` Stephen J. Turnbull
2011-08-04  0:48         ` Kenichi Handa
2011-08-03 13:26       ` Andy Moreton
2011-08-03 13:44         ` Eli Zaretskii
2011-08-03 14:18           ` Stefan Monnier
2011-08-03 15:49             ` Andy Moreton
2011-08-03 16:16               ` Eli Zaretskii
2011-08-03 16:11             ` Eli Zaretskii
2011-08-03 16:01           ` Andy Moreton
2011-08-03 16:38             ` Eli Zaretskii
2011-08-04 23:15               ` Andy Moreton
2011-08-04  3:28             ` Stephen J. Turnbull
2011-08-04  5:15               ` Stefan Monnier
2011-08-04  5:19                 ` Eli Zaretskii
2011-08-04  9:23                 ` Stephen J. Turnbull

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m3wrevv9bh.fsf@verona.se \
    --to=joakim@verona.se \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=larsi@gnus.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).