KS3 & GCSE Computing · GCSE

Stack Memory vs Heap Memory: GCSE Computer Science Guide

Compare stack memory and heap memory for GCSE Computer Science: how each is allocated, what causes stack overflow and memory leaks, and worked Python examples.

Duke Harewood — author of AI Tutors for Key Stage 3Updated 5 min read

On this page

Short answer

When a program runs, the OS divides RAM into two regions: the stack and the heap. The stack handles local variables and function calls automatically; the heap stores data whose size is not known in advance. Both regions are essential for understanding memory management and recursion at GCSE.

At a glance

Key stage
GCSE
Subject
Computing
Type
Comparison
For
Students
Read time
5 min
Last updated
21 October 2026

Where this fits

  1. Key Stage 3Years 7–9
  2. GCSEYears 10–11This article
This article is aimed at GCSE (Years 10–11), the stage after Key Stage 3 (Years 7–9).

What is the stack and how does it work?

Think of the stack as a neat pile of trays in a canteen. Each time you call a function, a fresh tray — called a stack frame — is placed on top. The frame holds:

  • the function's local variables,
  • the return address (where execution must resume after the function ends),
  • the parameters passed into the function.

When the function returns, its frame is removed from the top of the pile. This is a LIFO (Last In, First Out) structure — the most recently added frame is always the first to be removed.

Stack allocation is managed entirely by the CPU at high speed. The size of a stack frame is known at compile time (or when the function is called), so no searching for free space is needed.

What is the heap and how does it work?

The heap is a large, unordered pool of available memory. Unlike the stack, allocation on the heap does not follow an automatic pattern — the program must explicitly request a block of memory and, in many languages, must also free it when finished.

In Python you do not manage heap memory directly; every time you create an object — a list, a dictionary, a string longer than a small constant — Python's runtime allocates space on the heap. Python's garbage collector periodically identifies objects that nothing in the program still references and reclaims their heap space automatically.

Higher-level languages (Python, Java, C#) manage heap memory for you. Lower-level languages (C, C++) require the programmer to call malloc() and free() explicitly.

How do the stack and heap compare?

Property Stack Heap
Allocation speed Very fast (just move a pointer) Slower (must search for free block)
Size limit Small (typically 1–8 MB per thread) Large (limited mainly by available RAM)
Lifetime of data Tied to the function call Controlled by programmer or GC
Management Automatic (LIFO) Manual (C/C++) or garbage-collected
Typical contents Local variables, return addresses Objects, arrays, dynamically sized data
Access pattern Sequential, predictable Random, fragmented over time

What is a stack overflow?

A stack overflow occurs when a program pushes more frames onto the stack than its fixed size allows. The most common cause in GCSE contexts is infinite or deeply nested recursion: every recursive call adds a new frame; if the base case is missing or never reached, the stack fills and the program crashes with a RecursionError (Python) or StackOverflowException (Java/.NET).

def count_down(n):
    print(n)
    count_down(n - 1)   # no base case — infinite recursion

count_down(5)           # RecursionError: maximum recursion depth exceeded

Adding a base case solves the problem:

def count_down(n):
    if n <= 0:
        return          # base case — stops recursion
    print(n)
    count_down(n - 1)

What is a memory leak?

A memory leak occurs when heap memory is allocated but never released, so the program gradually consumes more and more RAM until it slows down or crashes. Memory leaks are most common in languages where the programmer manages memory manually (C/C++): if malloc() is called but free() is never called, the allocated block is lost.

In Python, memory leaks can still occur if references to large objects are accidentally kept alive — for example, adding items to a list that grows indefinitely without ever removing them. The garbage collector can only free an object when no references to it remain.

Which variables go on the stack and which on the heap?

In Python the distinction is less visible than in C, but conceptually:

  • Stack: the local variable names (bindings) inside each function call.
  • Heap: the actual objects those names point to — lists, dictionaries, class instances, strings.
def example():
    x = 10          # x (the name) is on the stack; 10 (a small int) may be cached
    items = [1, 2]  # items (the name) is on the stack; the list object is on the heap

When example() returns, x and items are popped off the stack. If nothing else references the list [1, 2], Python's garbage collector will eventually free that heap space.

Frequently asked questions

Why is the stack faster than the heap?

Stack allocation is a single pointer adjustment — the CPU simply decrements the stack pointer register to reserve space. Heap allocation requires the allocator to search a list of free blocks, find one large enough, and mark it as used. This search takes unpredictable time, especially when the heap is fragmented after many allocations and frees.

Does every function call use the stack?

Yes. In virtually every programming language, calling a function automatically creates a new stack frame containing the return address and local variables. This is why the stack grows as functions call other functions and shrinks as they return — even a simple print() call in Python involves creating a frame (internally) and removing it on return.

What is the connection between the call stack and recursion in GCSE exams?

GCSE questions on recursion often ask you to trace a recursive function and show the sequence of stack frames. Each recursive call adds a frame with its own copy of the local variables. When the base case is reached, frames are removed one by one (LIFO) and the return values are passed back up the chain. Understanding the stack is the key to correctly tracing and explaining recursive programs.

Can you increase the stack size to prevent stack overflow?

In some environments, yes — Python allows sys.setrecursionlimit(n) to raise the default limit (usually 1,000 recursive calls). However, this is a workaround, not a fix. The proper solution is either to rewrite the recursion with a correct base case or to convert the algorithm to an iterative (loop-based) version, which uses constant stack space regardless of the input size.


For Socratic GCSE tutoring and free Computer Science flashcards, see aitutors.me.

Key terms

  • stack
  • heap
  • stack frame
  • local variables
  • return address
  • parameters
  • LIFO
  • garbage collector

Sources