This repository was archived by the owner on Feb 13, 2025. It is now read-only.
Fix memory leak issue caused by reference cycle #27
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Atm there is memory leak issue, caused by cyclic references that
MagicString
andChunk
struct instances hold on to, to reproduce the memory leak issue we can just run the following code snippet and monitor the memory usage:Memory leak comes mostly from
split()
method ofChunk
where a reference cycle gets created at very last couple lines of the method (at least as far as I've been able to detect) which causes instances ofMagicString
to not completely free up the memory after they go out of scope.Proposed Solution
To fix and avoid memory leak issue we simply need to drop all the cyclic references that have been created during
MagicString
method calls when an instance ofMagicString
is going out of scope, so we add a new private method forChunk
to set itsOption<Rc<RefCell>>
toNone
which ensures that all cyclic reference go out of scope explicitly:next we just implement
Drop
trait forMagicString
to callclear()
for itsRc<RefCell<Chunk>>
properties:this ensures that any cyclic reference that has been created during method calls and are persisting even after an instance of
MagicString
goes out of scope, gets dropped explicitly before dropping the wholeMagicString
instance.To check if the fix works, we can just run the snippet above once again and monitor the memory usage.
PS: There are probably other approaches to address and fix this issue, but I found this approach to be very minimal, and it doesn't touch and change any other parts of the code and logic while still guaranteeing no memory leak, as the logic of this lib has many recursive components and changing it might possibly introduce unintended bugs.