diff options
| author | Stefan Monnier | 2026-01-13 13:41:55 -0500 |
|---|---|---|
| committer | Stefan Monnier | 2026-01-20 16:09:22 -0500 |
| commit | 4150c2e22e93ca6bdf682b0067d430e412db3688 (patch) | |
| tree | d6679363f7600845facc7d6711440c1bd82c5887 /src/alloc.c | |
| parent | 9ba2f13176a756030e4d8476436662d630447f65 (diff) | |
| download | emacs-4150c2e22e93ca6bdf682b0067d430e412db3688.tar.gz emacs-4150c2e22e93ca6bdf682b0067d430e412db3688.zip | |
(garbage-collect-heapsize): New function
The info returned from `garbage-collect` is really handy to
track the evolution of the heap size, but sadly it's available only
at the cost of running a full GC, which has two big downsides:
it's slow, it affects what we're measuring, and it can't be used
in `post-gc-hook`.
So, this patch makes it available without running the GC.
* src/alloc.c (Fgarbage_collect_heapsize): New function, extracted from
`Fgarbage_collect`.
(Fgarbage_collect): Use it.
(syms_of_alloc): defsubr it.
* doc/lispref/internals.texi (Garbage Collection): Extract
documentation for it from that of `garbage-collect`.
Diffstat (limited to 'src/alloc.c')
| -rw-r--r-- | src/alloc.c | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/src/alloc.c b/src/alloc.c index c0e23192c0f..a4e97d7a8c3 100644 --- a/src/alloc.c +++ b/src/alloc.c | |||
| @@ -5996,14 +5996,7 @@ DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "", | |||
| 5996 | doc: /* Reclaim storage for Lisp objects no longer needed. | 5996 | doc: /* Reclaim storage for Lisp objects no longer needed. |
| 5997 | Garbage collection happens automatically if you cons more than | 5997 | Garbage collection happens automatically if you cons more than |
| 5998 | `gc-cons-threshold' bytes of Lisp data since previous garbage collection. | 5998 | `gc-cons-threshold' bytes of Lisp data since previous garbage collection. |
| 5999 | `garbage-collect' normally returns a list with info on amount of space in use, | 5999 | It returns the same info as `garbage-collect-heapsize'. |
| 6000 | where each entry has the form (NAME SIZE USED FREE), where: | ||
| 6001 | - NAME is a symbol describing the kind of objects this entry represents, | ||
| 6002 | - SIZE is the number of bytes used by each one, | ||
| 6003 | - USED is the number of those objects that were found live in the heap, | ||
| 6004 | - FREE is the number of those objects that are not live but that Emacs | ||
| 6005 | keeps around for future allocations (maybe because it does not know how | ||
| 6006 | to return them to the OS). | ||
| 6007 | 6000 | ||
| 6008 | Note that calling this function does not guarantee that absolutely all | 6001 | Note that calling this function does not guarantee that absolutely all |
| 6009 | unreachable objects will be garbage-collected. Emacs uses a | 6002 | unreachable objects will be garbage-collected. Emacs uses a |
| @@ -6020,8 +6013,29 @@ For further details, see Info node `(elisp)Garbage Collection'. */) | |||
| 6020 | specbind (Qsymbols_with_pos_enabled, Qnil); | 6013 | specbind (Qsymbols_with_pos_enabled, Qnil); |
| 6021 | garbage_collect (); | 6014 | garbage_collect (); |
| 6022 | unbind_to (count, Qnil); | 6015 | unbind_to (count, Qnil); |
| 6016 | return Fgarbage_collect_heapsize (); | ||
| 6017 | } | ||
| 6018 | |||
| 6019 | DEFUN ("garbage-collect-heapsize", Fgarbage_collect_heapsize, | ||
| 6020 | Sgarbage_collect_heapsize, 0, 0, 0, | ||
| 6021 | doc: /* Return a list with info on amount of space in use. | ||
| 6022 | This info may not be fully up to date unless it is called right after | ||
| 6023 | a full garbage collection cycle. | ||
| 6024 | Each entry has the form (NAME SIZE USED FREE), where: | ||
| 6025 | - NAME is a symbol describing the kind of objects this entry represents, | ||
| 6026 | - SIZE is the number of bytes used by each one, | ||
| 6027 | - USED is the number of those objects that were found live in the heap, | ||
| 6028 | - FREE is the number of those objects that are not live but that Emacs | ||
| 6029 | keeps around for future allocations (maybe because it does not know how | ||
| 6030 | to return them to the OS). */) | ||
| 6031 | () | ||
| 6032 | { | ||
| 6023 | struct gcstat gcst = gcstat; | 6033 | struct gcstat gcst = gcstat; |
| 6024 | 6034 | ||
| 6035 | /* FIXME: Maybe we could/should add a field countaing the approximate | ||
| 6036 | amount of memory allocated since the last GC, such as | ||
| 6037 | 'gc_threshold - consing_until_gc'. */ | ||
| 6038 | |||
| 6025 | Lisp_Object total[] = { | 6039 | Lisp_Object total[] = { |
| 6026 | list4 (Qconses, make_fixnum (sizeof (struct Lisp_Cons)), | 6040 | list4 (Qconses, make_fixnum (sizeof (struct Lisp_Cons)), |
| 6027 | make_int (gcst.total_conses), | 6041 | make_int (gcst.total_conses), |
| @@ -7512,6 +7526,7 @@ N should be nonnegative. */); | |||
| 7512 | defsubr (&Smake_finalizer); | 7526 | defsubr (&Smake_finalizer); |
| 7513 | defsubr (&Sgarbage_collect); | 7527 | defsubr (&Sgarbage_collect); |
| 7514 | defsubr (&Sgarbage_collect_maybe); | 7528 | defsubr (&Sgarbage_collect_maybe); |
| 7529 | defsubr (&Sgarbage_collect_heapsize); | ||
| 7515 | defsubr (&Smemory_info); | 7530 | defsubr (&Smemory_info); |
| 7516 | defsubr (&Smemory_use_counts); | 7531 | defsubr (&Smemory_use_counts); |
| 7517 | #if defined GNU_LINUX && defined __GLIBC__ && \ | 7532 | #if defined GNU_LINUX && defined __GLIBC__ && \ |