From: David Poger (d.poger_at_uq.edu.au)
Date: Tue Aug 05 2008 - 02:18:05 CDT

Thanks!!! It works really well now. No memory leaking anymore!

Cheers,
David

John Stone wrote:
> David,
> You're deleting them too late. The delete should be placed
> at the end of the same loop in which you created the selection,
> not in the outer scope that contains the loop.
> Loops with atom selections should always look something like:
>
> for ... {
> set mysel [atomselect ...]
> do stuff...
> $mysel delete
> }
>
> If you wait to delete until after the end of that loop,
> it's too late, and you've already "leaked" the memory as
> $mysel will get overwritten with a handle to a completely
> different selection the next time through the loop.
>
> You can see if you're leaking selections by doing "atomselect list"
> after your code runs. Try it on a small test case after you fix
> your code to verify you've cleaned things up.
>
> Cheers,
> John Stone
> vmd_at_ks.uiuc.edu
>
> On Tue, Aug 05, 2008 at 03:24:10PM +1000, David Poger wrote:
>
>> Thanks for the quick reply. Unfortunately, I still have a memory leak...
>> I added the commands "$jsel delete" and "unset jsel" after the "j" loop
>> and "$isel delete" and "unset isel" after the "i" loop though. Should I
>> delete the frames once the coordinates of each one has been read? If so,
>> how?
>>
>> Thanks for any help
>>
>> David
>>
>>
>> for {set i $Dom1_1 } { $i<=$Dom1_n } {incr i} {
>> # Number of atoms in the residues of Dom1 which is selected
>> set isel [atomselect top "resid $i"]
>>
>> for {set j $Dom2_1 } { $j<=$Dom2_n } {incr j} {
>> set distm 1000
>>
>> foreach coordi [$isel get {x y z}] {
>> # Number of atoms in the residues of Dom2 which is selected
>> set jsel [atomselect top "resid $j"]
>>
>> foreach coordj [$jsel get {x y z}] {
>> set dist [vecdist $coordi $coordj]
>> if {$dist<$distm} {set distm $dist}
>> }
>> }
>> # The array is in angstroem!!
>> set sumdistij($i,$j) [expr $sumdistij($i,$j)+$distm]
>> set sumdistij2($i,$j) [expr $sumdistij2($i,$j)+$distm*$distm]
>>
>> #Release memory!
>> $jsel delete
>> unset jsel
>> }
>> $isel delete
>> unset isel
>> }
>>
>>
>>
>>
>>
>> John Stone wrote:
>>
>>> Hi,
>>> You must delete the atom selections that you're creating within
>>> the loops in your script. Specifically, you must add
>>> "$isel delete" and "$jsel delete" (for example) to your nested
>>> loops. This is the cause of your memory consumption problem.
>>> This is a frequently asked question, it comes up almost once every
>>> week or two.
>>>
>>> Cheers,
>>> John Stone
>>> vmd_at_ks.uiuc.edu
>>>
>>> On Tue, Aug 05, 2008 at 11:31:10AM +1000, David Poger wrote:
>>>
>>>
>>>> Hello
>>>>
>>>> I have written a Tcl script that basically reads a trajectory and for
>>>> each frame, finds the shortest distance between every single residue of
>>>> a first domain and every single residue of another domain in a protein.
>>>> The final matrix with all the minimal distances is written in an output
>>>> file. The script works fine... for 2 frames. In fact, after a few
>>>> minutes (~10-20 frames), I have to kill it as it fills up the memory of
>>>> the PC (32GB of RAM!!!). Is there any way to optimize this script
>>>> (release memory for example?). I know I have several nested loops but I
>>>> don't how to do differentlty and I have absolutely no knowledge in code
>>>> optimization!
>>>>
>>>>
>>>> Thanks a lot!
>>>> David
>>>>
>>>>
>>>> # In order to run it start vmd without graphical interface by typing
>>>> # $> vmd -dispdev text -e script.tcl
>>>>
>>>> #======================================================#
>>>> # TO BE EDITED #
>>>>
>>>> set grofile protein.gro
>>>> set trajfile traj.xtc
>>>> set outputfile traj_contact.dat
>>>>
>>>> # #
>>>> #======================================================#
>>>>
>>>>
>>>> set datafile [open $outputfile w]
>>>> puts $datafile [format "%1s%7s%8s%9s%9s" "#" "Res1" "Res2" "<d>" "rmsd"]
>>>>
>>>>
>>>> mol new $grofile
>>>>
>>>> # Residues in domains 1D and 2D
>>>> set Dom1_1 20
>>>> set Dom1_n 155
>>>> set Dom2_1 126
>>>> set Dom2_n 244
>>>>
>>>> for {set i $Dom1_1 } { $i<=$Dom1_n } {incr i} {
>>>> for {set j $Dom2_1 } { $j<=$Dom2_n } {incr j} {
>>>> set sumdistij($i,$j) 0
>>>> set sumdistij2($i,$j) 0
>>>> }
>>>> }
>>>>
>>>> #animate style once
>>>> mol addfile $trajfile waitfor all
>>>> set nframes [molinfo top get numframes]
>>>> puts "nframes=$nframes"
>>>>
>>>> # The 1st frame is the $grofile used for the topology so I don't use it
>>>> # That's why k starts from 1 and not 0
>>>> for {set k 1} {$k<$nframes} {incr k} {
>>>> animate goto $k
>>>> display update
>>>> puts "frame #$k"
>>>>
>>>> for {set i $Dom1_1 } { $i<=$Dom1_n } {incr i} {
>>>> # Number of atoms in the residues of Dom1 which is selected
>>>> set isel [atomselect top "resid $i"]
>>>>
>>>> for {set j $Dom2_1 } { $j<=$Dom2_n } {incr j} {
>>>> set distm 1000
>>>>
>>>> foreach coordi [$isel get {x y z}] {
>>>> # Number of atoms in the residues of Dom2 which is selected
>>>> set jsel [atomselect top "resid $j"]
>>>>
>>>> foreach coordj [$jsel get {x y z}] {
>>>> set dist [vecdist $coordi $coordj]
>>>> if {$dist<$distm} {set distm $dist}
>>>> }
>>>> }
>>>> # The array is in angstroem!!
>>>> set sumdistij($i,$j) [expr $sumdistij($i,$j)+$distm]
>>>> set sumdistij2($i,$j) [expr $sumdistij2($i,$j)+$distm*$distm]
>>>>
>>>> }
>>>> }
>>>>
>>>> }
>>>>
>>>>
>>>> for {set i $Dom1_1 } { $i<=$Dom1_n } {incr i} {
>>>> for {set j $Dom2_1 } { $j<=$Dom2_n } {incr j} {
>>>> set mean [expr $sumdistij($i,$j)/($nframes-1)]
>>>> set mean2 [expr $sumdistij2($i,$j)/($nframes-1)]
>>>> set msd [expr $mean2-$mean*$mean]
>>>> set rmsd [expr sqrt($msd)]
>>>>
>>>> puts $datafile [format "%8d%8d%9.3f%9.3f" $i $j [expr $mean/10]
>>>> [expr $rmsd/10]]
>>>> }
>>>> }
>>>>
>>>> close $datafile
>>>>
>>>> puts "Finished"
>>>>
>>>> exit
>>>>
>>>> --
>>>>
>>>> _________________________________________________
>>>> David POGER, Ph.D.
>>>> Postdoctoral Research Fellow
>>>> Molecular Dynamics Group
>>>> School of Molecular and Microbial Sciences (SMMS)
>>>> Chemistry Building (#68)
>>>> The University of Queensland
>>>> Brisbane, QLD 4067
>>>> Australia
>>>>
>>>> Email: d.poger_at_uq.edu.au
>>>> Tel: +61 7 3365 7562
>>>> Fax: +61 7 3365 3872
>>>>
>>>>
>>>> ** Notice: Unless stated otherwise, this e-mail represents only the views
>>>> of the Sender and not the views of The University of Queensland.
>>>>
>>>>
>>>
>>>
>> --
>>
>> _________________________________________________
>> David POGER, Ph.D.
>> Postdoctoral Research Fellow
>> Molecular Dynamics Group
>> School of Molecular and Microbial Sciences (SMMS)
>> Chemistry Building (#68)
>> The University of Queensland
>> Brisbane, QLD 4067
>> Australia
>>
>> Email: d.poger_at_uq.edu.au
>> Tel: +61 7 3365 7562
>> Fax: +61 7 3365 3872
>>
>>
>> ** Notice: Unless stated otherwise, this e-mail represents only the views
>> of the Sender and not the views of The University of Queensland.
>>
>
>

-- 
_________________________________________________
David POGER, Ph.D.
Postdoctoral Research Fellow
Molecular Dynamics Group
School of Molecular and Microbial Sciences (SMMS)
Chemistry Building (#68)
The University of Queensland
Brisbane, QLD 4067
Australia
Email: d.poger_at_uq.edu.au
Tel: +61 7 3365 7562
Fax: +61 7 3365 3872
** Notice: Unless stated otherwise, this e-mail represents only the views of the Sender and not the views of The University of Queensland.