Debian Codegraph @

HOWTOs/GNU-APL-Memory-Management.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
5 <head>
6 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
7 <meta name="generator" content="AsciiDoc 10.1.2" />
8 <title>Memory Management in GNU APL</title>
9 <style type="text/css">
10 /* Shared CSS for AsciiDoc xhtml11 and html5 backends */
11
12 /* Default font. */
13 body {
14 font-family: Georgia,serif;
15 }
16
17 /* Title font. */
18 h1, h2, h3, h4, h5, h6,
19 div.title, caption.title,
20 thead, p.table.header,
21 #toctitle,
22 #author, #revnumber, #revdate, #revremark,
23 #footer {
24 font-family: Arial,Helvetica,sans-serif;
25 }
26
27 body {
28 margin: 1em 5% 1em 5%;
29 }
30
31 a {
32 color: blue;
33 text-decoration: underline;
34 }
35 a:visited {
36 color: fuchsia;
37 }
38
39 em {
40 font-style: italic;
41 color: navy;
42 }
43
44 strong {
45 font-weight: bold;
46 color: #083194;
47 }
48
49 h1, h2, h3, h4, h5, h6 {
50 color: #527bbd;
51 margin-top: 1.2em;
52 margin-bottom: 0.5em;
53 line-height: 1.3;
54 }
55
56 h1, h2, h3 {
57 border-bottom: 2px solid silver;
58 }
59 h2 {
60 padding-top: 0.5em;
61 }
62 h3 {
63 float: left;
64 }
65 h3 + * {
66 clear: left;
67 }
68 h5 {
69 font-size: 1.0em;
70 }
71
72 div.sectionbody {
73 margin-left: 0;
74 }
75
76 hr {
77 border: 1px solid silver;
78 }
79
80 p {
81 margin-top: 0.5em;
82 margin-bottom: 0.5em;
83 }
84
85 ul, ol, li > p {
86 margin-top: 0;
87 }
88 ul > li { color: #aaa; }
89 ul > li > * { color: black; }
90
91 .monospaced, code, pre {
92 font-family: "Courier New", Courier, monospace;
93 font-size: inherit;
94 color: navy;
95 padding: 0;
96 margin: 0;
97 }
98 pre {
99 white-space: pre-wrap;
100 }
101
102 #author {
103 color: #527bbd;
104 font-weight: bold;
105 font-size: 1.1em;
106 }
107 #email {
108 }
109 #revnumber, #revdate, #revremark {
110 }
111
112 #footer {
113 font-size: small;
114 border-top: 2px solid silver;
115 padding-top: 0.5em;
116 margin-top: 4.0em;
117 }
118 #footer-text {
119 float: left;
120 padding-bottom: 0.5em;
121 }
122 #footer-badges {
123 float: right;
124 padding-bottom: 0.5em;
125 }
126
127 #preamble {
128 margin-top: 1.5em;
129 margin-bottom: 1.5em;
130 }
131 div.imageblock, div.exampleblock, div.verseblock,
132 div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
133 div.admonitionblock {
134 margin-top: 1.0em;
135 margin-bottom: 1.5em;
136 }
137 div.admonitionblock {
138 margin-top: 2.0em;
139 margin-bottom: 2.0em;
140 margin-right: 10%;
141 color: #606060;
142 }
143
144 div.content { /* Block element content. */
145 padding: 0;
146 }
147
148 /* Block element titles. */
149 div.title, caption.title {
150 color: #527bbd;
151 font-weight: bold;
152 text-align: left;
153 margin-top: 1.0em;
154 margin-bottom: 0.5em;
155 }
156 div.title + * {
157 margin-top: 0;
158 }
159
160 td div.title:first-child {
161 margin-top: 0.0em;
162 }
163 div.content div.title:first-child {
164 margin-top: 0.0em;
165 }
166 div.content + div.title {
167 margin-top: 0.0em;
168 }
169
170 div.sidebarblock > div.content {
171 background: #ffffee;
172 border: 1px solid #dddddd;
173 border-left: 4px solid #f0f0f0;
174 padding: 0.5em;
175 }
176
177 div.listingblock > div.content {
178 border: 1px solid #dddddd;
179 border-left: 5px solid #f0f0f0;
180 background: #f8f8f8;
181 padding: 0.5em;
182 }
183
184 div.quoteblock, div.verseblock {
185 padding-left: 1.0em;
186 margin-left: 1.0em;
187 margin-right: 10%;
188 border-left: 5px solid #f0f0f0;
189 color: #888;
190 }
191
192 div.quoteblock > div.attribution {
193 padding-top: 0.5em;
194 text-align: right;
195 }
196
197 div.verseblock > pre.content {
198 font-family: inherit;
199 font-size: inherit;
200 }
201 div.verseblock > div.attribution {
202 padding-top: 0.75em;
203 text-align: left;
204 }
205 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
206 div.verseblock + div.attribution {
207 text-align: left;
208 }
209
210 div.admonitionblock .icon {
211 vertical-align: top;
212 font-size: 1.1em;
213 font-weight: bold;
214 text-decoration: underline;
215 color: #527bbd;
216 padding-right: 0.5em;
217 }
218 div.admonitionblock td.content {
219 padding-left: 0.5em;
220 border-left: 3px solid #dddddd;
221 }
222
223 div.exampleblock > div.content {
224 border-left: 3px solid #dddddd;
225 padding-left: 0.5em;
226 }
227
228 div.imageblock div.content { padding-left: 0; }
229 span.image img { border-style: none; vertical-align: text-bottom; }
230 a.image:visited { color: white; }
231
232 dl {
233 margin-top: 0.8em;
234 margin-bottom: 0.8em;
235 }
236 dt {
237 margin-top: 0.5em;
238 margin-bottom: 0;
239 font-style: normal;
240 color: navy;
241 }
242 dd > *:first-child {
243 margin-top: 0.1em;
244 }
245
246 ul, ol {
247 list-style-position: outside;
248 }
249 ol.arabic {
250 list-style-type: decimal;
251 }
252 ol.loweralpha {
253 list-style-type: lower-alpha;
254 }
255 ol.upperalpha {
256 list-style-type: upper-alpha;
257 }
258 ol.lowerroman {
259 list-style-type: lower-roman;
260 }
261 ol.upperroman {
262 list-style-type: upper-roman;
263 }
264
265 div.compact ul, div.compact ol,
266 div.compact p, div.compact p,
267 div.compact div, div.compact div {
268 margin-top: 0.1em;
269 margin-bottom: 0.1em;
270 }
271
272 tfoot {
273 font-weight: bold;
274 }
275 td > div.verse {
276 white-space: pre;
277 }
278
279 div.hdlist {
280 margin-top: 0.8em;
281 margin-bottom: 0.8em;
282 }
283 div.hdlist tr {
284 padding-bottom: 15px;
285 }
286 dt.hdlist1.strong, td.hdlist1.strong {
287 font-weight: bold;
288 }
289 td.hdlist1 {
290 vertical-align: top;
291 font-style: normal;
292 padding-right: 0.8em;
293 color: navy;
294 }
295 td.hdlist2 {
296 vertical-align: top;
297 }
298 div.hdlist.compact tr {
299 margin: 0;
300 padding-bottom: 0;
301 }
302
303 .comment {
304 background: yellow;
305 }
306
307 .footnote, .footnoteref {
308 font-size: 0.8em;
309 }
310
311 span.footnote, span.footnoteref {
312 vertical-align: super;
313 }
314
315 #footnotes {
316 margin: 20px 0 20px 0;
317 padding: 7px 0 0 0;
318 }
319
320 #footnotes div.footnote {
321 margin: 0 0 5px 0;
322 }
323
324 #footnotes hr {
325 border: none;
326 border-top: 1px solid silver;
327 height: 1px;
328 text-align: left;
329 margin-left: 0;
330 width: 20%;
331 min-width: 100px;
332 }
333
334 div.colist td {
335 padding-right: 0.5em;
336 padding-bottom: 0.3em;
337 vertical-align: top;
338 }
339 div.colist td img {
340 margin-top: 0.3em;
341 }
342
343 @media print {
344 #footer-badges { display: none; }
345 }
346
347 #toc {
348 margin-bottom: 2.5em;
349 }
350
351 #toctitle {
352 color: #527bbd;
353 font-size: 1.1em;
354 font-weight: bold;
355 margin-top: 1.0em;
356 margin-bottom: 0.1em;
357 }
358
359 div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
360 margin-top: 0;
361 margin-bottom: 0;
362 }
363 div.toclevel2 {
364 margin-left: 2em;
365 font-size: 0.9em;
366 }
367 div.toclevel3 {
368 margin-left: 4em;
369 font-size: 0.9em;
370 }
371 div.toclevel4 {
372 margin-left: 6em;
373 font-size: 0.9em;
374 }
375
376 span.aqua { color: aqua; }
377 span.black { color: black; }
378 span.blue { color: blue; }
379 span.fuchsia { color: fuchsia; }
380 span.gray { color: gray; }
381 span.green { color: green; }
382 span.lime { color: lime; }
383 span.maroon { color: maroon; }
384 span.navy { color: navy; }
385 span.olive { color: olive; }
386 span.purple { color: purple; }
387 span.red { color: red; }
388 span.silver { color: silver; }
389 span.teal { color: teal; }
390 span.white { color: white; }
391 span.yellow { color: yellow; }
392
393 span.aqua-background { background: aqua; }
394 span.black-background { background: black; }
395 span.blue-background { background: blue; }
396 span.fuchsia-background { background: fuchsia; }
397 span.gray-background { background: gray; }
398 span.green-background { background: green; }
399 span.lime-background { background: lime; }
400 span.maroon-background { background: maroon; }
401 span.navy-background { background: navy; }
402 span.olive-background { background: olive; }
403 span.purple-background { background: purple; }
404 span.red-background { background: red; }
405 span.silver-background { background: silver; }
406 span.teal-background { background: teal; }
407 span.white-background { background: white; }
408 span.yellow-background { background: yellow; }
409
410 span.big { font-size: 2em; }
411 span.small { font-size: 0.6em; }
412
413 span.underline { text-decoration: underline; }
414 span.overline { text-decoration: overline; }
415 span.line-through { text-decoration: line-through; }
416
417 div.unbreakable { page-break-inside: avoid; }
418
419
420 /*
421 * xhtml11 specific
422 *
423 * */
424
425 div.tableblock {
426 margin-top: 1.0em;
427 margin-bottom: 1.5em;
428 }
429 div.tableblock > table {
430 border: 3px solid #527bbd;
431 }
432 thead, p.table.header {
433 font-weight: bold;
434 color: #527bbd;
435 }
436 p.table {
437 margin-top: 0;
438 }
439 /* Because the table frame attribute is overridden by CSS in most browsers. */
440 div.tableblock > table[frame="void"] {
441 border-style: none;
442 }
443 div.tableblock > table[frame="hsides"] {
444 border-left-style: none;
445 border-right-style: none;
446 }
447 div.tableblock > table[frame="vsides"] {
448 border-top-style: none;
449 border-bottom-style: none;
450 }
451
452
453 /*
454 * html5 specific
455 *
456 * */
457
458 table.tableblock {
459 margin-top: 1.0em;
460 margin-bottom: 1.5em;
461 }
462 thead, p.tableblock.header {
463 font-weight: bold;
464 color: #527bbd;
465 }
466 p.tableblock {
467 margin-top: 0;
468 }
469 table.tableblock {
470 border-width: 3px;
471 border-spacing: 0px;
472 border-style: solid;
473 border-color: #527bbd;
474 border-collapse: collapse;
475 }
476 th.tableblock, td.tableblock {
477 border-width: 1px;
478 padding: 4px;
479 border-style: solid;
480 border-color: #527bbd;
481 }
482
483 table.tableblock.frame-topbot {
484 border-left-style: hidden;
485 border-right-style: hidden;
486 }
487 table.tableblock.frame-sides {
488 border-top-style: hidden;
489 border-bottom-style: hidden;
490 }
491 table.tableblock.frame-none {
492 border-style: hidden;
493 }
494
495 th.tableblock.halign-left, td.tableblock.halign-left {
496 text-align: left;
497 }
498 th.tableblock.halign-center, td.tableblock.halign-center {
499 text-align: center;
500 }
501 th.tableblock.halign-right, td.tableblock.halign-right {
502 text-align: right;
503 }
504
505 th.tableblock.valign-top, td.tableblock.valign-top {
506 vertical-align: top;
507 }
508 th.tableblock.valign-middle, td.tableblock.valign-middle {
509 vertical-align: middle;
510 }
511 th.tableblock.valign-bottom, td.tableblock.valign-bottom {
512 vertical-align: bottom;
513 }
514
515
516 /*
517 * manpage specific
518 *
519 * */
520
521 body.manpage h1 {
522 padding-top: 0.5em;
523 padding-bottom: 0.5em;
524 border-top: 2px solid silver;
525 border-bottom: 2px solid silver;
526 }
527 body.manpage h2 {
528 border-style: none;
529 }
530 body.manpage div.sectionbody {
531 margin-left: 3em;
532 }
533
534 @media print {
535 body.manpage div#toc { display: none; }
536 }
537
538
539 </style>
540 <script type="text/javascript">
541 /*<![CDATA[*/
542 var asciidoc = { // Namespace.
543
544 /////////////////////////////////////////////////////////////////////
545 // Table Of Contents generator
546 /////////////////////////////////////////////////////////////////////
547
548 /* Author: Mihai Bazon, September 2002
549 * http://students.infoiasi.ro/~mishoo
550 *
551 * Table Of Content generator
552 * Version: 0.4
553 *
554 * Feel free to use this script under the terms of the GNU General Public
555 * License, as long as you do not remove or alter this notice.
556 */
557
558 /* modified by Troy D. Hanson, September 2006. License: GPL */
559 /* modified by Stuart Rackham, 2006, 2009. License: GPL */
560
561 // toclevels = 1..4.
562 toc: function (toclevels) {
563
564 function getText(el) {
565 var text = "";
566 for (var i = el.firstChild; i != null; i = i.nextSibling) {
567 if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
568 text += i.data;
569 else if (i.firstChild != null)
570 text += getText(i);
571 }
572 return text;
573 }
574
575 function TocEntry(el, text, toclevel) {
576 this.element = el;
577 this.text = text;
578 this.toclevel = toclevel;
579 }
580
581 function tocEntries(el, toclevels) {
582 var result = new Array;
583 var re = new RegExp('[hH]([1-'+(toclevels+1)+'])');
584 // Function that scans the DOM tree for header elements (the DOM2
585 // nodeIterator API would be a better technique but not supported by all
586 // browsers).
587 var iterate = function (el) {
588 for (var i = el.firstChild; i != null; i = i.nextSibling) {
589 if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
590 var mo = re.exec(i.tagName);
591 if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
592 result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
593 }
594 iterate(i);
595 }
596 }
597 }
598 iterate(el);
599 return result;
600 }
601
602 var toc = document.getElementById("toc");
603 if (!toc) {
604 return;
605 }
606
607 // Delete existing TOC entries in case we're reloading the TOC.
608 var tocEntriesToRemove = [];
609 var i;
610 for (i = 0; i < toc.childNodes.length; i++) {
611 var entry = toc.childNodes[i];
612 if (entry.nodeName.toLowerCase() == 'div'
613 && entry.getAttribute("class")
614 && entry.getAttribute("class").match(/^toclevel/))
615 tocEntriesToRemove.push(entry);
616 }
617 for (i = 0; i < tocEntriesToRemove.length; i++) {
618 toc.removeChild(tocEntriesToRemove[i]);
619 }
620
621 // Rebuild TOC entries.
622 var entries = tocEntries(document.getElementById("content"), toclevels);
623 for (var i = 0; i < entries.length; ++i) {
624 var entry = entries[i];
625 if (entry.element.id == "")
626 entry.element.id = "_toc_" + i;
627 var a = document.createElement("a");
628 a.href = "#" + entry.element.id;
629 a.appendChild(document.createTextNode(entry.text));
630 var div = document.createElement("div");
631 div.appendChild(a);
632 div.className = "toclevel" + entry.toclevel;
633 toc.appendChild(div);
634 }
635 if (entries.length == 0)
636 toc.parentNode.removeChild(toc);
637 },
638
639
640 /////////////////////////////////////////////////////////////////////
641 // Footnotes generator
642 /////////////////////////////////////////////////////////////////////
643
644 /* Based on footnote generation code from:
645 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
646 */
647
648 footnotes: function () {
649 // Delete existing footnote entries in case we're reloading the footnodes.
650 var i;
651 var noteholder = document.getElementById("footnotes");
652 if (!noteholder) {
653 return;
654 }
655 var entriesToRemove = [];
656 for (i = 0; i < noteholder.childNodes.length; i++) {
657 var entry = noteholder.childNodes[i];
658 if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote")
659 entriesToRemove.push(entry);
660 }
661 for (i = 0; i < entriesToRemove.length; i++) {
662 noteholder.removeChild(entriesToRemove[i]);
663 }
664
665 // Rebuild footnote entries.
666 var cont = document.getElementById("content");
667 var spans = cont.getElementsByTagName("span");
668 var refs = {};
669 var n = 0;
670 for (i=0; i<spans.length; i++) {
671 if (spans[i].className == "footnote") {
672 n++;
673 var note = spans[i].getAttribute("data-note");
674 if (!note) {
675 // Use [\s\S] in place of . so multi-line matches work.
676 // Because JavaScript has no s (dotall) regex flag.
677 note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
678 spans[i].innerHTML =
679 "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
680 "' title='View footnote' class='footnote'>" + n + "</a>]";
681 spans[i].setAttribute("data-note", note);
682 }
683 noteholder.innerHTML +=
684 "<div class='footnote' id='_footnote_" + n + "'>" +
685 "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
686 n + "</a>. " + note + "</div>";
687 var id =spans[i].getAttribute("id");
688 if (id != null) refs["#"+id] = n;
689 }
690 }
691 if (n == 0)
692 noteholder.parentNode.removeChild(noteholder);
693 else {
694 // Process footnoterefs.
695 for (i=0; i<spans.length; i++) {
696 if (spans[i].className == "footnoteref") {
697 var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
698 href = href.match(/#.*/)[0]; // Because IE return full URL.
699 n = refs[href];
700 spans[i].innerHTML =
701 "[<a href='#_footnote_" + n +
702 "' title='View footnote' class='footnote'>" + n + "</a>]";
703 }
704 }
705 }
706 },
707
708 install: function(toclevels) {
709 var timerId;
710
711 function reinstall() {
712 asciidoc.footnotes();
713 if (toclevels) {
714 asciidoc.toc(toclevels);
715 }
716 }
717
718 function reinstallAndRemoveTimer() {
719 clearInterval(timerId);
720 reinstall();
721 }
722
723 timerId = setInterval(reinstall, 500);
724 if (document.addEventListener)
725 document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false);
726 else
727 window.onload = reinstallAndRemoveTimer;
728 }
729
730 }
731 asciidoc.install();
732 /*]]>*/
733 </script>
734 </head>
735 <body class="article">
736 <div id="header">
737 <h1>Memory Management in GNU APL</h1>
738 <span id="author">Jürgen Sauermann, GNU APL</span><br />
739 </div>
740 <div id="content">
741 <div class="sect1">
742 <h2 id="_abstract">Abstract</h2>
743 <div class="sectionbody">
744 <div class="paragraph"><p>This document describes how memory is being managed in GNU APL and what
745 the consequences are for a GNU APL programmer.</p></div>
746 </div>
747 </div>
748 <div class="sect1">
749 <h2 id="_fundamentals">Fundamentals</h2>
750 <div class="sectionbody">
751 <div class="ulist"><ul>
752 <li>
753 <p>
754 The GNU APL interpreter is an executable that runs in a single process.
755 Inside the interpreter run a handful of threads; the memory overhead of
756 these threads can be neglected.
757 </p>
758 </li>
759 <li>
760 <p>
761 Depending on configuration details and user preferences, the interpreter may
762 fork additional processes (APserver, GTK_server) whose sizes are so small
763 they can also be neglected.
764 </p>
765 </li>
766 </ul></div>
767 <div class="paragraph"><p>The sizes of the binaries mentioned (at the time of this writing) are:</p></div>
768 <div class="tableblock">
769 <table rules="all"
770 width="60%"
771 frame="border"
772 cellspacing="0" cellpadding="4">
773 <caption class="title">Table 1. Binary Sizes</caption>
774 <col width="50%" />
775 <col width="50%" />
776 <thead>
777 <tr>
778 <th align="left" valign="top"> Binary </th>
779 <th align="left" valign="top"> Size (bytes)</th>
780 </tr>
781 </thead>
782 <tbody>
783 <tr>
784 <td align="left" valign="top"><p class="table">GNU APL Interpreter</p></td>
785 <td align="left" valign="top"><p class="table">44,620,848</p></td>
786 </tr>
787 <tr>
788 <td align="left" valign="top"><p class="table">APserver</p></td>
789 <td align="left" valign="top"><p class="table">1,540,944</p></td>
790 </tr>
791 <tr>
792 <td align="left" valign="top"><p class="table">Gtk_server</p></td>
793 <td align="left" valign="top"><p class="table">530,376</p></td>
794 </tr>
795 </tbody>
796 </table>
797 </div>
798 <div class="ulist"><ul>
799 <li>
800 <p>
801 After the interpreter has started it requests additional memory from the
802 operating system on behalf of APL commands and APL code executed. That
803 memory is allocated by malloc(), either directly or indirectly (via C++
804 new()). When the memory is no longer needed, it is returned to malloc()
805 memory management (for later re-use). The memory kept in the malloc()
806 memory management is only returned to the operating system when the
807 GNU APL interpreter exits.
808 </p>
809 </li>
810 <li>
811 <p>
812 GNU APL tries to allocate memory statically as much as possible. The
813 static allocation has already taken place when the interpreter outputs its
814 first APL prompt. After that dynamic memory allocation via malloc()/new()
815 is performed for objects whose lifetime is potentially infinite (i.e. whose
816 destruction is triggered by APL code. These objects are:
817 </p>
818 <div class="ulist"><ul>
819 <li>
820 <p>
821 APL variables
822 </p>
823 </li>
824 <li>
825 <p>
826 APL values
827 </p>
828 </li>
829 <li>
830 <p>
831 defined functions
832 </p>
833 </li>
834 <li>
835 <p>
836 call stack (aka. <strong>)SI</strong> stack) entries of defined functions
837 </p>
838 </li>
839 </ul></div>
840 </li>
841 </ul></div>
842 <div class="paragraph"><p>In the following the sizes of these objects are given. The sizes differ
843 slightly between 32-bit and 64-bit systems because 32-bit systems use 4-byte
844 pointers and 64-bit systems use 8-byte pointers. In addition to the indicated
845 sizes, there is a (very) small overhead in the malloc() library for every
846 object. Each of the objects is significantly larger than its malloc overhead
847 and therefore the overhead can normally be ignored. A typical APL workspace is
848 assumed to have fewer than 1000 APL symbols (i.e. variables or defined
849 functions) and an )SI stack depth of less than 100. A typical computer on which
850 GNU APL runs is assumed to have 4 GB of memory available to the interpreter and
851 it should definitely have at least 1 GB.</p></div>
852 <div class="tableblock">
853 <table rules="all"
854 width="60%"
855 frame="border"
856 cellspacing="0" cellpadding="4">
857 <caption class="title">Table 2. Sizes of Dynamic Objects</caption>
858 <col width="33%" />
859 <col width="33%" />
860 <col width="33%" />
861 <thead>
862 <tr>
863 <th align="left" valign="top"> Object </th>
864 <th align="left" valign="top"> 32-bit system </th>
865 <th align="left" valign="top"> 64-bit system</th>
866 </tr>
867 </thead>
868 <tbody>
869 <tr>
870 <td align="left" valign="top"><p class="table">ValueStackItem</p></td>
871 <td align="left" valign="top"><p class="table">16</p></td>
872 <td align="left" valign="top"><p class="table">24</p></td>
873 </tr>
874 <tr>
875 <td align="left" valign="top"><p class="table">Token</p></td>
876 <td align="left" valign="top"><p class="table">20</p></td>
877 <td align="left" valign="top"><p class="table">24</p></td>
878 </tr>
879 <tr>
880 <td align="left" valign="top"><p class="table">Ravel Cell</p></td>
881 <td align="left" valign="top"><p class="table">20</p></td>
882 <td align="left" valign="top"><p class="table">24</p></td>
883 </tr>
884 <tr>
885 <td align="left" valign="top"><p class="table">UCS_string</p></td>
886 <td align="left" valign="top"><p class="table">20 + X</p></td>
887 <td align="left" valign="top"><p class="table">24 + X</p></td>
888 </tr>
889 <tr>
890 <td align="left" valign="top"><p class="table">APL Symbol</p></td>
891 <td align="left" valign="top"><p class="table">56 + X</p></td>
892 <td align="left" valign="top"><p class="table">80 + X</p></td>
893 </tr>
894 <tr>
895 <td align="left" valign="top"><p class="table">Defined Function</p></td>
896 <td align="left" valign="top"><p class="table">292 + X</p></td>
897 <td align="left" valign="top"><p class="table">400 + X</p></td>
898 </tr>
899 <tr>
900 <td align="left" valign="top"><p class="table">Call Stack Entry</p></td>
901 <td align="left" valign="top"><p class="table">2952</p></td>
902 <td align="left" valign="top"><p class="table">3952</p></td>
903 </tr>
904 <tr>
905 <td align="left" valign="top"><p class="table">APL Value</p></td>
906 <td align="left" valign="top"><p class="table">372 + X</p></td>
907 <td align="left" valign="top"><p class="table">456 + X</p></td>
908 </tr>
909 </tbody>
910 </table>
911 </div>
912 <div class="paragraph"><p>In the table above, the term + X means that the object is a structure that
913 contains other objects. For example, the size of a defined function depends
914 on the number of function lines and their lengths. Likewise, the size of an
915 APL value depends primarily on the number of its ravel elements. The term X
916 for the different objects is explained below.</p></div>
917 <div class="sect2">
918 <h3 id="_x_for_a_unicode_string_aka_ucs_string">X for a Unicode string (aka. UCS string)</h3>
919 <div class="paragraph"><p>X is 4 bytes per Unicode character. For performance reasons at least 16
920 characters are allocated so that X ≥ 64.</p></div>
921 </div>
922 <div class="sect2">
923 <h3 id="_x_for_an_apl_symbol">X for an APL Symbol</h3>
924 <div class="paragraph"><p>An APL symbol has two variable parts: a UCS string containing the name of the
925 symbol (e.g. a variable name or the name of a defined function) and a value
926 stack. The value stack of a function is initially empty, but is increased
927 when a defined function is called and the symbol is a local variable or a
928 label in the defined function and decreased if that function returns. The
929 amount of memory by which the stack is increased is the size of a
930 <strong>ValueStackItem</strong> in the table above.</p></div>
931 </div>
932 <div class="sect2">
933 <h3 id="_x_for_a_defined_function">X for a Defined Function</h3>
934 <div class="paragraph"><p>When a defined function is created, its textual representation (i.e. the
935 argument for ⎕FX or the result produced by the ∇-editor) is stored in the
936 function object for the purpose of displaying error information in case the
937 execution of the function fails.</p></div>
938 <div class="paragraph"><p>The textual representation is then converted into an internal representation
939 of fixed-sized Tokens with one token for each lexical unit. A lexical unit is:</p></div>
940 <div class="ulist"><ul>
941 <li>
942 <p>
943 one user-defined name, or
944 </p>
945 </li>
946 <li>
947 <p>
948 one distinguished name, or
949 </p>
950 </li>
951 <li>
952 <p>
953 one primitive function, or
954 </p>
955 </li>
956 <li>
957 <p>
958 a special symbol (◊ [ ] etc.)
959 </p>
960 </li>
961 </ul></div>
962 <div class="paragraph"><p>Finally, a jump table to the different function lines is added to the
963 function. The X for the function is therefore Utx + N×To + L×Jt, where</p></div>
964 <div class="ulist"><ul>
965 <li>
966 <p>
967 Utx is the size of the UCS_string containing the function text,
968 </p>
969 </li>
970 <li>
971 <p>
972 N is the number of Tokens,
973 </p>
974 </li>
975 <li>
976 <p>
977 To is the size of a Token as above,
978 </p>
979 </li>
980 <li>
981 <p>
982 L is the number of function lines
983 </p>
984 </li>
985 <li>
986 <p>
987 Jt is the size of a jump table entry (4 bytes)
988 </p>
989 </li>
990 </ul></div>
991 </div>
992 <div class="sect2">
993 <h3 id="_x_for_an_apl_value">X for an APL Value</h3>
994 <div class="paragraph"><p>GNU APL distinguishes between short values and long values. A short value is a
995 value with up to 12 ravel elements while a long value is a value with more
996 than 12 ravel elements. The split point between long and short values is 12 by
997 default, but can be changed via ./configure.</p></div>
998 <div class="paragraph"><p>The rationale for distinguishing short and long values is performance. A short
999 value can be allocated in a single new() call while a long value needs two
1000 new() calls: one for the value header (which contains the shape of the value),
1001 and one for the ravel cells of the value. Looking at the details of malloc()
1002 one can see that the allocation or deallocation of smaller memory areas is more
1003 efficient than the allocation or deallocation of larger areas, Finally, short
1004 APL values occur more frequently in APL programs than long values, and they
1005 are also more frequently overridden as a whole (which allocates a new value
1006 and discards the old) as opposed to being updated via indexed assignment
1007 (which changes an existing value but does not create a newly allocated
1008 value (unless nesting is involved)).</p></div>
1009 <div class="ulist"><ul>
1010 <li>
1011 <p>
1012 If a value is short then X = 0
1013 </p>
1014 </li>
1015 <li>
1016 <p>
1017 otherwise X = N × Ce, where
1018 </p>
1019 <div class="ulist"><ul>
1020 <li>
1021 <p>
1022 N is the number of ravel elements (i.e. ⍴,VALUE), and
1023 </p>
1024 </li>
1025 <li>
1026 <p>
1027 Ce is the size of a ravel Cell as above.
1028 </p>
1029 </li>
1030 </ul></div>
1031 </li>
1032 </ul></div>
1033 <div class="paragraph"><p><strong>Note:</strong> In the classical APL world of ISO 8485, all ravel elements of an APL
1034 value had the same type such as bit, 8-bit char, 32-bit integer, or float and
1035 the ravel was densely packed. Accessing a particular element V[n] of a value V
1036 was a fast constant time operation, which was internally performed very
1037 frequently. Many APL primitive functions (with the noteworthy exception of
1038 scalar functions) need to perform multiple computations of V[n] for one
1039 of their arguments V.</p></div>
1040 <div class="paragraph"><p>In ISO 13751 the concept of mixed values was introduced. In a mixed value,
1041 different ravel elements can have different types, so that the type becomes a
1042 property of each ravel element of a value instead of a property of the value
1043 itself. The implementer of ISO 13751 then had different alternatives (and
1044 possibly combinations of them) to deal with mixed values:</p></div>
1045 <div class="ulist"><ul>
1046 <li>
1047 <p>
1048 Leave the existing bit-, byte-, integer- and float-arrays as they
1049 are and add a new kind of mixed-arrays. This was a reasonable approach for
1050 those who had a code base from ISO 8485 already and who wanted to extend
1051 that code base to ISO 13751.
1052 </p>
1053 </li>
1054 <li>
1055 <p>
1056 Make the ravel elements pointers to a different location that contains the
1057 real ravel elements. Since all pointers have the same size, the constant-time
1058 access to arbitrary ravel elements would remain. The performance cost
1059 would be somewhat higher due to:
1060 </p>
1061 <div class="ulist"><ul>
1062 <li>
1063 <p>
1064 the additional indirection via the pointers, and
1065 </p>
1066 </li>
1067 <li>
1068 <p>
1069 possibly one more memory allocation per ravel element.
1070 While the cost for the additional indirection would probably be more than
1071 compensated by the reduced access time, the cost for additional memory
1072 allocations could have become a major headache.
1073 </p>
1074 </li>
1075 </ul></div>
1076 </li>
1077 <li>
1078 <p>
1079 Make the ravel elements virtual objects that all have the same size. That
1080 means instead of primitive functions working with pointers to ravel
1081 elements, the ravel elements would point to the functions that are
1082 appropriate for their type. On one hand, this approach wastes quite some
1083 memory because the largest possible type (i.e. complex numbers) determines
1084 the size of all ravel cells including those that would fit in far fewer
1085 bytes. On the other hand, there are quite a few advantages:
1086 </p>
1087 <div class="ulist"><ul>
1088 <li>
1089 <p>
1090 The same size of all elements causes V[n] to take constant time,
1091 </p>
1092 </li>
1093 <li>
1094 <p>
1095 The indexed assignment of e.g. a single element (of a different type) to a
1096 non-mixed array would also take (constant time (in option 1 above the entire
1097 array would have needed to be converted from non-mixed to mixed)).
1098 </p>
1099 </li>
1100 <li>
1101 <p>
1102 The number of different array types would be reduced to 1 instead of being
1103 increased from 5 to 6 (bit, byte, integer, float, complex, and mixed). This
1104 in turn makes a huge number of type checks in the dyadic APL primitives
1105 obsolete. Since a virtual cell knows its own type, it only needs to check
1106 the type of the other cell(s) in a dyadic operation.
1107 </p>
1108 </li>
1109 <li>
1110 <p>
1111 The "wasted" space could be used for other features at no extra cost such as:
1112 </p>
1113 <div class="ulist"><ul>
1114 <li>
1115 <p>
1116 64-bit integers instead of 32-bit Integers,
1117 </p>
1118 </li>
1119 <li>
1120 <p>
1121 rational number arithmetic for floating point numbers
1122 </p>
1123 </li>
1124 </ul></div>
1125 </li>
1126 </ul></div>
1127 </li>
1128 </ul></div>
1129 <div class="paragraph"><p>Since GNU APL was to be designed from scratch without having a prior code base,
1130 the first option was ruled out almost immediately. The second option was
1131 briefly considered, but then ruled out as well because code size for the second
1132 option was expected to be significantly larger than for the third option.</p></div>
1133 </div>
1134 </div>
1135 </div>
1136 <div class="sect1">
1137 <h2 id="_summary">Summary</h2>
1138 <div class="sectionbody">
1139 <div class="paragraph"><p>The discussion of sizes so far can be summarized as:</p></div>
1140 <div class="paragraph"><p><strong>Corollary:</strong> For a typical APL program on a typical computer, the only items
1141 of concern in the context of memory management are APL values.</p></div>
1142 <div class="paragraph"><p><strong>Note:</strong> The default values of system variable ⎕SYL (aka. system limits)
1143 guarantee to some extent that an APL program behaves like a typical APL
1144 program,</p></div>
1145 </div>
1146 </div>
1147 <div class="sect1">
1148 <h2 id="_how_the_available_memory_is_estimated">How the Available Memory is Estimated</h2>
1149 <div class="sectionbody">
1150 <div class="sect2">
1151 <h3 id="_history">History</h3>
1152 <div class="paragraph"><p>In the good old days of CP/M and friends, say 1975, memory management was
1153 rather simple. The process running an APL interpreter would know how much
1154 memory was reserved (and therefore available for it) and it could use that
1155 memory without restrictions. The amount of memory (say <strong>initial-⎕WA</strong>) that
1156 the process could use was simply:</p></div>
1157 <div class="ulist"><ul>
1158 <li>
1159 <p>
1160 <strong>initial-⎕WA = top-of-memory - top-of-used-memory</strong>
1161 </p>
1162 </li>
1163 </ul></div>
1164 <div class="paragraph"><p>This value was determined at the start-up of the interpreter (where the malloc()
1165 pool was essentially empty) and the interpreter would keep track of the memory
1166 that it allocated after that, say <strong>memory-used</strong>. So printing ⎕WA at any time
1167 would essentially show <strong>initial-⎕WA - memory-used</strong>. And that was it.</p></div>
1168 </div>
1169 <div class="sect2">
1170 <h3 id="_present_memory_management">Present Memory Management</h3>
1171 <div class="paragraph"><p>Since then a number of things have changed. The changes are in general
1172 advantageous, but also cause trouble in very specific situations. The main
1173 differences, as seen by an application like GNU APL are:</p></div>
1174 <div class="ulist"><ul>
1175 <li>
1176 <p>
1177 Change from physical to virtual memory. These days the memory allocated by
1178 malloc() is virtual. Under <strong>normal</strong> circumstances the application need not
1179 care about this difference, but in some special cases it is negatively
1180 affected (and can then do very little about it).
1181 </p>
1182 </li>
1183 <li>
1184 <p>
1185 A direct consequence of virtual memory is that the size of it is no longer
1186 determined by the physical memory available in a computer, but by other
1187 properties like the number of bits in the virtual memory addresses. This
1188 makes it impossible to determine initial-⎕WA (more precisely: ⎕WA is no
1189 longer a constant but becomes dependent on what happens not only in the APL
1190 interpreter itself but also in other processes and/or in the kernel.
1191 </p>
1192 </li>
1193 <li>
1194 <p>
1195 Over-commitment: Modern operating systems return far more virtual memory to
1196 applications than they really have in terms of physical memory. The idea is
1197 that not all applications reach their maximum need for memory at the same
1198 time, so that the same piece of physical memory can be used by different
1199 applications (or by the kernel for that matter) at different times.
1200 </p>
1201 </li>
1202 </ul></div>
1203 <div class="paragraph"><p><strong>Corollary 1:</strong> In the old days, the memory that an application has obtained
1204 from the kernel was a <strong>guarantee</strong>. These days it is merely a <strong>promise</strong> which
1205 is <strong>normally</strong> kept but may fail with a small though non-zero probability.</p></div>
1206 <div class="paragraph"><p><strong>Corollary 2:</strong> As far as GNU APL is concerned, GNU APL does not (and actually
1207 cannot) guarantee proper operation if the kernel reaches the limit of its
1208 physical memory. Some error handling mechanisms, in particular WS FULL errors,
1209 may fail in a non-graceful fashion - including immediate termination of the
1210 GNU APL process without any warnings.</p></div>
1211 <div class="paragraph"><p>Although a crash of GNU APL cannot be prevented in general, one can decrease
1212 the probability of such crashes by taking countermeasures beforehand. Most GNU
1213 APL users will not need this (see <strong>normal</strong> above), but those facing problems
1214 with improper WS FULL handling should read on.</p></div>
1215 </div>
1216 </div>
1217 </div>
1218 <div class="sect1">
1219 <h2 id="_improving_the_gnu_apl_ws_full_handling">Improving the GNU APL WS FULL handling</h2>
1220 <div class="sectionbody">
1221 <div class="sect2">
1222 <h3 id="_the_oom_handler">The OOM handler</h3>
1223 <div class="paragraph"><p>One enemy of GNU APL is the kernel&#8217;s OOM (out-of-memory) handler. The OOM
1224 handler is invoked when the kernel needs (physical) memory and has none. It
1225 then kills one or more running processes in order to claim their memory back.
1226 The processes that are killed (ungracefully) may or may not be the process(es)
1227 that have caused the shortage of memory. That is:</p></div>
1228 <div class="ulist"><ul>
1229 <li>
1230 <p>
1231 GNU APL may be killed by some other (unrelated) process or interrupt that
1232 has requested memory, or
1233 </p>
1234 </li>
1235 <li>
1236 <p>
1237 some other (unrelated) process may be killed by a memory request from GNU
1238 APL, or
1239 </p>
1240 </li>
1241 <li>
1242 <p>
1243 GNU APL is killed as a result of its own memory request.
1244 </p>
1245 </li>
1246 </ul></div>
1247 <div class="paragraph"><p>It should be clear that the first two cases are very difficult to reproduce.
1248 The third case is much easier by creating a very large APL value. It is also
1249 the case that has been observed in reality.</p></div>
1250 <div class="paragraph"><p>In GNU/Linux, one can protect individual processes from being killed by the
1251 OOM handler with the following command (as root; &lt;PID&gt; is the process ID of
1252 the process to be protected:</p></div>
1253 <div class="listingblock">
1254 <div class="content">
1255 <pre>
1256
1257 <code>echo -17 &gt; /proc/&lt;PID&gt;/oom_adj</code>
1258
1259 </pre>
1260 </div></div>
1261 <div class="paragraph"><p>The OOM handler can also be disabled entirely with</p></div>
1262 <div class="listingblock">
1263 <div class="content">
1264 <pre>
1265
1266 <code>sysctl vm.overcommit_memory=2</code>
1267
1268 </pre>
1269 </div></div>
1270 <div class="paragraph"><p>or:</p></div>
1271 <div class="listingblock">
1272 <div class="content">
1273 <pre>
1274
1275 <code>echo "vm.overcommit_memory=2" &gt;&gt; /etc/sysctl.conf</code>
1276
1277 </pre>
1278 </div></div>
1279 <div class="paragraph"><p><strong>Warning:</strong> These settings can seriously impact the stability of the operating
1280 system and should not be used on machines whose availability is a concern.
1281 There is no point in sacrificing the stability of a system for the stability of
1282 a process running on that system.</p></div>
1283 </div>
1284 <div class="sect2">
1285 <h3 id="_using_system_limits">Using System Limits</h3>
1286 <div class="paragraph"><p>Instead of disabling the OOM handler, one should use methods that prevent GNU
1287 APL from requesting too much memory, so that the OOM handler is not invoked
1288 in the first place (at least not from GNU APL). There are several ⎕SYL (aka.
1289 system limit) entries that can be set by an APL program to prevent GNU APL
1290 from requesting too much memory. Some of them have to be combined with others
1291 to achieve full protection.</p></div>
1292 <div class="ulist"><ul>
1293 <li>
1294 <p>
1295 ⎕SYL[1;] limits the depth of the )SI stack and therefore the amount of
1296 memory spent for function calls. Referring to Table 2 above, <strong>⎕SYL[1;2]←1000</strong>
1297 will throw a LIMIT ERROR if the )SI stack grows above 3 Megabytes (32-bit
1298 systems) or 4 Megabytes (64-bit systems).
1299 </p>
1300 </li>
1301 <li>
1302 <p>
1303 ⎕SYL[2;] limits the number of APL values. <strong>⎕SYL[2;2]←1000</strong> will throw a
1304 LIMIT ERROR when more than 1000 APL values (variables as well as
1305 intermediate results) are created. Referring to Table 2 above, this
1306 corresponds to 372 kByte on 32-bit systems, or 456 kByte on 64-bit systems.
1307 Note that the space for localized variables of defined functions belongs to
1308 ⎕SYL[2;] and not to ⎕SYL[1;].
1309 </p>
1310 </li>
1311 <li>
1312 <p>
1313 ⎕SYL[3;] limits the number of ravel elements in APL values.
1314 <strong>⎕SYL[3;2]←1000000</strong> will throw a LIMIT ERROR before more than 20 MByte on
1315 32-bit systems, or more than 24 MByte on 64-bit systems is requested.
1316 </p>
1317 </li>
1318 <li>
1319 <p>
1320 Finally, ⎕SYL[31 32;] define a safety margin that is is explained below.
1321 </p>
1322 </li>
1323 </ul></div>
1324 <div class="paragraph"><p>In short, ⎕SYL[1;] protects primarily against too deep recursion, which is most
1325 likely caused by a programming fault. ⎕SYL[2;] protects against too many APL
1326 values, and ⎕SYL[3;] protects against APL values becoming too large.</p></div>
1327 <div class="paragraph"><p>Like WS FULL and other APL errors, hitting a system limit brings the
1328 interpreter back to immediate execution (interactive) mode. If one wants to
1329 handle the situation programmatically, one can catch the limit error with ⎕EA,
1330 ⎕EB, or ⎕EC.</p></div>
1331 </div>
1332 <div class="sect2">
1333 <h3 id="_analyzing_proc_meminfo">Analyzing /proc/meminfo</h3>
1334 <div class="paragraph"><p>On a GNU/Linux machine, the file <strong>/proc/meminfo</strong> provides quite useful
1335 information about the usage of memory at the point in time when /proc/meminfo
1336 is read. Before setting system limits as described above, one should consult
1337 /proc/meminfo to see how the memory is distributed and then set the limits
1338 accordingly.</p></div>
1339 <div class="paragraph"><p>Ideally <strong>/proc/meminfo</strong> shows something like:</p></div>
1340 <div class="listingblock">
1341 <div class="content">
1342 <pre>
1343
1344 <code>MemTotal: 185736 kB
1345 MemFree: 12660 kB
1346 MemAvailable: 126264 kB
1347 Buffers: 74704 kB
1348 Cached: 54732 kB</code>
1349
1350 </pre>
1351 </div></div>
1352 <div class="paragraph"><p>In that case <strong>MemAvailable:</strong> is the value on which the system limits settings
1353 should be based. On older GNU/Linux machines <strong>MemAvailable:</strong> may not be
1354 displayed. In that case <strong>MemFree: + Cached:</strong> can be used as a workaround.</p></div>
1355 <div class="paragraph"><p>The actual implementation of <strong>MemAvailable:</strong> by the kernel is quite complicated
1356 (more than simply adding <strong>MemFree:</strong> and <strong>Cached:</strong>). For that reason, even if
1357 <strong>MemAvailable:</strong> were available on all GNU/Linux machines it would still be
1358 impossible to call it before each creation of an APL value.</p></div>
1359 <div class="paragraph"><p>Instead, GNU APL uses the following simple algorithm:</p></div>
1360 <div class="ulist"><ul>
1361 <li>
1362 <p>
1363 After the interpreter has started, compute <strong>total_memory</strong> as follows:
1364 </p>
1365 <div class="ulist"><ul>
1366 <li>
1367 <p>
1368 If the process that runs the interpreter has a limit set on the amount of
1369 its virtual memory, then the interpreter assumes that amount of memory
1370 will be available and sets <strong>total_memory</strong> to that limit. In this case
1371 <strong>/proc/meminfo</strong> is not used.
1372 </p>
1373 </li>
1374 <li>
1375 <p>
1376 Otherwise (i.e. the virtual memory for the process is unlimited) the
1377 interpreter consults <strong>/proc/meminfo</strong> and sets <strong>total_memory</strong> to either
1378 <strong>MemAvailable:</strong> (if present) or else to <strong>MemFree: + Cached:</strong>.
1379 </p>
1380 </li>
1381 </ul></div>
1382 </li>
1383 <li>
1384 <p>
1385 As new APL values are created and destroyed in the course of running APL
1386 programs, the amount of memory allocated for them is tracked in, say,
1387 <strong>used_memory</strong>.
1388 </p>
1389 </li>
1390 <li>
1391 <p>
1392 Before requesting new memory of, say, size <strong>new</strong>, GNU APL checks that
1393 </p>
1394 </li>
1395 </ul></div>
1396 <div class="listingblock">
1397 <div class="content">
1398 <pre>
1399
1400 <code> total_memory_1 ≥ used_memory + new + ⎕WA-margin</code>
1401
1402 </pre>
1403 </div></div>
1404 <div class="ulist"><ul>
1405 <li>
1406 <p>
1407 In this check <strong>total_memory_1</strong> is <strong>⎕SYL[32;2]</strong> percent of <strong>total_memory</strong>
1408 above, and reduced by <strong>⎕SYL[31;2]</strong>. The reason for the scaling by
1409 <strong>⎕SYL[32;2]</strong> is that malloc() usually requests slightly more memory from
1410 the kernel than the application had requested from malloc().
1411 </p>
1412 <div class="ulist"><ul>
1413 <li>
1414 <p>
1415 If that check fails then WS FULL is raised,
1416 </p>
1417 </li>
1418 <li>
1419 <p>
1420 otherwise the request is forwarded to malloc() and from there possibly
1421 to the kernel.
1422 </p>
1423 </li>
1424 </ul></div>
1425 </li>
1426 </ul></div>
1427 <div class="paragraph"><p>The ⎕WA-margin currently has a default value of 0, but can be changed via</p></div>
1428 </div>
1429 <div class="sect2">
1430 <h3 id="_hints_for_gnu_apl_users">Hints for GNU APL Users</h3>
1431 <div class="sect3">
1432 <h4 id="_setting_a_memory_limit">Setting a memory limit</h4>
1433 <div class="paragraph"><p>If stability of a system is a concern, then setting memory limits for
1434 processes is normally a good idea. The command for doing that differs
1435 between shells; in <strong>bash</strong> you can start GNU APL with a virtual memory limit
1436 of like this:</p></div>
1437 <div class="listingblock">
1438 <div class="content">
1439 <pre>
1440
1441 <code>ulimit -v 1000000
1442 apl</code>
1443
1444 </pre>
1445 </div></div>
1446 <div class="paragraph"><p>The unit is kB, so that the command above will set the limit to 1 GB. The limit
1447 remains in force for all subsequent commands and can be removed like this:</p></div>
1448 <div class="listingblock">
1449 <div class="content">
1450 <pre>
1451
1452 <code>ulimit -v unlimited
1453 apl</code>
1454
1455 </pre>
1456 </div></div>
1457 <div class="paragraph"><p><strong>Note:</strong> setting memory limits via <strong>ulimit</strong> as such does not directly solve the
1458 problem that a process may have when it reaches the limit. However, it helps a
1459 process to predict when the limit will be reached.</p></div>
1460 </div>
1461 </div>
1462 <div class="sect2">
1463 <h3 id="_32_bit_gnu_linux">32-bit GNU/Linux</h3>
1464 <div class="paragraph"><p>On 32-bit machines, there is a per process memory limit of about 3 GByte. If
1465 the memory of a machine is, say, 8 GB or more, then one can have several
1466 processes with each process allocating up to 3 GByte as long as the memory
1467 shown in <strong>/proc/meminfo</strong> is not exceeded.</p></div>
1468 <div class="paragraph"><p>If GNU APL sees a <strong>total_memory</strong> of more than 3 GB in the analysis of
1469 <strong>/proc/meminfo</strong>, then it reduces <strong>total_memory</strong> to about 3 GB. if it is
1470 running on a 32-bit kernel. 64-bit machines have a similar limit, but the
1471 limit is so high that it is not relevant in practice.</p></div>
1472 <div class="paragraph"><p>The safety margin <strong>(total_memory_1</strong> - <strong>total_memory)</strong> that results from
1473 <strong>⎕SYL[31 32;2]</strong> remains in effect so that the total memory that an APL program
1474 can obtain is somewhere below 3 GB.</p></div>
1475 <div class="sect3">
1476 <h4 id="_troubleshooting_ws_full_problems">Troubleshooting WS FULL problems</h4>
1477 <div class="paragraph"><p>For debugging purposes the safety margin can be disabled in APL like this:</p></div>
1478 <div class="listingblock">
1479 <div class="content">
1480 <pre>
1481
1482 <code> ⎕SYL[31;2] ← 1000000 ⍝ smallest margin (1 MB)
1483 ⎕SYL[32;2] ← 200 ⍝ far above available</code>
1484
1485 </pre>
1486 </div></div>
1487 <div class="paragraph"><p>WS FULL can occur very many places within the interpreter. To find the exact
1488 position where a WS FULL was generated, some related logging facilities can be
1489 turned on:</p></div>
1490 <div class="listingblock">
1491 <div class="content">
1492 <pre>
1493
1494 <code> ]log 25
1495 ]log 26
1496 ]log 45</code>
1497
1498 </pre>
1499 </div></div>
1500 <div class="paragraph"><p>On a 32-bit machine a WS FULL can be reliably produced like this (the example
1501 was kindly provided by Christian Robert):</p></div>
1502 <div class="listingblock">
1503 <div class="content">
1504 <pre>
1505
1506 <code> N←5000000 ⍝ 32-bit; use a larger value for 64-bit
1507 A←⊂'0123456789ABCDEF'
1508 ↑↑ N ⍴ A ⍝ trigger WS FULL</code>
1509
1510 </pre>
1511 </div></div>
1512 <div class="paragraph"><p>This example is of particular interest because, depending upon variable <strong>N</strong>
1513 it creates two different challenges for the kernel. One is a big allocation
1514 when the process is already near starvation and one is a small allocation
1515 that fails.</p></div>
1516 <div class="paragraph"><p>In both cases the WS FULL is caused by <strong>N ⍴ A</strong>. Because A is a scalar (due to
1517 ⊂), all ravel elements of the result are nested values. Therefore the
1518 computation of <strong>N ⍴ A</strong> creates exactly 1+N values: a large value for the result
1519 itself and N small nested values - the ravel of the result.</p></div>
1520 <div class="paragraph"><p>Referring to Table 2, the variable <strong>A</strong> (and therefore each nested value of the
1521 result needs <strong>372 + 16×20 = 692</strong> bytes. The top-level of the result requires
1522 <strong>372 + N×20</strong> bytes. Let <strong>F</strong> be the amount of (free) memory that could be
1523 obtained from malloc(). Then:</p></div>
1524 <div class="olist arabic"><ol class="arabic">
1525 <li>
1526 <p>
1527 if <strong>F &lt; 372 + N×20</strong> then the allocation of the top-level value fails,
1528 </p>
1529 </li>
1530 <li>
1531 <p>
1532 else if <strong>F &lt; 372 + N×20 + N×692</strong> then the allocation of the top-level value
1533 succeeds, but the allocation of one of the nested sub-values fails,
1534 </p>
1535 </li>
1536 <li>
1537 <p>
1538 else all allocations succeed and <strong>N ⍴ A</strong> succeeds as expected.
1539 </p>
1540 </li>
1541 </ol></div>
1542 <div class="paragraph"><p>The less than obvious difference between the first and the second case is the
1543 amount of free memory at the point in time where the allocation failed:</p></div>
1544 <div class="olist arabic"><ol class="arabic">
1545 <li>
1546 <p>
1547 if the first (large) allocation fails, then the free memory is still F,
1548 </p>
1549 </li>
1550 <li>
1551 <p>
1552 if one of the subsequent (small) allocations fails then (since the previous
1553 allocations have succeeded) the free memory is now less than the requested
1554 size, regardless how large F was initially. In our example F would be less
1555 than 692 bytes (in theory) or less than about 1024 (in practice because
1556 malloc allocates the next power of 2).
1557 </p>
1558 </li>
1559 </ol></div>
1560 <div class="paragraph"><p>Exception handling with only 1024 bytes left can fail as can be demonstrated
1561 on a 32-bit machine quite easily. The following examples show both cases (with
1562 safety margin disabled in order to allow near-memory-starvation.</p></div>
1563 </div>
1564 <div class="sect3">
1565 <h4 id="_example_1_ws_full_with_sufficient_free_memory_left">Example 1: WS FULL with sufficient free memory left</h4>
1566 <div class="listingblock">
1567 <div class="content">
1568 <pre>
1569
1570 <code> ⍝ enable relevant logging facilities
1571 ]log 25
1572 Log facility 'more verbose errors ' is now ON
1573 ]log 26
1574 Log facility 'details of error throwing ' is now ON
1575 ]log 45
1576 Log facility 'details of Value allocation ' is now ON
1577
1578 ⍝ disable safety margin
1579 ⎕SYL[31 32;2]←1000000 200
1580 ⎕SYL[31 32;]
1581 ⎕WA safety margin (bytes) 1000000
1582 ⎕WA memory scale (%) 200
1583
1584 A←⊂'0123456789ABCDEF' ⍝ 692 bytes
1585 N←200000000 ⍝ for 200 Mio ravel Cells
1586 ↑↑ N⍴A ⍝ 4 GB top-level + 138.4 GB nested sub-values
1587
1588 throwing WS FULL at PrimitiveFunction.cc:230
1589
1590 ----------------------------------------
1591 -- Stack trace at Error.cc:184
1592 ----------------------------------------
1593 0xB7160AF3 __libc_start_main
1594 0x8092998 main
1595 0x8247A9D Workspace::immediate_execution(bool)
1596 0x80EF330 Command::process_line()
1597 0x80EFCAB Command::do_APL_expression(UCS_string&amp;)
1598 0x80EF459 Command::finish_context()
1599 0x80FB64D Executable::execute_body() const
1600 0x81DBE31 StateIndicator::run()
1601 0x8149E2C Prefix::reduce_statements()
1602 0x8146913 Prefix::reduce_A_F_B_()
1603 0x815579D Bif_F12_RHO::eval_AB(Value_P, Value_P)
1604 0x8155463 Bif_F12_RHO::do_reshape(Shape const&amp;, Value const&amp;)
1605 0x80B5EE6 Value_P::Value_P(Shape const&amp;, char const*)
1606 0x8245B9A Value::init_ravel()
1607 0x80FA35B throw_apl_error(ErrorCode, char const*)
1608 ========================================
1609 WS FULL+
1610 ↑↑N⍴A
1611 ^ ^
1612 Ravel allocation failed
1613
1614 )MORE
1615 new Value(PrimitiveFunction.cc:230) failed (APL error in ravel allocation)
1616 throwing WS FULL at Value_P.icc:227</code>
1617
1618 </pre>
1619 </div></div>
1620 </div>
1621 <div class="sect3">
1622 <h4 id="_example_2_ws_full_with_very_little_free_memory_left">Example 2: WS FULL with very little free memory left</h4>
1623 <div class="listingblock">
1624 <div class="content">
1625 <pre>
1626
1627 <code> ⍝ enable relevant logging facilities
1628 ]log 25
1629 Log facility 'more verbose errors ' is now ON
1630 ]log 26
1631 Log facility 'details of error throwing ' is now ON
1632 ]log 45
1633 Log facility 'details of Value allocation ' is now ON
1634
1635 ⍝ disable safety margin
1636 ⎕SYL[31 32;2]←1000000 200
1637 ⎕SYL[31 32;]
1638 ⎕WA safety margin (bytes) 1000000
1639 ⎕WA memory scale (%) 200
1640
1641 A←⊂'0123456789ABCDEF' ⍝ 692 bytes
1642 N←20000000 ⍝ for 20 Mio ravel Cells
1643 ↑↑ N⍴A ⍝ 0,4 GB top-level + 13.84 GB nested sub-values
1644 Value_P::Value_P(const Shape &amp; shape, const char * loc) failed at
1645 Value_P.icc:235 (caller: PrimitiveFunction.cc:240)
1646 what: std::bad_alloc
1647 initial sbrk(): 0xa28a000
1648 current sbrk(): 0x9eaec000
1649 alloc_size: 0x140 (320)
1650 used memory: 0xbbb495c0 (3149174208)
1651
1652 throwing WS FULL at Value.cc:233
1653
1654 ----------------------------------------
1655 -- Stack trace at Error.cc:184
1656 ----------------------------------------
1657 backtrace_symbols() failed. Using backtrace_symbols_fd() instead...
1658
1659 ./apl[0x809a380]
1660 /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb70c0af3]
1661 ./apl(main+0x48)[0x8092998]
1662 ./apl(_ZN9Workspace19immediate_executionEb+0x1d)[0x8247a9d]
1663 ./apl(_ZN7Command12process_lineEv+0x460)[0x80ef330]
1664 ./apl(_ZN7Command17do_APL_expressionER10UCS_string+0x6b)[0x80efcab]
1665 ./apl(_ZN7Command14finish_contextEv+0x29)[0x80ef459]
1666 ./apl(_ZNK10Executable12execute_bodyEv+0x1d)[0x80fb64d]
1667 ./apl(_ZN14StateIndicator3runEv+0x21)[0x81dbe31]
1668 ./apl(_ZN6Prefix17reduce_statementsEv+0x22c)[0x8149e2c]
1669 ./apl(_ZN6Prefix13reduce_A_F_B_Ev+0xd3)[0x8146913]
1670 ./apl(_ZN11Bif_F12_RHO7eval_ABE7Value_PS0_+0xad)[0x815579d]
1671 ./apl(_ZN11Bif_F12_RHO10do_reshapeERK5ShapeRK5Value+0x258)[0x8155678]
1672 ./apl(_ZNK11PointerCell10init_otherEPvR5ValuePKc+0x49)[0x814c6e9]
1673 ./apl(_ZNK5Value5cloneEPKc+0x30)[0x82438a0]
1674 ./apl(_ZN7Value_PC1ERK5ShapePKc+0x13e)[0x80b5fae]
1675 ./apl(_ZN5Value15catch_exceptionERKSt9exceptionPKcS4_S4_+0x31e)[0x823c93e]
1676 ./apl(_Z15throw_apl_error9ErrorCodePKc+0x6b)[0x80fa35b]
1677 ========================================
1678 WS FULL+
1679 ↑↑N⍴A
1680 ^ ^
1681
1682 )MORE
1683 new Value(const Shape &amp; shape, const char * loc) failed (std::bad_alloc)
1684 throwing WS FULL at Value_P.icc:227</code>
1685
1686 </pre>
1687 </div></div>
1688 </div>
1689 <div class="sect3">
1690 <h4 id="_differences_between_example_1_and_example_2">Differences between Example 1 and Example 2</h4>
1691 <div class="ulist"><ul>
1692 <li>
1693 <p>
1694 Execution time: the second example uses noticeably more time before the WS
1695 FULL occurs. In this time the interpreter creates a large number of
1696 sub-values until the memory is exhausted.
1697 </p>
1698 </li>
1699 <li>
1700 <p>
1701 The stack dumps differ. The pretty-printing of C++ function names requires
1702 extra memory, in example 2 that extra memory is not available so that
1703 backtrace_symbols() fails (and the interpreter then uses a function that
1704 needs less memory):
1705 </p>
1706 </li>
1707 </ul></div>
1708 <div class="listingblock">
1709 <div class="content">
1710 <pre>
1711
1712 <code> ----------------------------------------
1713 -- Stack trace at Error.cc:184
1714 ----------------------------------------
1715 backtrace_symbols() failed. Using backtrace_symbols_fd() instead...</code>
1716
1717 </pre>
1718 </div></div>
1719 <div class="ulist"><ul>
1720 <li>
1721 <p>
1722 The WS FULL is thrown from different locations. PrimitiveFunction.cc:230 is
1723 in the implementation of dyadic ⍴, while Value.cc:233 is the exception
1724 handler for <strong>std::bad_alloc</strong> (see also <strong>what:</strong> in the debug printout),
1725 </p>
1726 </li>
1727 </ul></div>
1728 <div class="listingblock">
1729 <div class="content">
1730 <pre>
1731
1732 <code>throwing WS FULL at PrimitiveFunction.cc:230</code>
1733
1734 </pre>
1735 </div></div>
1736 <div class="listingblock">
1737 <div class="content">
1738 <pre>
1739
1740 <code>throwing WS FULL at Value.cc:233</code>
1741
1742 </pre>
1743 </div></div>
1744 <div class="ulist"><ul>
1745 <li>
1746 <p>
1747 The )MORE information in the two examples differ. The term <strong>Ravel
1748 allocation</strong> in the printout of example 1 refers to the allocation of the
1749 (large) top-level ravel.
1750 </p>
1751 </li>
1752 </ul></div>
1753 </div>
1754 </div>
1755 <div class="sect2">
1756 <h3 id="_final_remarks">Final Remarks</h3>
1757 <div class="paragraph"><p>The exact behavior of processes with too little memory is generally difficult
1758 to predict. While the A⍴N example given above is understood to some extent,
1759 there are other cases such as out-of-memory when the process stack is
1760 exhausted that were not yet reported.</p></div>
1761 <div class="paragraph"><p>We are trying our best to fix such cases, but when reporting them as errors
1762 on bug-apl please consider the following.</p></div>
1763 <div class="ulist"><ul>
1764 <li>
1765 <p>
1766 It helps a lot if you can provide a reliable way to reproduce the problem.
1767 Most of the time for fixing an error is spent on reproducing it.
1768 </p>
1769 </li>
1770 <li>
1771 <p>
1772 Check that the problem cannot be fixed by setting memory limits. As explained
1773 above there are some boundary conditions from the operating system that
1774 cannot be fixed (or even properly handled) by a process. Remember the
1775 existing tools for that:
1776 </p>
1777 <div class="ulist"><ul>
1778 <li>
1779 <p>
1780 ulimit to protect processes from each other,
1781 </p>
1782 </li>
1783 <li>
1784 <p>
1785 ⎕SYL[1 2 3;2] to limit what the GNU APL interpreter may try to allocate, and
1786 </p>
1787 </li>
1788 <li>
1789 <p>
1790 ⎕SYL[31 32;2] to avoid near-memory-starvation situations in the WS FULL
1791 exception handling.
1792 </p>
1793 </li>
1794 </ul></div>
1795 </li>
1796 </ul></div>
1797 <div class="paragraph"><p>Thanks for using GNU APL.</p></div>
1798 </div>
1799 </div>
1800 </div>
1801 </div>
1802 <div id="footnotes"><hr /></div>
1803 <div id="footer">
1804 <div id="footer-text">
1805 Last updated
1806 2024-03-14 14:48:59 CET
1807 </div>
1808 </div>
1809 </body>
1810 </html>