Ad Space — Top Banner

4 Out of memory

Sinclair ZX Spectrum

Severity: Moderate

What Does This Error Mean?

Error 4 on the ZX Spectrum means the machine has run out of RAM for your BASIC program, variables, or display file. On a 48K Spectrum, you have about 41KB for programs and variables. On a 16K model, only about 9KB is available.

Affected Models

  • Sinclair ZX Spectrum 16K
  • Sinclair ZX Spectrum 48K
  • Sinclair ZX Spectrum 128K
  • Spectrum+
  • Spectrum +2
  • Spectrum +3
  • ZX Spectrum Next
  • Fuse emulator

Common Causes

  • BASIC program too large for available RAM
  • Too many variables or large string arrays consuming memory
  • LOAD or MERGE attempted when insufficient RAM to hold the program
  • On a 16K model, the very limited 9KB BASIC area is easily exhausted
  • Large screen operations (DRAW, CIRCLE with many points) consuming stack space

How to Fix It

  1. Check free memory: type PRINT PEEK 23732 + 256 * PEEK 23733 - PEEK 23641 - 256 * PEEK 23642 and press ENTER.

    This classic Spectrum formula calculates free bytes. If the result is very small or negative, memory is critical.

  2. On a 16K Spectrum: this error is expected for any large program — upgrade to 48K.

    The 16K model has only about 9KB of usable BASIC RAM. Almost all commercial Spectrum software requires 48K. The RAM pack expansion (Sinclair or third-party) solves this permanently.

  3. Remove unused variables with CLEAR.

    CLEAR without arguments frees all variables and resets RANDOMIZE. This is useful partway through a program when earlier variables are no longer needed.

  4. Shorten variable names and remove REM lines.

    Unlike most BASICs, Spectrum BASIC stores the full text of each REM line and variable name. Removing REM comments and using single-letter variable names saves RAM.

  5. On a 128K Spectrum: use the extra RAM banks via BANK commands.

    The Spectrum 128, +2, and +3 have 128KB of RAM in 16KB banks. Programs can be structured to use the extra banks, though this requires more advanced BASIC or machine code.

Frequently Asked Questions

How much RAM does a ZX Spectrum 48K actually give to BASIC programs?

About 41.5KB — from address 23755 to 65535 minus the display file (6912 bytes). The remaining RAM goes to the OS system variables, the display file, and the printer buffer.

Is there a way to load a program that is slightly too large?

Sometimes. Using LOAD "" CODE to load pure machine code into high memory first frees the BASIC area. For pure BASIC programs that are too large, the only real solutions are editing the program to use less RAM or upgrading the machine.