-
Notifications
You must be signed in to change notification settings - Fork 127
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
Memory managment of serialization lib on javascript #542
Comments
@AngelCastilloB , you do need to free every single object ever produced. Nearly always any data passed into a Rust call or received from Rust is cloned, to stop consuming of values from JS and preserve the usual JS code expectations. This means that there's no direct link between the value you have created in your JS code, e.g. a datum, and the value that is contained within a transaction struct, once you have passed that datum in - that information is cloned at the moment of passing it into a setter. |
thanks @vsubhuman for the clarification. I just ran into a memory leak that it seems I cant control. from_bech32 from the address object leaks if an invalid address is passed, and doesn't returns an object or a pointer that I can free, this code reproduces the issue:
After a few seconds you start getting |
@AngelCastilloB , if I remember things correctly the issue is not in this function specifically but in the bindgen processing of The only solution to this is to not rely on errors as business-logic, instead check values separately and then only call the function when you expect for the error to not happen. We can add |
@vsubhuman @AngelCastilloB I believe this is what you're referring to rustwasm/wasm-bindgen#1963 which has been fixed already. Just update the dependency, no need to add functionality. |
Note: at least when we updated it in CML, it had some changes in how errors were handled (since there are both |
Nice! Thank you, @rooooooooob! We'll look into updating then and might have this resolved by the next version |
@AngelCastilloB , this is actually my mistake to agree this is an issue that needs fixing. In your example the memory leak is not caused by the library at all. In the current latest library version just having a raised error does not cause a leak and if you change your example to something like this, it will not demonstrate a leak:
There will be no memory leak visible. The problem in your code example is that logging an object in JS stops it from being garbage collected, so you just force all produced error instances to be stored forever. Even if you try logging anything, like an empty string, on every single
Causes a leak. But:
Does not cause a leak (actually does, but just 100K times slower). |
Hi @vsubhuman thanks for the clarification, this was just an example, our production code does not has that log line, our problem is that the library crashes with an out of bound memory error after a while, we tracked down to that Btw I repeated the experiment with the cardano multiplatform lib, and the crash doesnt happen, I inspected the glue code generated by the CSL:
vs the CML:
And it seems that this extra code added is preventing the crash, which does makes me believes that is related to the bug in wasm_bindgen that @rooooooooob mentioned rather than the memory leak itself |
@AngelCastilloB , understood. Try version |
@vsubhuman I apologizes for the late reply, I did tested it and indeed it seems the problem is solved or at least improved, I could run my sample code for a long time without a crash |
@AngelCastilloB, thank you for the feedback! 🙌 |
A non-beta version |
We (MLabs) came up with a hack that ties JS objects to WASM-allocated memory using https://www.npmjs.com/package/csl-runtime-gc Well, FinalizationRegistry does not provide strict guarantees, but it works surprisingly well for us in CTL. |
@lisicky thank you! We didn't know about it. Why isn't CSL compiled with it by default?| UPD: actually WASM_BINDGEN_WEAKREF throws exceptions attempting to double-free at runtime when we run our tests |
Hi, I am currently trying to implement proper memory management and disposal of the CSL objects in the projects I have that use this library, but I would like to understand how far I need to go to properly dispose of all the objects, do I need to call free on every object that I create using one of the constructors, or is it enough to free the parent object?, for example, when creating a transaction we need to provide all sort of objects (redeemers, datums, signatures, input, outputs etc), is it enough to free the transaction where these objects have been added to it or we must free all of the objects individually? The same question goes for objects we get from other objects, do we need to manually free all these objects returned by the getters, or is it enough to free the parent object?.
The text was updated successfully, but these errors were encountered: