1 Memory Management In Extempore
Chanda Larkins edited this page 2025-08-11 05:42:14 +00:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.


Its not really attainable to clarify Extempores memory allocation story without a detour into xtlang varieties, so well cover some of that as effectively. The 2 languages hosted by the Extempore compiler, xtlang and Scheme, have completely different approaches to allocating & managing memory. Each languages ultimately share the same memory-the stack and heap associated with the Extempore course of-but via completely different mechanisms. Broadly talking, with Scheme code Extempore manages memory for you, Memory Wave while in xtlang you need to do it yourself. That is a common commerce-off, and each has its benefits (in efficiency, programmer productivity, and many others.) and disadvantages. So if youre principally going to be writing Scheme code (e.g. youre making music utilizing the constructed-in instruments) then you definately probably dont have to learn this (although understanding how things work underneath the hood is still typically useful). To work successfully in xtlang, although, youll have to now a bit extra about memory in Extempore. Scheme objects (lists, closures, numbers, and so forth.) are mechanically garbage collected by the Extempore run-time garbage collector (GC).


Which means when new objects are created, memory is routinely allocated to store those objects, and as objects are destroyed or go out of scope (that's, there are now not any references to them) the Memory Wave System is mechanically freed up for re-use. Lets do probably the most basic memory allocation possible: Memory Wave simply binding a numerical worth to an emblem. The truth that we will use the symbol a and have it consider to 5 (as it should) implies that the worth (5) have to be stored in memory someplace. It doesnt matter the place in memory (what the address is), as a result of we will always check with the value using the symbol a. But its good to do not forget that the outline form is allocating some memory, storing the value 5 in that memory, and binding a reference to the value in the image a. We will redefine the symbol a to be some other Scheme object, say, a list.
thememorywaave.com


The three-component checklist (1 2 3) takes up extra memory than the quantity 5. So define cant simply write the new worth of a excessive of the previous one. What it does (and actually what re-defining issues always does) is allocate some new memory to store the brand new value into, and alter the variable a to point to that new worth. But what occurs to the outdated worth of 5 in memory? Nicely, it sits there untouched, at least for some time. But we cant attain it-the one handle we needed to refer to it with was the image a, and thats now certain to some other worth as a substitute. The value 5 in memory is unreachable. So theres no point having it sitting round, taking on house. Thats the place the garbage collector is available in. Every now and then the rubbish collector checks all of the Scheme objects on this planet, determines which of them are now not reachable, and then frees up that memory for use for other issues.


While I dont recommend this harsh utilitarian approach to dealing with family who are down on their luck, it is nice thought in a computer program. Memory is a finite resource, and the extra efficiently we are able to get rid of memory thats not being used the better. Mainly, having a GC implies that when youre writing Scheme code, you dont have to fret about memory. The GC takes care of all of the allocation/deallocation bookkeeping for you. The price is that this bookkeeping requires CPU cycles-cycles which you could be utilizing to do other cool things. Additionally, from time to time the GC has to briefly stop the world (freeze the execution of all Scheme code) to do its job. This takes time, and introduces a component of uncertainty (non-determinism) to the execution of your code-you by no means know precisely when the GC goes to freeze things to do its job, and theres a risk that itll happen at a really inconvenient time as far as your program is anxious (Murphys regulation and all that).