Skip to content

Release parsed tree and source lines after convert() (#1725) - #1727

Open
tony1233200 wants to merge 1 commit into
Python-Markdown:masterfrom
tony1233200:fix/1725-release-tree-after-convert
Open

tony1233200 wants to merge 1 commit into
Python-Markdown:masterfrom
tony1233200:fix/1725-release-tree-after-convert

Conversation

@tony1233200

Copy link
Copy Markdown

Description

Closes #1725

After Markdown.convert() serializes a document, the parsed ElementTree stayed reachable from self.parser.root, InlineProcessor.parent_map / stashed_nodes held every element, and self.lines kept the split source — so a reused instance (the documented MkDocs/Pelican pattern of one instance + reset() between documents) held onto the last document's full tree.

This clears each reference once its stage is done with it:

  • self.lines = [] after parseDocument()
  • root, newRoot and self.parser.root set to None after serializing
  • InlineProcessor.parent_map / stashed_nodes reset to empty dicts at the end of run() (both are rebuilt at the start of every run)

Verified with tracemalloc on the repo's own docs/ folder: retained memory after reset() drops to ~0 MiB (the issue measured 2.7 MiB retained on a 0.29 MiB source before the fix). Repeated converts on one instance still produce identical output.

AI Assistance Disclosure

  • AI tools were used and I have disclosed which ones, and have fully reviewed and verified their output.

    AI tools were use to assist with the following:

    • Code
    • Code Comments
    • Documentation
    • Changelog Entry
    • Commit Message
    • The Description Above
    • Other: ______________

    (AI assistant: Muse, used as a coding agent; the fix and tests were run and verified locally.)

Checklist

…#1725)

Clear the references that kept the last document alive on a reused
Markdown instance: source lines after parsing, the parsed tree after
serialization, and InlineProcessor's parent_map/stashed_nodes at the end
of its run. Measured with tracemalloc, retained memory after reset()
drops from MiBs to ~0.
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.

Parsed tree and source lines stay in memory after convert()

1 participant