Plan to throw one away
&gt; <i>The failure to store a represention of the parsed code was disastrous because it meant that in order to evaluate an expression or statement for a second time, the engine had to parse it a second time.</i><p>Ha! They must have learned how to write an interpreter from Herbert Schildt:<p><a href="http:&#x2F;&#x2F;www.drdobbs.com&#x2F;cpp&#x2F;building-your-own-c-interpreter&#x2F;184408184" rel="nofollow">http:&#x2F;&#x2F;www.drdobbs.com&#x2F;cpp&#x2F;building-your-own-c-interpreter&#x2F;1...</a> [1989]<p>In this piece of amusement, the Little C program is a giant null-terminated string, and the &quot;instruction pointer&quot; is of type char * . You get the picture.<p>&gt; <i>When I did come to write a garbage collector I used the mark-and-sweep algorithm. But something puzzled me, and I couldn’t find an answer in any of the textbooks I looked at: how was I supposed to schedule the collections? In a classic description of a garbage collector, you wait until memory runs out and then you collect the world. But this leads to bad behaviour on modern systems, because of swapping, and because other processes need memory too. You need to schedule collections well before memory is full. But when exactly? I still don’t know of a comprehensive solution to this problem.</i><p>In a nutshell, you let your run-time pretend that it&#x27;s running in a small machine, until it is too close to huffing and puffing too hard and then you say &quot;hey I lied, you&#x27;re actually in a bigger machine: have some breathing room&quot;. This rubbery constraint keeps it behaving reasonably nicely, rather than &quot;Wee, I have 4GB of free RAM to stomp over with strings and cons cells before ever calling GC!&quot;<p>What you have to do is pick some heap size (that is typically substantially smaller than the amount of RAM). You let the GC whack against this artificial threshold, and if that gets too excessive, according to some measure, you increase it. E.g. if after a full GC you have less than some fudge threshold free, call the OS for more memory to integrate into the object heap.<p>The threshold is calculated in some way that the image doesn&#x27;t have to execute numerous frequent GC&#x27;s before it triggers the request for more space (it doesn&#x27;t have to whack too hard and wastefully against the artificial limit).<p>Also, ephemeral GC will help, and ephemeral GC can have its own threshold against frequent ephemeral GC&#x27;s.  When not enough space is liberated by ephemeral, you schedule a full. Then if <i>that</i> doesn&#x27;t liberate enough space, add more. Since ephemeral is fast (doesn&#x27;t scan the full heap), you can work a bit closer to the heap limit (since you can suffer frequent ephemeral GC&#x27;s better than frequent full GC&#x27;s).<p>And, of course, the parameters controlling these behaviors are exposed in some way so users can tweak them. Command line arguments, env vars, local config file, system config file, run time global variable&#x2F;API, ...