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

Possible memory leak in animate #520

Closed
alexvakimov opened this issue May 26, 2021 · 18 comments
Closed

Possible memory leak in animate #520

alexvakimov opened this issue May 26, 2021 · 18 comments

Comments

@alexvakimov
Copy link

I'm using py3Dmol to animate the xyz trajectory file. I use py3Dmol v0.8.0 installed from the Anaconda cloud.

The code goes like

view = py3Dmol.view(width=800,height=400)
view.addModel(xyz,'xyz',{'vibrate': {'frames':10,'amplitude':1}}, viewer=(0,0))
view.setBackgroundColor('0xeeeeee')
view.setStyle({'sphere':{}})
view.animate({'loop': 'backAndForth'})
view.zoomTo()
view.show()

Here, xyz is a string containing the trajectory file.

I noticed that RAM usage steadily increases when the above code is being executed in a Jupyter notebook. If I comment the animate line, there is no problem with the RAM.

I wonder if this had been fixed already. Also, it would be great to have the corresponding fix available in the Conda installer. Please advise.

@alexvakimov alexvakimov changed the title Possible memory in animate Possible memory leak in animate May 26, 2021
@dkoes
Copy link
Contributor

dkoes commented Jun 2, 2021

I've let this example go for many hours in chrome and firefox:
https://colab.research.google.com/drive/1SzC9oqjemfu_UqCPVpi6JnA23Na9I7T_?usp=sharing
and while I see memory go up and down (as is expected with a garbage collected language) I don't see a consistent monotonic increase like I would expect with a true leak.

Can you provide more evidence of a leak? How much is memory increasing and at what rate? What browser and operating system are you using?

@alexvakimov
Copy link
Author

Sorry for the late response. Here are some more details:

I'm running a Jupyter notebook in the WSL on Windows then open it in a browser to run the examples:
image

The tutorial I'm running can be found here

You'd need to have the Libra code installed - the instructions can be found [here] (https://github.com/Quantum-Dynamics-Hub/libra-code/tree/devel) - note the "devel" branch, but probably can be run without it. I had been using py3Dmol with the "animate" a while ago and this wasn't a problem indeed. As of now, regular visualization works fine, but when i use "animate" is starts building up the memory used.

So here are my tests:
I first run the regular (no animate) py3Dmol visualization - the memory usage is notable, but stable - no drift:
image

Then I turn on the animate - to see the effect in a shorter time i run the entire notebook (for more systems) and i can see slow but steady memory usage increase. These are still relatively small systems to visualize - i had done it on bigger ones - the memory usage grows faster there, as expected.

image

after a little while:
image

Then I kill the notebook:
image

Now, even opening the notebook (without running it!) that contains an embedded py3Dmol animation causes the leak.
Here is the memory before opening:
image

Here is the one with:
image

@dkoes
Copy link
Contributor

dkoes commented Jun 14, 2021

Did you try the example I posted? Does the memory leak reproduce there? Is the extra memory in Chrome or Python?

@duerrsimon
Copy link

duerrsimon commented Jun 14, 2021

I experience a similar thing using animated structures but I have increased CPU usage if I reload the cell with the animation a few times. CPU usage does not go down again until I close the notebook in VS Code.

I made a video of how the CPU usage increases:
https://drive.google.com/file/d/1yNhGiehxpLUjrwtX1lQzD4V_ATeFSyYF/view?usp=sharing

The code to reproduce is:

xyz2 = '11\n\nC 0.165626322472 -0.6009485177280001 -0.078208762892 -0.023061929903999998 0.369308436796 -0.104040909752 \nC 0.165626322472 -0.6009485177280001 1.3438201845120001 0.026309394956 -0.084140638088 -0.07855796239200001 \nCL 0.165626322472 1.7078959732640002 -0.5590256428120001 -0.006784128884 -0.064784319652 0.0111827547 \nH 1.1054056915200001 -0.55594994052 -0.568185785804 0.023879796096 -0.05867686266 -0.050372364536000004 \nC -1.042273171888 -0.8983382780320001 -0.881051195544 0.002712364356 0.041081823328000006 -0.02212669212 \nO 0.342364057048 -1.9075831410480002 0.941645701672 0.0061341767240000004 -0.09371214309600001 0.13912094638 \nH 0.999024704912 -0.131344177744 1.843675471768 -0.004855561984 -0.03149522038 -0.033523170772000004 \nH -0.77102128376 -0.375005587528 1.834650201124 0.033826814476 0.008798236824 -0.061177532624 \nH -1.061031733528 -0.288445578096 -1.764225168992 -0.104482391312 -0.087586843968 -0.114009835784 \nH -1.053642499312 -1.9403946672760002 -1.1588758934480001 0.12062744564000001 0.010097960704 0.117174112432 \nH -1.933094934732 -0.6864447730400001 -0.31382666967600004 -0.0019213473240000001 0.009387967784 -0.016421261324000003 \n'

view = py3Dmol.view(width=400, height=400)
view.addModel(xyz2, "xyz", {'vibrate': {'frames':10,'amplitude':1}})
view.setStyle({'sphere':{'scale':0.30},'stick':{'radius':0.25}})
view.setBackgroundColor('0xeeeeee')
view.animate({'loop': 'backAndForth'})
view.zoomTo()
view.show()

I am on windows 10 and I used both the Jupyter Notebook extension within VS Code and a normal jupyter notebook running as a tab in Microsoft Edge. After executing the cell a few times CPU usages increases quite heavily and goes down to idle as soon as the tab is closed (see video and the CPU usage of Visual Studio Code).

@dkoes
Copy link
Contributor

dkoes commented Jun 14, 2021

Does this mean you can't reproduce the problem in a regular browser environment?

@duerrsimon
Copy link

duerrsimon commented Jun 14, 2021

@dkoes I did not try what had been done above but I thought I add my description to the problem as it sounded similar. I get the increased CPU usage both in VS Code and in a Webkit browser (Edge).

I also get the following warnings in the JS console. That might be related to the problem.

image

@dkoes
Copy link
Contributor

dkoes commented Jun 14, 2021

Do you have issues with Chrome?

@duerrsimon
Copy link

duerrsimon commented Jun 14, 2021

Edge and Chrome are both webkit browsers so behavior is similar. I just tested it to be sure.
In Firefox CPU usage after multiple executions of the Jupyter cell also does go up considerably (to 25 % from 4%) on my machine. Firefox DevTools prints:
image

@dkoes
Copy link
Contributor

dkoes commented Jun 14, 2021

Do you see these same error messages when loading the colab example I posted?

What is the output of https://webglreport.com/?v=2 on your machine?

@duerrsimon
Copy link

duerrsimon commented Jun 14, 2021

When I run the colab example locally (using the jupyter instance I have installed) I get the same problem of increased CPU usage and the same webgl warnings.
Left is localhost, right is google colab dev tools.
image

On Google Colab it works fine if I execute the cell multiple times (no increase in CPU usage of Google Chrome) and there is not the same WebGL error as on localhost.

(My jupyter instance runs on wsl but imho this is a webgl problem).

Here is the webgl report. Looks all fine to me.
image
image
image

@dkoes
Copy link
Contributor

dkoes commented Jun 16, 2021

I can't reproduce this in a local Jupyter notebook either (notebook server 6.0.3, python 3.8.5). I am increasingly skeptical that this is a 3Dmol.js issue as opposed to with something else in your stack (e.g. WSL GPU acceleration). Can you reproduce it outside of WSL? What process is increasing in memory, the browser or python?

@duerrsimon
Copy link

duerrsimon commented Jun 16, 2021

I tried on my windows notebook with jupyter running in wsl 1 and the browser running in the normal windows environment and on my Debian workstation running the jupyter server and browser natively. I can check the specific versions tomorrow and what happens if I kill the jupyter server keeping the browser open if you want.
For me the increased CPU usage occurs if I quickly execute the cell several times but I also noticed the increased Cpu usage when I was working on a notebook for an hour or so rexecuting the cell multiple times.

On my Linux workstation the Cpu usage was ~3% before execution of the cell multiple times and ~20% after. I'm on a 20 core, 128 gb RAM, 2x GPU machine. The cpu usage is from the browser process.

@duerrsimon
Copy link

I can also reproduce the problem in a hosted Jupyter Lab instance. It is definitely the browser process using the CPU ressources.
See here where I first reexecute the cell about 15 times which constantly increases CPU usage and then I first shut down the kernel (no change in usage). The tab which uses all the resources is the one running 3dmol.js.

PDydlGCYN9.mp4

This is using Python 3.6.9, Jupyter Core 4.7.1, ipython, 7.16.1, ipykernel 5.5.0, jupyter client 6.1.12

@dkoes
Copy link
Contributor

dkoes commented Jun 18, 2021

Can you provide a link to the hosted instance?

dkoes added a commit that referenced this issue Jun 18, 2021
This might address Issue #520.  It at least should reduce cpu usage when
an animated cell is reloaded a bunch of times.
@duerrsimon
Copy link

Sorry it is a private university resource running on jupyterlab 1 so you can unfortunately not get acces. Interestingly though I cannot reproduce the issue on MyBinder. I just tried it.
Let me know if you want me to test the commit you just made :) And thanks for the awesome library by the way :)

@dkoes
Copy link
Contributor

dkoes commented Jun 18, 2021

Sure, go ahead and test. If you're using py3Dmol as long as you clear your cache you should get the new code.

@dkoes
Copy link
Contributor

dkoes commented Sep 9, 2021

Is this resolved? Can the issue be closed?

@duerrsimon
Copy link

From my side it is resolved. Sorry about not getting back to you. Thanks again for adressing this.

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

No branches or pull requests

3 participants