Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:416rehman DeepZero Memory Safe Parallelism

From Leeroopedia
Knowledge Sources
Domains Optimization, Reverse_Engineering
Last Updated 2026-10-27 00:00 GMT

Overview

Auto-derive a memory-safe worker count for Ghidra headless analysis so `parallel: 0` cannot exhaust RAM — each JVM worker holds ~4 GiB of program database in memory.

Description

Each Ghidra headless worker is a separate JVM holding a full program database in memory (~4 GiB during analysis). Letting `parallel: 0` (auto-scale to CPU count) spawn one worker per core on a many-core machine would blow through available RAM and cause swapping or OOM kills. The `GhidraDecompile` processor overrides `max_parallelism()` to derive a safe worker count bounded by three factors: CPU count, available physical RAM divided by ~4 GiB per worker, and a hard cap of 16. When RAM cannot be measured, it falls back to a conservative `min(cpu, 4)`.

Usage

Use this heuristic when configuring Ghidra decompilation parallelism in a pipeline. Set `parallel: 0` (auto) to let the processor pick a memory-safe default, or set `max_parallel` in the processor config to raise the ceiling on a large machine (e.g. `max_parallel: 24`). Never set `parallel: N` higher than `RAM_GiB / 4` on the target machine.

The Insight (Rule of Thumb)

  • Action: Set `parallel: 0` (auto) in the pipeline stage config. The processor's `max_parallelism()` derives the worker count.
  • Value: `workers = max(1, min(cpu_count, floor(RAM_GiB / 4.0), 16))`. Falls back to `max(1, min(cpu, 4))` when RAM is unmeasurable.
  • Trade-off: Fewer workers means slower throughput but prevents OOM. Raising `max_parallel` trades RAM headroom for speed on large machines.
  • Compatibility: Applies to the `GhidraDecompile` processor only; other processors do not spawn memory-heavy JVMs.

Reasoning

A Ghidra headless analysis loads the entire binary into an in-memory program database. On a 64-core machine with 64 GiB RAM, auto-scaling to 64 workers would need 256 GiB — four times available RAM. Bounding by `RAM / 4 GiB` yields 16 workers (also the hard cap), which fits in 64 GiB. On a 16-core machine with 32 GiB, it yields 8 workers. The fallback to `min(cpu, 4)` when RAM is unknown is deliberately conservative — 4 workers is safe on virtually any machine that can run Ghidra.

Code Evidence

Memory-per-worker constant and cap from `ghidra_decompile.py:22-26`:

# each ghidra headless worker is a separate JVM holding a full program
# database in memory (roughly this many GiB during analysis). auto worker
# counts are derived from this so `parallel: 0` cannot exhaust RAM.
_GHIDRA_GB_PER_WORKER = 4.0
_GHIDRA_MAX_AUTO_WORKERS = 16

Auto worker derivation from `ghidra_decompile.py:66-77`:

def _auto_ghidra_workers(cpu: int | None = None, ram_gb: float | None = None) -> int:
    cpu = cpu or os.cpu_count() or 4
    ram = ram_gb if ram_gb is not None else _total_ram_gb()
    if ram is None:
        return max(1, min(cpu, 4))
    by_ram = max(1, int(ram // _GHIDRA_GB_PER_WORKER))
    return max(1, min(cpu, by_ram, _GHIDRA_MAX_AUTO_WORKERS))

Integration with runner's parallelism resolution from `runner.py:428-444`:

def _resolve_parallelism(self, spec: StageSpec, processor: Processor) -> int:
    parallelism = spec.parallel
    if parallelism > 0:
        return parallelism
    parallelism = os.cpu_count() or 4
    ceiling = processor.max_parallelism()
    if ceiling is not None and 0 < ceiling < parallelism:
        log.info(
            "%s: capping auto concurrency to %d workers (processor limit)", spec.name, ceiling
        )
        parallelism = ceiling
    return parallelism

Config ceiling override from `ghidra_decompile.py:112-120`:

# ceiling on auto (parallel: 0) concurrency. 0 = derive a memory-safe
# default (~4 GiB RAM per worker; each worker is a full JVM). raise it
# to use more of a large machine, e.g. max_parallel: 24.
max_parallel: int = 0

def max_parallelism(self) -> int | None:
    if self.config.max_parallel and self.config.max_parallel > 0:
        return self.config.max_parallel
    return _auto_ghidra_workers()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment