Skip to content

Fix *vm.VM memory leak #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

diegommm
Copy link
Contributor

@diegommm diegommm commented Jul 4, 2025

Currently, it's reslicing using [0:0] (which is equivalent to [:0]), but this does not clear the memory references in the underlying array, which prevents garbage collection. For instance, if:

  • A VM runs a program and pushes N items to the stack
  • The last item is (or contains) a pointer or reference type
  • The VM is later reused, but it always pushes less than N items to the stack

Then the Nth old item will not be garbage collected because there is still a reference to it in the underlying array.

So we need to "clear the tail" of the slice before reslicing to avoid this. See these example issues:

@diegommm diegommm marked this pull request as ready for review July 4, 2025 18:21
@diegommm diegommm changed the title Fix VM not clearing memory references when reused Fix *vm.VM memory leak Jul 4, 2025
@antonmedv
Copy link
Member

Slightly slower bench, but this is expected.

_expr_reuseVm-4               70.73n ± 1%   75.88n ± 1%   +7.28% (p=0.001 n=10)

Reusing VM still faster than creating stack vm each time:

_expr-4                       114.2n ± 0%   119.9n ± 3%   +4.99% (p=0.000 n=10)
_expr_reuseVm-4               70.73n ± 1%   75.88n ± 1%   +7.28% (p=0.001 n=10)

@antonmedv antonmedv merged commit 1128642 into expr-lang:master Jul 5, 2025
17 checks passed
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