Skip to content

manual GC cleanup#4147

Draft
advaita-saha wants to merge 8 commits intomasterfrom
memory-opt-v2
Draft

manual GC cleanup#4147
advaita-saha wants to merge 8 commits intomasterfrom
memory-opt-v2

Conversation

@advaita-saha
Copy link
Copy Markdown
Contributor

Attempt to fix the sawtooth nature of the memory graph

  - sink params consume the Block at each call boundary, reducing concurrent copies from ~4 to 1
  - Explicit body reset in validateBlock frees seq[Transaction] immediately after persistence, not when GC collects the async frame
  - Closure handler nil-out in processQueue breaks the reference chain from the queue item to the captured Block
  - move at the fetch site clears the source blocks[n] immediately
@advaita-saha
Copy link
Copy Markdown
Contributor Author

Comparing master with memory-opt-v2 ( 5d597f6 )

Screenshot_from_2026-04-18_23-05-09

parentBlk: BlockRef,
txFrame: CoreDbTxRef,
blk: Block,
blk: sink Block,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use sink here? Block is a stack variable so shouldn't effect the heap memory usage.

# per-arena free-lists by default; without this, RSS doesn't shrink after
# disposed frames are reclaimed.
proc mallocTrim(pad: csize_t): cint {.importc: "malloc_trim", header: "<malloc.h>".}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not available on MacOS or windows, does this fix work on those platforms? Or does it depend on using malloc_trim?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rss memory does shrink if there's memory pressure - there's no need for this kind of trimming generally, ie RSS is not that relevant

var memAfter = 0
if c.persistedCount > 0:
memBefore = getOccupiedMem()
GC_fullCollect()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to avoid gc_fullcollect as much as possible as it is very expensive..

The way to get rid of sawtooth is to find reference cycles and explicitly break these with = nil - what gc_fullcollect does is essentially to collect cycles - the sawtooth happens because the cycle collector runs rarely (and it runs rarely because it's very very slow)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants