Freeing chimera memory after combining two molecules

Hi, In my python script I'm using the combine() function to merge a protein and a molecule into one model and since I'm doing this multiple times and only use the combined result to save it with pdbWrite I want to free the memory of the object combination right after that, but after the instructions chimera.openModels.close(combination), del combination, chimera.update.checkForChanges(), gc.collect(), chimera.closeSession() the memory is not freed. This is the code that reproduces the problem, it uses the molecules in the zipfile I have attatched. import chimera from Combine import combine import zipfile import time import gc import os #from memory_profiler import profile for itemnum in range(3): #@profile def pathmaker(itemnum): dirpath = os.path.dirname(os.path.realpath(__file__)) zippath=dirpath+'/'+'test1_000.zip' torsionpath=zipfile.ZipFile(zippath).extract('Pathway_000_Pathway.txt') proteinpath=zipfile.ZipFile(zippath).extract('Protein_000_Protein.mol2') ligandpath=zipfile.ZipFile(zippath).extract('Ligand_000_Ligand.mol2') opened = chimera.openModels.open(proteinpath) protein = opened[0] opened1 = chimera.openModels.open(ligandpath) mol=opened1[0] combination=combine([mol,protein],protein) totalnum=str(itemnum).zfill(3)+'_'+str(i).zfill(3) chimera.pdbWrite([combination], chimera.Xform(), 'testpdb%s.pdb' %totalnum) print('writing testpdb%s.pdb' %totalnum) chimera.openModels.close(combination) del combination chimera.update.checkForChanges() gc.collect() chimera.closeSession() for i in range(20): pathmaker(itemnum) #this stops the code and shows memory increase for each iteration print('pause 6 seconds...') time.sleep(6)

Hi Daniel, The ‘combine’ function doesn’t add the created Molecule to chimera.openModels. You would have to do that yourself with chimera.openModels.add(). Without having added ‘combination' to openModels, openModels.remove(combination) is a no-op, and therefore the C++ memory being used by the Molecule is not reclaimed. You either have to use openModels.add() or you have to explicitly reclaim the C++ memory with combination.destroy(). —Eric Eric Pettersen UCSF Computer Graphics Lab
On Oct 4, 2018, at 6:51 AM, Daniel VH <dviladrich95@gmail.com> wrote:
Hi, In my python script I'm using the combine() function to merge a protein and a molecule into one model and since I'm doing this multiple times and only use the combined result to save it with pdbWrite I want to free the memory of the object combination right after that, but after the instructions chimera.openModels.close(combination), del combination, chimera.update.checkForChanges(), gc.collect(), chimera.closeSession() the memory is not freed. This is the code that reproduces the problem, it uses the molecules in the zipfile I have attatched.
import chimera from Combine import combine import zipfile import time import gc import os #from memory_profiler import profile
for itemnum in range(3): #@profile def pathmaker(itemnum): dirpath = os.path.dirname(os.path.realpath(__file__)) zippath=dirpath+'/'+'test1_000.zip' torsionpath=zipfile.ZipFile(zippath).extract('Pathway_000_Pathway.txt') proteinpath=zipfile.ZipFile(zippath).extract('Protein_000_Protein.mol2') ligandpath=zipfile.ZipFile(zippath).extract('Ligand_000_Ligand.mol2')
opened = chimera.openModels.open(proteinpath) protein = opened[0] opened1 = chimera.openModels.open(ligandpath) mol=opened1[0] combination=combine([mol,protein],protein) totalnum=str(itemnum).zfill(3)+'_'+str(i).zfill(3) chimera.pdbWrite([combination], chimera.Xform(), 'testpdb%s.pdb' %totalnum) print('writing testpdb%s.pdb' %totalnum) chimera.openModels.close(combination) del combination chimera.update.checkForChanges() gc.collect() chimera.closeSession()
for i in range(20): pathmaker(itemnum) #this stops the code and shows memory increase for each iteration print('pause 6 seconds...') time.sleep(6) <testing.py><test1_000.zip>_______________________________________________ Chimera-dev mailing list Chimera-dev@cgl.ucsf.edu http://plato.cgl.ucsf.edu/mailman/listinfo/chimera-dev

Thanks a lot! On Fri, Oct 5, 2018 at 12:51 AM Eric Pettersen <pett@cgl.ucsf.edu> wrote:
Hi Daniel, The ‘combine’ function doesn’t add the created Molecule to chimera.openModels. You would have to do that yourself with chimera.openModels.add(). Without having added ‘combination' to openModels, openModels.remove(combination) is a no-op, and therefore the C++ memory being used by the Molecule is not reclaimed. You either have to use openModels.add() or you have to explicitly reclaim the C++ memory with combination.destroy().
—Eric
Eric Pettersen UCSF Computer Graphics Lab
On Oct 4, 2018, at 6:51 AM, Daniel VH <dviladrich95@gmail.com> wrote:
Hi, In my python script I'm using the combine() function to merge a protein and a molecule into one model and since I'm doing this multiple times and only use the combined result to save it with pdbWrite I want to free the memory of the object combination right after that, but after the instructions chimera.openModels.close(combination), del combination, chimera.update.checkForChanges(), gc.collect(), chimera.closeSession() the memory is not freed. This is the code that reproduces the problem, it uses the molecules in the zipfile I have attatched.
import chimera from Combine import combine import zipfile import time import gc import os #from memory_profiler import profile
for itemnum in range(3): #@profile def pathmaker(itemnum): dirpath = os.path.dirname(os.path.realpath(__file__)) zippath=dirpath+'/'+'test1_000.zip' torsionpath=zipfile.ZipFile(zippath).extract('Pathway_000_Pathway.txt') proteinpath=zipfile.ZipFile(zippath).extract('Protein_000_Protein.mol2') ligandpath=zipfile.ZipFile(zippath).extract('Ligand_000_Ligand.mol2')
opened = chimera.openModels.open(proteinpath) protein = opened[0] opened1 = chimera.openModels.open(ligandpath) mol=opened1[0] combination=combine([mol,protein],protein) totalnum=str(itemnum).zfill(3)+'_'+str(i).zfill(3) chimera.pdbWrite([combination], chimera.Xform(), 'testpdb%s.pdb' %totalnum) print('writing testpdb%s.pdb' %totalnum) chimera.openModels.close(combination) del combination chimera.update.checkForChanges() gc.collect() chimera.closeSession()
for i in range(20): pathmaker(itemnum) #this stops the code and shows memory increase for each iteration print('pause 6 seconds...') time.sleep(6)
<testing.py><test1_000.zip>_______________________________________________ Chimera-dev mailing list Chimera-dev@cgl.ucsf.edu http://plato.cgl.ucsf.edu/mailman/listinfo/chimera-dev
participants (2)
-
Daniel VH
-
Eric Pettersen