Skip to content
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

The webR Shelter mechanism sometimes leaks memory to the Global Shelter #423

Open
christianp opened this issue May 9, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@christianp
Copy link

I'm writing this issue at the end of the day so haven't spent the time to make a demo of this, but I'm logging it now in case you can tell me the answer anyway!

I noticed that Numbas seems not to destroy the R objects created when it runs code. For each session, we create Shelter and and REnvironment objects, run several blocks of code, and then run shelter.purge(), since that's the only method I could see that looked like it cleared memory.

However, something's still being retained between session - after running code that makes a vector of a million numbers in about 200 separate sessions, I start getting "can't allocate a vector" errors.

What else should I be doing?

@georgestagg
Copy link
Member

You could try clearing the Global shelter at the same time as purging your own user Shelter:

webR.globalShelter.purge();

Without looking at the extension code to check, you could be hitting a situation where we're unable to protect R objects in the user Shelter for whatever reason, and so they're protected globally instead. We'd like to eliminate those situations eventually, but I know a few corner cases still exist.

You could also try explicitly asking R to invoke its internal garbage collector, after purging the shelters:

webR.evalRVoid("gc()");

@christianp
Copy link
Author

It looks like purging the global shelter does fix it.

For reference, here's a minimal example that just runs the same code repeatedly in new shelters until it runs out of memory: https://webr-not-clearing-memory.think.somethingorotherwhatever.com/. On my PC, it fails after 25ish runs.

And here's a version that also purges the global shelter: https://webr-clear-memory-purge-globalshelter.think.somethingorotherwhatever.com/. I let it get to 100 runs before stopping.

Should I leave this issue open as a case where the objects aren't protected in the user shelter?

christianp added a commit to numbas/numbas-extension-programming that referenced this issue May 10, 2024
We want R to begin as if from new for each session, so it's OK to purge
the global shelter as well as the user shelter created for the session,
in case there's anything that WebR is protecting globally.

see r-wasm/webr#423
@georgestagg
Copy link
Member

Thanks for the really clear examples! Yes, please leave this open for now.

@georgestagg georgestagg added the bug Something isn't working label May 13, 2024
@georgestagg georgestagg changed the title How do I clear memory? The webR Shelter mechanism sometimes leaks memory to the Global Shelter May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants