Next: garbage_collect_1, Up: Garbage Collection - Step by Step [Contents][Index]
The first thing that anyone should know about garbage collection is: when and how the garbage collector is invoked. One might think that this could happen every time new memory is allocated, e.g. new objects are created, but this is not the case. Instead, we have the following situation:
The entry point of any process of garbage collection is an invocation
of the function garbage_collect_1
in file alloc.c
. The
invocation can occur explicitly by calling the function
Fgarbage_collect
(in addition this function provides information
about the freed memory), or can occur implicitly in four different
situations:
main_1
in file emacs.c
. This function is called
at each startup of xemacs. The garbage collection is invoked after all
initial creations are completed, but only if a special internal error
checking-constant ERROR_CHECK_GC
is defined.
disksave_object_finalization
in file
alloc.c
. The only purpose of this function is to clear the
objects from memory which need not be stored with xemacs when we dump out
an executable. This is only done by Fdump_emacs
or by
Fdump_emacs_data
respectively (both in emacs.c
). The
actual clearing is accomplished by making these objects unreachable and
starting a garbage collection. The function is only used while building
xemacs.
Feval / eval
in file eval.c
. Each time the
well known and often used function eval is called to evaluate a form,
one of the first things that could happen, is a potential call of
garbage_collect_1
. There exist three global variables,
consing_since_gc
(counts the created cons-cells since the last
garbage collection), gc_cons_threshold
(a specified threshold
after which a garbage collection occurs) and always_gc
. If
always_gc
is set or if the threshold is exceeded, the garbage
collection will start.
Ffuncall / funcall
in file eval.c
. This
function evaluates calls of elisp functions and works according to
Feval
.
The upshot is that garbage collection can basically occur everywhere
Feval
, respectively Ffuncall
, is used - either directly or
through another function. Since calls to these two functions are hidden
in various other functions, many calls to garbage_collect_1
are
not obviously foreseeable, and therefore unexpected. Instances where
they are used that are worth remembering are various elisp commands, as
for example or
, and
, if
, cond
, while
,
setq
, etc., miscellaneous gui_item_...
functions,
everything related to eval
(Feval_buffer
, call0
,
...) and inside Fsignal
. The latter is used to handle signals, as
for example the ones raised by every QUIT
-macro triggered after
pressing Ctrl-g.
Next: garbage_collect_1, Up: Garbage Collection - Step by Step [Contents][Index]