Skip to content

feat: release unused python memory#274

Open
PeterStaar-IBM wants to merge 8 commits into
mainfrom
dev/release-unused-python-memory
Open

feat: release unused python memory#274
PeterStaar-IBM wants to merge 8 commits into
mainfrom
dev/release-unused-python-memory

Conversation

@PeterStaar-IBM
Copy link
Copy Markdown
Member

@PeterStaar-IBM PeterStaar-IBM commented May 20, 2026

Summary

This change introduces a configurable native memory trimming policy to reduce allocator fragmentation during page processing.

resolving: #273, #227

What changed

  • Added release_native_memory_every_n_pages to DecodePageConfig
    • 0 disables trimming
    • N > 0 triggers trimming every N processed pages
  • Added a shared cross-platform helper for native memory release:
    • glibc: malloc_trim(0)
    • macOS: malloc_zone_pressure_relief(nullptr, 0)
    • Windows: HeapCompact(GetProcessHeap(), 0)
  • Added an internal atomic page counter to the threaded processing base and invoke trimming after completed page results
    are produced
  • Applied the same throttled trimming policy to the non-threaded docling_parser path, using the decode_config passed to
    get_page_decoder(...)
  • Exposed the new config field through pybind and propagated it through the Python config copy helper

Design notes

  • Trimming is now driven by decode-time configuration instead of unconditional calls in unload paths
  • The policy is shared across:
    • threaded parser
    • threaded renderer
    • non-threaded parser
  • The setting is named for native memory, not Python memory, since this operates on the process allocator rather than
    Python GC

Expected impact

  • Reduces long-run native heap fragmentation during large decode workloads
  • Avoids trimming on every page by making the cadence configurable
  • Keeps the feature opt-in by default via 0 disabled

Example

cfg = DecodePageConfig()
cfg.release_native_memory_every_n_pages = 50

Signed-off-by: Peter Staar <taa@zurich.ibm.com>
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented May 20, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@github-actions
Copy link
Copy Markdown
Contributor

DCO Check Passed

Thanks @PeterStaar-IBM, all your commits are properly signed off. 🎉

Signed-off-by: Peter Staar <taa@zurich.ibm.com>
@simonschoe
Copy link
Copy Markdown

@PeterStaar-IBM awesome, really appreciate that your are relentlessly working on the core issue. Kudos to you and the team! 🫶

Signed-off-by: Peter Staar <taa@zurich.ibm.com>
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.

2 participants