Skip to content

Conversation

@ludfjig
Copy link
Contributor

@ludfjig ludfjig commented Sep 16, 2025

JavaScript components leak memory when returning non-primitive types (strings, objects, arrays, etc.).

Cause

Return pointer allocation used JS_realloc which bypasses memory tracking, so allocations were never freed.

Fix

Use cabi_realloc instead to ensure proper tracking and cleanup.


Before:

RES memory leaks indefinitely as can be seen in repro provided in #279:

Loading js component from: /home/ludde/leak/runner/../component/js/handler.wasm
Finished compiling component
Testing string echo (should leak)...
String Iteration: 0, Virtual: 5218636 KB, RES: 785404 KB
String Iteration: 500000, Virtual: 5218896 KB, RES: 838668 KB
String Iteration: 1000000, Virtual: 5218896 KB, RES: 845532 KB
String Iteration: 1500000, Virtual: 5218896 KB, RES: 852132 KB
String Iteration: 2000000, Virtual: 5218896 KB, RES: 855828 KB
String Iteration: 2500000, Virtual: 5218896 KB, RES: 862428 KB
String Iteration: 3000000, Virtual: 5218896 KB, RES: 870348 KB
String Iteration: 3500000, Virtual: 5218896 KB, RES: 878796 KB
String Iteration: 4000000, Virtual: 5218896 KB, RES: 886188 KB
String Iteration: 4500000, Virtual: 5218896 KB, RES: 893580 KB
String Iteration: 5000000, Virtual: 5218896 KB, RES: 902028 KB
.
.
.
String Iteration: 17000000, Virtual: 5218896 KB, RES: 1089204 KB
...

After:

RES memory stabilizes after X guest call iterations:

Loading js component from: /home/ludde/leak/runner/../component/js/handler.wasm
Finished compiling component
Testing string echo (should leak)...
String Iteration: 0, Virtual: 5349704 KB, RES: 779232 KB
String Iteration: 500000, Virtual: 5349964 KB, RES: 821192 KB
String Iteration: 1000000, Virtual: 5349964 KB, RES: 821192 KB
String Iteration: 1500000, Virtual: 5349964 KB, RES: 821192 KB
String Iteration: 2000000, Virtual: 5349964 KB, RES: 821192 KB
String Iteration: 2500000, Virtual: 5349964 KB, RES: 821192 KB
String Iteration: 3000000, Virtual: 5349964 KB, RES: 821192 KB
String Iteration: 3500000, Virtual: 5349964 KB, RES: 821192 KB
String Iteration: 4000000, Virtual: 5349964 KB, RES: 821192 KB
String Iteration: 4500000, Virtual: 5349964 KB, RES: 821192 KB
String Iteration: 5000000, Virtual: 5349964 KB, RES: 821192 KB
...

Closes #279

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@vados-cosmonic
Copy link
Contributor

Thanks for this @ludfjig ! @tschneidereit would you mind taking a look at this?

Copy link
Member

@tschneidereit tschneidereit left a comment

Choose a reason for hiding this comment

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

Yeah, that's definitely the right thing to do—thank you for tracking this down!

@tschneidereit tschneidereit merged commit efb1b6d into bytecodealliance:main Sep 16, 2025
12 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.

Continuous memory growth in jco component

3 participants