Skip to content

Instantly share code, notes, and snippets.

@vapniks
Created June 2, 2024 13:21
Show Gist options
  • Save vapniks/542a3c642e6fa459d7ed3e719d9aabf7 to your computer and use it in GitHub Desktop.
Save vapniks/542a3c642e6fa459d7ed3e719d9aabf7 to your computer and use it in GitHub Desktop.
Estimate elisp object memory usage
;; This code was created by chatgpt
(defun estimate-list-size (list)
"Estimate the memory size of LIST in bytes."
(let ((size 0))
(while list
(setq size (+ size (estimate-object-size (car list))))
(setq size (+ size 8)) ; Approximate overhead for cons cell
(setq list (cdr list)))
size))
(defun estimate-string-size (string)
"Estimate the memory size of STRING in bytes."
(string-bytes string))
(defun estimate-buffer-size (buffer)
"Estimate the memory size of BUFFER in bytes."
(with-current-buffer buffer
(buffer-size)))
(defun estimate-object-size (obj)
"Estimate the memory size of OBJ in bytes."
(cond
((stringp obj) (estimate-string-size obj))
((listp obj) (estimate-list-size obj))
((bufferp obj) (estimate-buffer-size obj))
;; Add more cases as needed
(t 0))) ; Default case for unsupported types
;; Example usage:
(estimate-object-size "This is a test string") ; Estimate size of a string
(estimate-object-size '(1 2 3 4)) ; Estimate size of a list
(estimate-object-size (current-buffer)) ; Estimate size of the current buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment