Re: BASIC OS for the PC

From: Ethan Dicks <ethan.dicks_at_gmail.com>
Date: Wed, 21 Dec 2016 20:08:24 -0500
Message-ID: <CAALmimnUnkKkMnVsd84fuki5+sKwn-b8J3GqpVgQe9m1xcgj=Q@mail.gmail.com>
On Wed, Dec 21, 2016 at 1:58 AM, Baltissen, GJPAA (Ruud)
<ruud.baltissen@apg.nl> wrote:
>> Is there a garbage collection in the variable area?
>
> Very good question. So far none. But there is a simple reason for that: I still don't understand what garbage collection exactly does.

Garbage collection in different contexts means different things at the
detail level, but in general, it's reclaiming space that was once
allocation but is no longer, and without specific intervention by your
code.  C does not use it because it requires you the developer to
malloc() and free() requested space.  In UNIX, the termination of a
process automatically runs down the allocation list and frees it, so
you don't really have to do anything.  How processes are implemented
on AmigaDOS, however, means that if you exit and you haven't freed
something, it's marked unavailable until reboot.  Nothing maintains a
list of what is _now_ free and can be given back.  Lots of time is
spent fixing 4-byte and other small memory leaks in Amiga programs.
OO languages that allocation massive structures behind the scenes
(object instantiation) also are subject to memory growth, but some of
them keep a list of objects that are destroyed and pick a later time
to free a bunch of them at once - garbage collection.

In the case of BASIC, there are no objects and no calls to malloc().
What is recovered is formerly-used string space.  If you say A$="foo"
and later say A$="bar", the string storage just grows downward,
leaving both 'foo' and 'bar' in memory, but only one live pointer.
Once things get too full of abandoned strings, GC kicks in and runs
down the string space and copies live strings over top of abandoned
strings and "compresses" the storage so that all the dead space is
compacted away, and that leaves more room below the bottom of "active"
string space and the top of the BASIC program.  In Commodore BASIC,
integers and floats are never given back once defined, so the only way
to free that memory is CLR or RUN which resets all the pointers to the
top of RAM, giving back all that space at once (and that only takes a
handful of cycles).

In a BASIC program, if your BASIC interpreter or OS doesn't perform
GC, you will eventually go to allocate a few bytes for a new string
and it will fail.  No GC means that programs have a finite run time
before memory is loaded with dead strings.  This is different than
trying to allocate an array that's too large - this is things run fine
for a while, then eventually, inevitably, you get OUT OF MEMORY even
if you only have a couple of variables in use.

-ethan

       Message was sent through the cbm-hackers mailing list
Received on 2016-12-22 02:00:02

Archive generated by hypermail 2.2.0.