1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

2.5 Memory Management/Use:

Applesoft BASIC is rather poor in its memory management, mostly at the
cost of backwards compatability-- a standard (no machine-language
assistance) program that runs on a //e will run on any other Apple II
with at least 64K. The downside is that no memory past the first 64K
is recognized or usable. The operating system and other reserved
blocks of memory (text screen, system globals, etc) leave only 35K
($800-$9600) available for both your program and all variables. The
Hires graphics screens will use 8K each from that 35K; lores uses the
text screen at no memory penalty.

Space is used for storing variable names and their contents in
memory-- an integer will use 2 bytes for the actual data, and 2-3 for
the name. Beyond the name overhead, array Integer variables use 2
bytes of memory each, reals 5.

Strings are dynamically allocated, and use their length plus 1 in
bytes. If a lot of string manipulations are done, the space used by
the old versions of the strings are not recycled-- they remain in
memory. Thus, periodic garbage collections can be necessary. The FRE()
statement was built into Applesoft to both report on memory management
and perform garbage collection. 'FRE(x)' (parameter ignored, though 0
or 1 is customary) will report how much memory is free--
'AVAIL=FRE(1)' sets the result to a variable and does the garbage
collection. If the free memory is greater than 32K, it will be
returned as a number less than zero; add 65536 to that to get the real
number.

This memory operation may take many seconds to perform, leaving a user
to wonder what is happening. If this happens, it may be to your
advantage to do more FRE(0)'s in your code, thus doing more (hopefully
smaller) compactions. Alternatively, under ProDOS's BASIC.SYSTEM,
'PRINT CHR$(4)"FRE"' is a much faster version. [There were several
addon machine language packages for faster FREs under DOS 3.3, but
that's outside of the scope of this FAQ.]

To reserve off some space for your own purposes, you can use the
'HIMEM:' and 'LOMEM:' statements. The two define the top and bottom of
the memory pool Applesoft uses for variable storage. To read the
current value of lomem, use 'PEEK(106)*256+PEEK(105)', himem can be
read by 'PEEK (116)*256+PEEK(115)'. Normally, lomem is the top of your
basic program, and himem is the highest available address under the OS
and its buffers; you should not lower the lomem or raise the himem
unless you are darn sure what you're doing. Also, it's usually a good
plan to change the himem or lomem at the start of your program before
using any variables.

ProDOS's BASIC.SYSTEM grabs space on the fly for things, so moving
HIMEM under ProDOS may be a risky job. In all cases, it must be a
multiple of 256 under ProDOS.
_______________________________________________________

Section 3: Input/Output to Text and Graphics screens

3.1 The 'PRINT' command and text formatting

PRINT (can be abbreviated as '?' when typing, but will be expanded
when LISTed) will print string constants ('PRINT "HELLO"'), numeric
constants and expressions ('PRINT 2+2'), strings ('PRINT NAME$'),
reals ('PRINT FOO'), or integers ('PRINT SP%'). It can also print
several of these at once if semicolons (';') or commas are between the
sections: 'PRINT "The Answer:"; 2+2'. Semicolons are not really needed
before the start of a string constant or after them-- 'PRINT
D$"CLOSE"' is perfectly legal.