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

Getting RangeError: WebAssembly.Memory(): could not allocate memory #2054

Open
3 tasks done
ninjaclown123 opened this issue May 8, 2024 · 1 comment
Open
3 tasks done
Labels
needs-triage Issue needs triage type: bug Something isn't working

Comments

@ninjaclown123
Copy link

Checklist

  • I added a descriptive title
  • I searched for other issues and couldn't find a solution or duplication
  • I already searched in Google and didn't find any good information or help

What happened?

I seem to be running into a memory leak because of the way I'm using PyScript in my project.

My project requires the following cycle to happen:

let c = 0;
function runCode() { 
c++

let res = document.getElementById('result')

res.innerHTML = ''

let newScript = document.createElement('script')

newScript.type = 'py'

newScript.setAttribute('worker', '')

newScript.setAttribute('terminal', '')

newScript.innerHTML = "print('hello')"

res.appendChild(newScript)

console.log(c); 
}

This is a modified section to show as an example but in actual usage, I am taking a python program that is written in a code editor, creating the above script element, setting the type to "py", setting worker attribute and terminal attribute and changing the innerHTML to the code of the python program. When the new script is appended to the result div, I can see the python program executed in the terminal window.

The above is equivalent to:

    <div id='result'>
    <script type="py" terminal worker id="worker">
    print('helloo')
    </script>
    </div>

image

In the above example, I have executed the above with a simple print.

The problem that I have now is that this eventually leads to a memory issue.

image

It seems to happen after running the above process 50 times. I inspected the Memory tab in chrome dev tools and found that it may be due to the workers created with each script not being freed when I clear the DOM. It seems to happen when the total heap size goes beyond 400~410 MB.

image

Is there a way to terminate the workers that are started in my process?

Or, since the Web Worker API has terminate() and close() methods, is there any way to start the same workers but by using the Web Worker API?

Instead of making new pyscript instances, I have also tried using the same instance and using .process() to execute my programs too like here. However, this was kind of clunky for me as the programs written in the code editor would get executed line by line and sometimes they would get entered into the REPL but wouldn't execute at all and would needed to get cleared.

I've tried looking for ways to clear or terminate the workers myself but could not find anything. Maybe there's a better way of doing my process and it would be appreciated to get some direction if that's the case.

What browsers are you seeing the problem on? (if applicable)

Chrome

Console info

MessageEvent {type: 'error', error: RangeError: WebAssembly.Memory(): could not allocate memory
    at http://127.0.0.1:5500/pyodide/py…}
error
: 
RangeError: WebAssembly.Memory(): could not allocate memory at http://127.0.0.1:5500/pyodide/pyodide.asm.js:9:4279 at loadPyodide (http://127.0.0.1:5500/pyodide/pyodide.mjs:9:9269) at async get (blob:http://127.0.0.1:5500/8ceef771-8c75-4a5b-8b7c-4c0dc461d88a:2:7138) at async Object.engine (blob:http://127.0.0.1:5500/8ceef771-8c75-4a5b-8b7c-4c0dc461d88a:2:23347) at async blob:http://127.0.0.1:5500/8ceef771-8c75-4a5b-8b7c-4c0dc461d88a:2:29181
type
: 
"error"

Stack trace: 
Uncaught (in promise) RangeError: WebAssembly.Memory(): could not allocate memory
    at pyodide.asm.js:9:4279
    at loadPyodide (pyodide.mjs:9:9269)
    at async get (8ceef771-8c75-4a5b-8b7c-4c0dc461d88a:2:7138)
    at async Object.engine (8ceef771-8c75-4a5b-8b7c-4c0dc461d88a:2:23347)
    at async 8ceef771-8c75-4a5b-8b7c-4c0dc461d88a:2:29181
    (anonymous) @ class.js:76

class.js screenshot provided in Additional Context

Additional Context

Sources tab when the memory issue occurs:
image

class.js:
image

@ninjaclown123 ninjaclown123 added needs-triage Issue needs triage type: bug Something isn't working labels May 8, 2024
@WebReflection
Copy link
Contributor

Is there a way to terminate the workers that are started in my process?

reach the script in case, then script.xworker.terminate() is all you need. It's WebWorker standard API by all means.

About this report ... if you create gazillion workers you gonna have issues no matter if PyScript or not ... I suggest you don't do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Issue needs triage type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants