GSI Forum
GSI Helmholtzzentrum für Schwerionenforschung

Home » PANDA » PandaRoot » Bugs, Fixes, Releases » [FIXED] Memory leak with TClonesArray and std::vector
[FIXED] Memory leak with TClonesArray and std::vector [message #15320] Fri, 23 August 2013 09:36 Go to next message
Klaus Götzen is currently offline  Klaus Götzen
Messages: 293
Registered: June 2006
Location: GSI
first-grade participant
From: *gsi.de
Hi,


this is more a ROOT related question, but maybe somebody from this club has an idea or knows about it.

The problem is that I observed quite a severe memory leak (just looking with 'top' how memory consumption of the job develops), apparently because the RhoCandidate stores the daughter pointers in a std::vector<RhoCandidate*>.

I isolated the problem in a short ROOT macro looking like:

#include "TClonesArray.h"
#include <vector>
#include <iostream>

// very simple class to be used with a TClonesArray
class MyClass: public TObject 
{
public:
    MyClass() {};
    ~MyClass() {};
    std::vector<int> fVec;
    ClassDef(MyClass,1)
};

void testtca(int num=100000)
{	
    TClonesArray *tca = new TClonesArray("MyClass",1100);
	
    for (int i=0;i<num;++i) 
    {
        if ((i%100)==0) cout <<i<<endl;
	tca->Clear();
		
	for (int  j=0;j<1000;++j) 
        {
	    MyClass *c= new ((*tca)[j]) MyClass();
	    c->fVec.push_back(42); // <-  THIS IS THE BAD GUY!!1
	    c->fVec.clear();
	}
    }	
}


When you run this with 'root -l -b -q testtca.C+' you will see the memory growing very!! fast (be careful, you might want to reduce num).

When you comment out the line with 'push_back(42)' it works perfectly well like you would expect it from a TClonesArray. The interesting part is, that even the 'fVec.clear()' directly after the push_back does not help at all to avoid the problem (most likely because clear() does not really release the allocated memory of the vector). And I think even an appropriate cleanup in the destructor wouldn't really help, since the objects in a TCA are not deleted anyway, right?

I didn't find any way to fix this leak up to now. Does somebody here has an idea how to avoid that problem? Or a solution for a dynamic container for pointers different from std::vector?


Best,
Klaus

[Updated on: Fri, 23 August 2013 17:18] by Moderator

Report message to a moderator

Re: Memory leak with TClonesArray and std::vector [message #15321 is a reply to message #15320] Fri, 23 August 2013 10:38 Go to previous messageGo to next message
Florian Uhlig is currently offline  Florian Uhlig
Messages: 424
Registered: May 2007
first-grade participant
From: *gsi.de
Hi Klaus,

you have to do a tca->Delete(); instead of a tca->Clear();

Clear will only release the memory used by the TClonesArray. Since the used vector is stored outside of the memory region used by the TClonesArray you delete only a pointer and you have a memory leak.

Delete will call the destructor of the object and such clear also memory used by the vector.

Ciao

Florian
Re: Memory leak with TClonesArray and std::vector [message #15328 is a reply to message #15321] Fri, 23 August 2013 12:54 Go to previous message
Klaus Götzen is currently offline  Klaus Götzen
Messages: 293
Registered: June 2006
Location: GSI
first-grade participant
From: *gsi.de
Hi Florian,


thanks for the solution!

I thought I tested already with Delete() without success, but now it worked.

Maybe I have to think about a fixed array for the pointers to benefit from the TClonesArray speedup.


Best,
Klaus
Previous Topic: [FIXED] genfit was moved
Next Topic: [FIXED] Error from PndMCTrackAssociator
Goto Forum:
  


Current Time: Tue Oct 08 21:07:46 CEST 2024

Total time taken to generate the page: 0.00782 seconds