Classic Computer Magazine Archive COMPUTE! ISSUE 61 / JUNE 1985 / PAGE 10

Garbage Collection And Backups

I have seen several references to "garbage collection" in connection with the operation of the Commodore 64, but I have not seen the same term used in connection with operation of the Apple, Atari, or IBM computers. Is the garbage collection routine peculiar to the Commodore, or does it exist on these machines as well?

I have also seen several programs advertised for the 64 which claim to allow you to copy an entire disk. This command exists in DOS on the IBM and Atari machines. Do these programs have some other purpose? Similarly, some of the software for the 64 says it will allow you to copy files from one disk to another. Does this command also not reside in DOS on the 64?

Pat O'Neil

The mysterious-looking term garbage collection has to do with strings. Strings are tricky for a computer to handle. The computer must set aside enough room in memory for all the strings declared in a program. The length of a string can change as a program runs, causing memory allocation problems. Some BASICs, such as Atari BASIC, tackle this problem by making you DIMension every string before using it for the first time. This sets the maximum length of the string. Atari strings always end up at a single spot in memory after the program starts, and do not move.

Microsoft BASIC uses a different trick. When a string is created, the actual string is stored at the top of BASIC memory. The string's name, length, and a pointer to the address of the string are stored after your program in memory. If you change the string, the new string is copied to the current top of memory (below any other strings) and the pointer is changed. Sooner or later, the strings fill all memory from the top until they collide with variables, arrays, or the end of your program.

This is where garbage collection steps in. A garbage collection routine in BASIC looks at each string, finds the string data, and repacks each string back to the top of memory, discarding unused strings along the way. This can take a while. It's difficult to predict just when garbage collection will occur, and when it does, the machine appears to lock up (in some cases, for more than 20 minutes).

Garbage collection is also a problem on the Apple and IBM machines (and on most Microsoft BASICs), but you can restrict the size of string space on the IBM. This forces garbage collection to occur more frequently, and since the size of the string buffer is small, garbage collection never takes very long. If an IBM program uses a lot of strings, it is sometimes necessary to expand the size of the string area, with the resulting garbage collection problem.

Your question about Disk Operating Systems (DOS) points out some differences between the way a Commodore computer implements DOS and the method used by almost every other computer. Most computers use a RAM-resident DOS to control the disk drive, but Commodore's DOS is in ROM within the drive itself. While this saves user memory, it creates some problems. The 64 and the 1541 drive are like separate computers, and must communicate over a slow serial bus. Commands are sent to the drive to scratch, rename, format, etc. There is a built-in copy command, but it can only copy a file to the same disk. The drive has no way to directly communicate with another disk drive, so the computer must act as a go-between.

The programs you mention let you copy files or disks by reading the disk into computer memory, then copying from memory to another disk. Since Commodore DOS is in the disk drive, there are no built-in programs in the 64 to perform transfers between disks—hence the need for the programs you mention. Many disk duplicators also try to defeat copy-protection schemes, since it's otherwise impossible to back up commercial software.