Debian Codegraph @

src/doc/old-manual/memory.xmlf

raw
1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE book [
3 <!ENTITY % eclent SYSTEM "ecl.ent">
4 %eclent;
5 ]>
6 <book xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en">
7 <chapter xml:id="ext.memory">
8 <title>Memory Management</title>
9
10 <section xml:id="ext.memory.intro">
11 <title>Introduction</title>
12
13 <para>&ECL; relies on the Boehm-Weiser garbage collector for handling
14 memory, creating and destroying objects, and handling finalization of
15 objects that are no longer reachable. The use of a garbage collector, and in
16 particular the use of a portable one, imposes certain restrictions that may
17 appear odd for C/C++ programmers.</para>
18
19 <para>In this section we will discuss garbage collection, how &ECL;
20 configures and uses the memory management library, what users may expect,
21 how to handle the memory and how to control the process by which objects are
22 deleted.</para>
23
24 </section>
25
26 <section xml:id="ext.memory.boehm">
27 <title>Boehm-Weiser garbage collector</title>
28
29 <para>First of all, the garbage collector must be able to determine which
30 objects are alive and which are not. In other words, the collector must able
31 to find all references to an object. One possiblity would be to know where
32 all variables of a program reside, and where is the stack of the program and
33 its size, and parse all data there, discriminating references to lisp
34 objects. To do this precisely one would need a very precise control of the
35 data and stack segments, as well as how objects are laid out by the C
36 compiler. This is beyond &ECL;'s scope and wishes and it can make
37 coexistence with other libraries (C++, Fortran, etc) difficult.</para>
38
39 <para>The Boehm-Weiser garbage collector, on the other hand, is a
40 conservative garbage collector. When scanning memory looking for references
41 to live data, it guesses, conservatively, whether a word is a pointer or
42 not. In case of doubt it will consider it to be a pointer and add it to the
43 list of live objects. This may cause certain objects to be retained longer
44 than what an user might expect but, in our experience, this is the best of
45 both worlds and &ECL; uses certain strategies to minimize the amount of
46 misinterpreted data.</para>
47
48 <para>More precisely, &ECL; uses the garbage collector with the following
49 settings:</para>
50 <itemizedlist>
51 <listitem><para>The collector will not scan the data sectors. If you embed
52 &ECL; in another program, or link libraries with &ECL;, you will have to
53 notify &ECL; which variables point to lisp objects.</para></listitem>
54
55 <listitem><para>The collector is configured to ignore pointers that point
56 to the middle of allocated objects. This minimizes the risk of
57 misinterpreting integers as pointers to live obejcts.</para></listitem>
58
59 <listitem><para>It is possible to register finalizers that are invoked when
60 an object is destroyed, but for that you should use &ECL;'s API and
61 understand the restriction described later in <xref
62 linkend="ext.memory.finalization" xrefstyle="select: label"/></para></listitem>
63 </itemizedlist>
64
65 <para>Except for finalization, which is a questionable feature, the previous
66 settings are not very relevant for &CommonLisp; programmers, but are crucial
67 for people interested in embedding in or cooperating with other C, C++ or
68 Fortran libraries. Care should be taken when manipulating directly the GC
69 library to avoid interfering with &ECL;'s expectations.</para>
70 </section>
71
72 <section xml:id="ext.memory.limits">
73 <title>Memory limits</title>
74
75 <para>Beginning with version 9.2.1, &ECL; operates a tighter control of the
76 resources it uses. In particular, it features explicit limits in the four
77 stacks and in the amount of live data. These limits are optional, can be
78 changed at run time, but they allow users to better control the
79 evolution of a program, handling memory and stack overflow gracefully via
80 the &CommonLisp; condition system.</para>
81
82 <para>The customizable limits are listed in <xref linkend="table.memory.limits" xrefstyle="select: label"/>, but they need a careful description.</para>
83 <itemizedlist>
84 <listitem><para><varname>ext:heap-size</varname> limits the total amount of
85 memory which is available for lisp objects. This is the memory used when
86 you create conses, arrays, structures, etc.</para></listitem>
87
88 <listitem><para><varname>ext:c-stack</varname> controls the size of the
89 stack for compiled code, including &ECL;'s library itself. This limit is
90 less stringent than the others. For instance, when code is compiled with
91 low safety settings, checks for this stack limit are usually omitted, for
92 performance reasons.</para></listitem>
93
94 <listitem><para><varname>ext:binding-stack</varname> controls the number of
95 nested bindings for special variables. The current value is usually safe
96 enough, unless you have deep recursive functions that bind special
97 variables, which is not really a good idea.</para></listitem>
98
99 <listitem><para><varname>ext:frame-stack</varname> controls the number of
100 nested blocks, tagbody and other control structures. It affects both
101 interpreted and compiled code, but quite often compiled code optimizes away
102 these stack frames, saving memory and not being affected by this
103 limit.</para></listitem>
104
105 <listitem><para><varname>ext:lisp-stack</varname> controls the size of the
106 interpreter stack. It only affects interpreted code.</para></listitem>
107 </itemizedlist>
108
109 <para>If you look at <xref linkend="table.memory.limits" xrefstyle="select:
110 label"/>, some of these limits may seem very stringent, but they exist to
111 allow detecting and correcting both stack and memory overflow
112 conditions. Larger values can be set systematically either in the
113 <filename>~/.eclrc</filename> initialization file, or using the command line
114 options from the table.</para>
115
116 </section>
117
118 <section xml:id="ext.memory.conditions">
119 <title>Memory Conditions</title>
120
121 <para>When &ECL; surpasses or approaches the memory limits it will signal a
122 &CommonLisp; condition. There are two types of conditions, <link
123 linkend="ref.memory.stack-overflow"><symbol>ext:stack-overflow</symbol></link>
124 and <link
125 linkend="ref.memory.storage-exhausted"><symbol>ext:storage-exhausted</symbol></link>,
126 for stack and heap overflows, respectively. Both errors are correctable, as
127 the following session shows:</para>
128 <programlisting>
129 > (defun foo (x) (foo x))
130
131 FOO
132 > (foo 1)
133 C-STACK overflow at size 1654784. Stack can probably be resized.
134 Broken at SI:BYTECODES.Available restarts:
135 1. (CONTINUE) Extend stack size
136 Broken at FOO.
137 >> :r1
138 C-STACK overflow at size 2514944. Stack can probably be resized.
139 Broken at SI:BYTECODES.Available restarts:
140 1. (CONTINUE) Extend stack size
141 Broken at FOO.
142 >> :q
143 Top level.
144 </programlisting>
145 </section>
146
147 <section xml:id="ext.memory.finalization">
148 <title>Finalization</title>
149
150 <para>As we all know, Common-Lisp relies on garbage collection for deleting
151 unreachable objects. However, it makes no provision for the equivalent of a
152 C++ Destructor function that should be called when the object is eliminated
153 by the garbage collector. The equivalent of such methods in a garbage
154 collected environment is normally called a <emphasis>finalizer</emphasis>.</para>
155
156 <para>&ECL; includes a simple implementation of finalizers which makes the
157 following promises.</para>
158 <itemizedlist>
159
160 <listitem><para>The finalizer can be any lisp function, let it be compiled
161 or interpreter.</para></listitem>
162
163 <listitem><para>Finalizers are not invoked during garbage
164 collection. Instead, if an unreachable object is found to have an
165 associated finalizer, it is pushed into a list and <emphasis>before the
166 next garbage collection cycle</emphasis>, the finalizer will be
167 invoked. </para></listitem>
168
169 <listitem><para>If the finalizer is invoked and it makes the object
170 reachable, for instance, by assigning it to a variable, it will not be
171 destroyed, but it will have no longer a finalizer associated to
172 it.</para></listitem>
173
174 <listitem><para>&ECL; will strive to call finalizers before the environment
175 is closed and the program is finished, but this mechanism may fail when
176 exiting in a non ordinary way.</para></listitem>
177 </itemizedlist>
178
179 <para>The implementation is based on two functions, <link
180 linkend="ref.memory.set-finalizer"><symbol>ext:set-finalizer</symbol></link>
181 and <link
182 linkend="ref.memory.get-finalizer"><symbol>ext:get-finalizer</symbol></link>,
183 which allow setting and querying the finalizer functions for certain
184 objects.</para>
185 </section>
186
187 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="ref_memory.xmlf" xpointer="ext.memory.dict"/>
188
189 </chapter>
190 </book>
191 <!-- Keep this comment at the end of the file
192 Local variables:
193 mode: nxml
194 sgml-parent-document: "ecl.xml"
195 sgml-indent-step: 1
196 nxml-child-indent: 1
197 nxml-outline-child-indent: 1
198 fill-column: 79
199 End:
200 -->