You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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:
class.js:
The text was updated successfully, but these errors were encountered:
Checklist
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:
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:
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.
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.
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
Additional Context
Sources tab when the memory issue occurs:
class.js:
The text was updated successfully, but these errors were encountered: