Skip to content

fix/issues-61-62: Batch the module purge, and set a Go memory limit from the container's - #87

Merged
rahlk merged 3 commits into
mainfrom
fix/issues-61-62-neo4j-push-memory
Nov 30, 2026
Merged

rahlk merged 3 commits into
mainfrom
fix/issues-61-62-neo4j-push-memory

Conversation

@rahlk

@rahlk rahlk commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

tl;dr An --eager -t push now deletes a changed module in batches, and in a container cango keeps its heap under 85 % of the container's memory. Gitea's push in a 16 GB container now peaks at 13.8 GiB, down from 15.3 GiB.

Motivation and Context

  • Bulk inserts to neo4j fails with MemoryPoolOutOfMemoryError #61: the Neo4j server refuses a transaction that uses more than its transaction memory limit.
    • Upserts: node and edge batches were already limited to 5,000 rows, and since --artifact-text uses approximately 5 times the file size in memory #80 a node batch is also sent when its text reaches 16 MiB. I measured the batch size on nats-server with the Neo4j settings from the issue (8G heap, dbms.memory.transaction.total.max=5600m). Every run pushed 1,858,128 nodes and 10,420,173 relationships:

      Rows per batch Push run Largest transaction sampled cango peak RSS
      2,000 890 s
      5,000 362 s 22 MiB 5.4 GiB
      10,000 363 s 42 MiB 6.2 GiB
      20,000 353 s 6.3 GiB

      The 5,000 row is the second of two runs; the first (494 s) was slower only because it ran first. Larger batches are no faster and use more memory, so the batch size stays at 5,000.

    • Deletes: the per-module purge of an --eager -t push deleted a whole module in one transaction, so a large module could exceed the limit. It now runs as CALL (n) { DETACH DELETE n } IN TRANSACTIONS OF 5000 ROWS, like the whole-app purge of an eager full push already did.

    • pruneVanishedModules is removed. It ran at the end of an eager full push, after the whole-app purge had already deleted every Go node of the app, and the push writes back only the modules that still exist. It never deleted anything.

  • Reduce analysis memory at -a 4 #62: the analysis itself still holds most of the memory, but Go was not told how much memory it has.
    • In a container (cgroup v2), cango now sets Go's memory limit to 85 % of the container's limit (/sys/fs/cgroup/memory.max). A GOMEMLIMIT you set still takes precedence, and nothing changes outside a container.
    • Under -v, cango logs its memory after the analysis, after the JSON write, and after each Neo4j push pass: live heap, memory from the OS, and peak RSS. The memory test prints these lines.

Measurements (--emit neo4j in a 16 GB container, Neo4j 5.26 with the issue's settings; one run each):

App main This branch
nats-server 6.2 GiB peak, 380 s 5.7 GiB peak, 353 s
gitea v1.25.3 15.3 GiB peak, 1,505 s 13.8 GiB peak, 1,458 s

Gitea's memory on this branch, from the new log lines (Go memory limit 13.60 GiB):

After Live heap From the OS Peak RSS
Analysis 7.06 GiB 10.06 GiB 9.90 GiB
Neo4j pass 1 3.90 GiB 8.94 GiB 11.43 GiB
Neo4j pass 2 1.51 GiB 3.58 GiB 13.37 GiB

Gitea meets the issue's target of 14 GiB, with 0.2 GiB to spare. The peak comes during the second push pass, while the L4 result is still in memory. A more compact L4 result is the next step for more margin, as a follow-up issue.

How Has This Been Tested?

  • Unit tests:
    • the Go memory limit is 85 % of the cgroup limit (16 GiB gives 14,602,888,806 bytes), and is not set for max, a missing or unreadable file, a malformed value, or when GOMEMLIMIT is set;
    • the module purge and the whole-app purge both run in transactions of 5,000 rows;
    • the memory test keeps only cango's memory lines from its logs.
  • Live Neo4j tests (-tags neo4j_it), with the database's per-transaction limit at 128 MiB (NEO4J_db_memory_transaction_max=128m):
    • an --eager -t push purges a module with 60,600 callables, which one transaction cannot delete under that limit;
    • an eager full push removes a module that disappeared from the app, now that pruneVanishedModules is gone;
    • all 10 live tests pass.
  • The memory test (-tags memtest) passes on nats-server and gitea, with the results above.
  • go test ./... and golangci-lint pass.

Breaking Changes

None. The graph a push writes is unchanged. In a container, Go now collects garbage sooner as the heap nears 85 % of the container's memory.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the Codellm-Devkit Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

…othing (#61)

An --eager -t push purged each changed module in one transaction, so a
large module could exceed the server's transaction memory limit. The
purge now runs as CALL { DETACH DELETE } IN TRANSACTIONS OF 5000 ROWS,
like the whole-app purge.

pruneVanishedModules ran on an eager full push after the whole-app purge
had already deleted every Go node of the app, so it never found anything.
It is removed, and the stager's end no longer takes the module ids. A live
test pins that an eager full push leaves no vanished module.

The batched-purge live test needs a database with a per-transaction limit
(db.memory.transaction.max=128m) and skips without one.
… per stage (#62)

In a container with a cgroup v2 memory limit, cango now sets the Go
memory limit to 85 % of it, unless GOMEMLIMIT is set. The GC then
collects harder near the limit, instead of letting garbage push the
process over it. With no cgroup limit (macOS, a plain host) nothing
changes.

Under -v, cango logs the live heap, the memory held from the OS and the
peak RSS after the analysis, after each Neo4j pass and after the JSON
output, so a run's log shows which stage reaches the peak.
@rahlk
rahlk force-pushed the fix/issues-61-62-neo4j-push-memory branch from b716798 to c95e56b Compare November 30, 2026 20:31
@rahlk
rahlk merged commit 4004ff6 into main Nov 30, 2026
4 of 6 checks passed
@rahlk rahlk added the bug Something isn't working label Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce analysis memory at -a 4 Bulk inserts to neo4j fails with MemoryPoolOutOfMemoryError

1 participant