From: Jérôme Hénin (
Date: Thu Apr 25 2013 - 04:29:27 CDT

Alternately, if you use the atomselect object only once, you can take advantage of the Tcl garbage collector:

[atomselect top "some selection text"] somecommand

The script keeps no reference to the atomselect object, so it gets deleted automatically.

Another way this happens is when the variable referencing the object goes out of scope, e.g. if it is local to a procedure:

proc getnum { selText } {
set sel [atomselect top $selText]
return [$sel num]

When this proc finishes, its local variable sel goes out of scope, the reference count to the atomselect object goes to zero, and it gets deleted.

Now suppose I want a very stupid way to multiply the number of atoms in the system by ten thousand, I could do:

set num 0
for { set i 0 } { $i < 10000 } { incr i } {
incr num [getnum all]
puts $num

This doesn't leak any memory. It works because the variable sel is never *overwritten* - that's when memory leaks happen.

Cheers ,

----- Original Message -----

> Reassigning the variable doesn't delete the atomselect object. I have
> learned the hard way to always follow the pattern:

> set sel [atomselect top "some selection text"]
> ... do something with the $sel ...
> $sel delete

> especially when the selection comes in a loop.

> On Apr 24, 2013, at 4:51 PM, Lorenzo Gontrani wrote:

> Dear VMD users and developers, I need to calculate several rdfs for
> quite large trajectories, and, from some tests I ran, I could see
> that the GPU version of gofr script (rdf) is actually very fast, but
> I noticed that i cannot easily calculate all the rdf together (i. e.
> launching vmd -dispdev only once) because the memory usage grows
> very rapidly with multiple selections. I read in the old posts that
> it should be possible to delete the selection with $sel delete (at
> the moment, my script overwrites sel1 and sel2, but a different
> "atomselect??" is echoed on the screen, so I guess that it is a new
> selection and the variable is not overwritten). Does anybody have
> experience about that?

> Thanks a lot for any help
> Lorenzo

> --
> ==========================================
> Lorenzo Gontrani
> Research associate of CNR-ISM (Rome Tor Vergata)
> EDXD group of University of Rome "La Sapienza"

> GSM +39 338 7615798
> Email lorenzo DOT gontrani AT gmail DOT com
> Webpage: http://webcaminiti/gontrani.html
> =========================================
> Rispetta l'ambiente: se non è necessario, non stampare
> questa e-mail
> Protect the environment: do not print this e-mail, unless
> necessary