GSI Forum
GSI Helmholtzzentrum für Schwerionenforschung

Home » PANDA » PandaRoot » Bugs, Fixes, Releases » memory leaks
memory leaks [message #9355] Wed, 09 September 2009 14:22 Go to next message
Anonymous Poster From: *e18.physik.tu-muenchen.de
Hi everybody,

I found in a few tasks that people do a Clear() call at the beginning of their tasks' Exec() methods on their output TClonesArray. PLease be aware that this is a memory leak. You should replace the Clear() call with a Delete() call.

I didnt use the ticket system because this could concern many developers.

Cheers, Christian
Re: memory leaks [message #9356 is a reply to message #9355] Wed, 09 September 2009 14:54 Go to previous messageGo to next message
Ralf Kliemt is currently offline  Ralf Kliemt
Messages: 507
Registered: May 2007
Location: GSI, Darmstadt
first-grade participant

From: *54-82-r.retail.telecomitalia.it
Thanks Christian,

The correct usage of Clear() and Delete() of the TClonesArray class can be found in the root documentation: http://root.cern.ch/root/html524/TClonesArray.html

I found about 120 occurances on a quick check.

Kind Regards, Ralf.
Re: memory leaks [message #9357 is a reply to message #9356] Wed, 09 September 2009 15:01 Go to previous messageGo to next message
Anonymous Poster From: *e18.physik.tu-muenchen.de
Hi Ralf,

so, what do you think. Is Clear() in this context a memory leak? I wasnt sure immediately what the ROOT daco was telling me. This fix was something I remembered from a long time ago....

Cheers, Christian
Re: memory leaks [message #9360 is a reply to message #9357] Wed, 09 September 2009 16:02 Go to previous messageGo to next message
StefanoSpataro is currently offline  StefanoSpataro
Messages: 2736
Registered: June 2005
Location: Torino
first-grade participant

From: *ext.kfa-juelich.de
Here a summary cut&paste:

Clear():
Clear the clones array. Only use this routine when your objects don't allocate memory
since it will not call the object dtors.


Delete():
Clear the clones array. Use this routine when your objects allocate  memory (e.g. objects
inheriting from TNamed or containing TStrings  allocate memory).
If not you better use Clear() since if is faster.


As far as I have understod, the usage of clear or delete depends on the data members of the object itself

[Updated on: Wed, 09 September 2009 16:03]

Report message to a moderator

Re: memory leaks [message #9361 is a reply to message #9360] Wed, 09 September 2009 16:05 Go to previous messageGo to next message
Anonymous Poster From: *e18.physik.tu-muenchen.de
Hi,

all the classes concerned in PandaROOT allocat lots of memory, so we'd have to use Delete().

Cheers, Christian
Re: memory leaks [message #9364 is a reply to message #9361] Wed, 09 September 2009 17:09 Go to previous messageGo to next message
StefanoSpataro is currently offline  StefanoSpataro
Messages: 2736
Registered: June 2005
Location: Torino
first-grade participant

From: *ext.kfa-juelich.de
I am not so sure, i.e. FairHit contains only numbers and does not allocate memory to create objects.
In theory, if I have understood well, for this reason all the FairHit objects could be "cleared".

Another thing, for objects which have strings or objects, one should implement the correct "Clear" function.
I remember that using Delete instead of Clear the reconstruction becomes very slow. Hve you seen particular effects changing Clear to Delete?
Re: memory leaks [message #9392 is a reply to message #9364] Mon, 14 September 2009 14:07 Go to previous message
Elwin Dijck
Messages: 16
Registered: June 2009
Location: Groningen, The Netherland...
occasional visitor
From: *KVI.nl
Whether using Clear() instead of Delete() is a memory leak indeed depends on the type of class in the TClonesArray. I had some problems with TClonesArrays earlier (see this thread); this is what I think that the different calls do, please correct me if I'm wrong:

1. Clear()
Marks the memory occupied by the objects in the TClonesArray as available, without running destructors and without actually deallocating anything. The memory will be reused for new objects put into the array, which will overwrite the remains of the old objects.

2. Clear("C")
Same as previous, but will call the function Clear() on each of the objects in the array before marking the memory as available (note that TObject has an implementation of Clear() that does nothing, which should then be overloaded).

3. Delete()
Will run the destructor for each of the objects in the array, still without actually freeing the memory of the objects themselves and then does the same as 1. This happens in a kind of weird way and is supposed to be slow (probably only matters for large arrays though, not sure).
However, calling Delete() is needed for classes containing for instance TStrings as data members, to make sure the internal (dynamically allocated) storage of the strings is deallocated.

I think this would mean the following:

  • Because FairHit doesn't dynamically allocate anything, indeed just using Clear() will be ok.
  • Classes that do dynamically allocate objects should have a proper Clear() function to be able to use Clear("C") on the TClonesArray, otherwise Delete() is needed to prevent memory leaks.
  • For classes that contain (not dynamically allocated) strings or other objects like containers, that themselves dynamically allocate memory, Delete() is needed. I don't think it would be possible to prevent memory leaks by implementing some Clear() function in these cases (unless all these objects have a function that makes them deallocate their internal storage, but for TString and STL classes, only destructors do that I think, since it would typically bring the objects into an invalid state).

Using Delete() is the safest thing to do but also the slowest, so it might indeed be useful to check when it actually matters.

When just using TClonesArrays while reading from a TTree using GetEntry(), there is normally no need to do any Clear() or Delete() as ROOT will free memory automatically, though it might still be useful when there are transient data members involved.

Best regards,
Elwin Dijck

[Updated on: Wed, 16 September 2009 14:04]

Report message to a moderator

Previous Topic: Mac OS X 10.6 Snow Leopard
Next Topic: [Release] Analysis: Rho & PidCandidate
Goto Forum:
  


Current Time: Mon Nov 25 20:08:39 CET 2024

Total time taken to generate the page: 0.00633 seconds