Skip to content

Memory Management

opencode-agent[bot] edited this page May 9, 2026 · 2 revisions

Memory Management

JNode is a monolithic Java OS with a single shared address space. Memory management involves both hardware paging (x86) and object-level Garbage Collection (GC).

Overview

Unlike traditional operating systems that isolate processes into separate virtual address spaces, JNode operates in a Single Address Space. All code—kernel, drivers, applications—shares the same memory space. Process isolation is achieved purely through Java's type safety and JNode's Isolate abstraction.

Memory management in JNode bridges the gap between raw physical memory pages (managed by assembly) and Java objects (managed by the GC).

Architecture

┌──────────────────────────────────────────────────┐
│             Isolates (Applications)              │
├──────────────────────────────────────────────────┤
│             Garbage Collector (MMTk)             │
│   Allocates objects, reclaims unreachable memory │
├──────────────────────────────────────────────────┤
│        VmHeapManager / MemoryBlockManager        │
│    Requests blocks of memory from the system     │
├──────────────────────────────────────────────────┤
│       Paging (mm32.asm / mm64.asm)               │
│     Maps virtual addresses to physical pages     │
└──────────────────────────────────────────────────┘

Native Memory (Paging)

Physical memory layout and hardware page tables are initialized in assembly during boot:

  • 32-bit (core/src/native/x86/mm32.asm): Sets up a flat 4GB address space. The first 4MB are mapped using 4KB pages (with page 0 unmapped to catch null pointer dereferences). The rest of the address space uses 4MB pages.
  • 64-bit (core/src/native/x86/mm64.asm): Sets up the Page Map Level 4 (PML4) tables, transitioning the CPU into long mode and establishing a flat 64-bit address space.

Once initialized, the VM abstracts physical memory regions via the MemoryResource API.

Heap Management & Garbage Collection

JNode supports pluggable heap managers.

1. Default GC (org.jnode.vm.memmgr.def)

  • Location: core/src/core/org/jnode/vm/memmgr/def/
  • Mechanism: A basic, stop-the-world, mark-and-sweep garbage collector native to JNode.
  • Components:
    • DefaultHeapManager: Manages the overall heap.
    • GCManager: Orchestrates the GC cycle.
    • GCMarkVisitor / GCSweepVisitor: Traverses the object graph and frees memory.
    • DefaultWriteBarrier: Used to track cross-generational or cross-region references.

2. MMTk (org.jnode.vm.memmgr.mmtk)

  • Location: core/src/mmtk-vm/org/jnode/vm/memmgr/
  • Mechanism: JNode includes bindings for the Memory Management Toolkit (MMTk), a high-performance memory management framework originating from the Jikes RVM. MMTk provides advanced algorithms like generational and concurrent collection.
  • Supported Plans: JNode integrates MMTk plans such as genrc (Generational Reference Counting), ms (Mark-Sweep), and nogc (No Garbage Collection).

The Boot Heap

Because Java objects are created during the build process (by BootImageBuilder), the boot image itself contains a serialized Java heap.

  • VmBootHeap: A special heap representation that wraps the pre-allocated objects baked into the .iso. This heap is immutable at runtime (objects cannot be freed), but new objects are allocated in the dynamic heaps.

Memory Interoperability

Drivers often need to interact with physical hardware via DMA (Direct Memory Access). This requires bridging Java references with raw physical addresses.

  • org.jnode.system.resource.MemoryResource: Represents a raw chunk of mapped memory (e.g., video RAM).
  • Address Unboxing: JNode uses org.vmmagic.unboxed.Address to pass raw memory pointers through Java code without incurring the overhead of object allocation.

Related Pages

  • Boot-Sequence — How kernel.asm and mm32.asm initialize memory.
  • Paging-Implementation — Deep dive into x86 page table setup, identity mapping, and guard pages.
  • VM-Magic — Explanation of Address and other unboxed types used for memory access.
  • Architecture — How Isolates provide separation in a single address space.

Clone this wiki locally