Re: How can I clean the memory used by deleted objects in a TFile/TDirectory?

From: Pierre-Luc Drouin <pldrouin_at_physics.carleton.ca>
Date: Wed, 18 Apr 2007 12:19:15 -0400


If for example I have the following code that creates a TDirectory with a TNamed object in that directory:
TDirectory *rootdir=gDirectory;
TDirectory *tdir=rootdir->mkdir("testdir"); TNamed *named=new TNamed("named","named"); tdir->Add(named);

and that I want to delete tdir and all the objects (and ideally all keys) contained in that directory (in this simple case only the single TNamed object), is there a simpler way to do it in ROOT than using the following lines that delete the objects one by one? tdir->RecursiveRemove(named);
delete named;
rootdir->RecursiveRemove(tdir);
delete tdir;

I thought originally that this is what the command "rootdir->RecursiveRemove(tdir);" would do for objects, but as you explained me it only removes all the references to a single object. So in other words what I am looking for is a TDirectory equivalent to the unix command "rm -rf tdir".

Thanks!

Pierre-Luc Drouin

Philippe Canal wrote:
>
>
>> TDirectory::RecursiveRemove(TObject *obj) will not remove obj from
>> memory but only from the TDirectory?
>>
>
> That is correct, RecursiveRevove removes the reference to the object
> from the TDirectory. Once the RecursiveRemove is done, you shoud call
> the delete operator to remove the object itself from memory.
>
> Cheers,
> Philippe
>
> -----Original Message-----
> From: Pierre-Luc Drouin [mailto:pldrouin_at_physics.carleton.ca]
> Sent: Monday, April 16, 2007 2:38 PM
> To: Philippe Canal
> Cc: roottalk_at_pcroot.cern.ch
> Subject: Re: [ROOT] How can I clean the memory used by deleted objects in a
> TFile/TDirectory?
>
> Hi,
>
> I try to delete all the TTree and TH objects from memory and their
> associated keys in the TFile using TDirectory::RecursiveRemove(TObject
> *) and TFile::Delete(char*) respectively. Do you mean that
> TDirectory::RecursiveRemove(TObject *obj) will not remove obj from
> memory but only from the TDirectory?
>
> Thanks
>
> Philippe Canal wrote:
>
>> Hi Pierre-Luc,
>>
>> The RAM should not grow unless you require it. Note that removing the
>> object from the file does __not__ remove it from memory. To help any
>> further I would need to get a bit more details on what you actually do.
>>
>> Cheers,
>> Philippe.
>>
>> PS. Also why is the code creating thousands of TTrees, this seems
>>
> unusual.
>
>> -----Original Message-----
>> From: owner-roottalk_at_pcroot.cern.ch [mailto:owner-roottalk_at_pcroot.cern.ch]
>> On Behalf Of Pierre-Luc Drouin
>> Sent: Friday, April 13, 2007 7:31 PM
>> To: Philippe Canal; roottalk_at_pcroot.cern.ch
>> Subject: Re: [ROOT] How can I clean the memory used by deleted objects in
>>
> a
>
>> TFile/TDirectory?
>>
>> I have noticed the same problem with RAM memory before I close the
>> TFile. Even if I delete these objects (TH, TTree), the memory used by
>> the process continues to increase. Is it normal too? The code creates
>> many thousands of TTrees and TH objects, so it can fill the whole RAM of
>> the machine quite easily...
>>
>> Thanks
>> Pierre-Luc Drouin
>>
>> Philippe Canal wrote:
>>
>>
>>> Hi Pierre-Luc,
>>>
>>> When you 'delete' an object from a ROOT file, it does not reduce the size
>>>
>>>
>> of
>>
>>
>>> the file. Instead, the add the newly 'freed' file area to a list of
>>>
>>>
>> 'empty
>>
>>
>>> space' in the file. This list is first search when writing another
>>>
> object
>
>>> to the file to see if one of the space will be able to hold it.
>>> 'Compacting' the file to remove to empty space would require to shift the
>>> bit around on the disk (aka an expansive operation). The easiest to
>>>
>>>
>> compact
>>
>>
>>> a ROOT file is simply to create a new file and copy the object to the new
>>> files. Since you have only histogram and tree, you can use hadd to do
>>>
>>>
>> this
>>
>>
>>> copy.
>>>
>>> Cheers,
>>> Philippe.
>>>
>>> -----Original Message-----
>>> From: owner-roottalk_at_pcroot.cern.ch
>>>
> [mailto:owner-roottalk_at_pcroot.cern.ch]
>
>>> On Behalf Of Pierre-Luc Drouin80
>>> Sent: Thursday, April 12, 2007 11:17 AM
>>> To: roottalk_at_pcroot.cern.ch
>>> Subject: [ROOT] How can I clean the memory used by deleted objects in a
>>> TFile/TDirectory?
>>>
>>> Hi,
>>>
>>> I have some code that creates and then deletes many objects and (TH,
>>> TTree, TDirectory) in a ROOT file and that saves this file with only a
>>> few remaining objects. When I reopen the ROOT file I see only the TKeys
>>> of the few objects that I have not deleted, but the size of the file is
>>> way too big for these objects (it looks like that some previously
>>> deleted objects are still using space) . I would like to know how could
>>> I clean efficiently the ROOT file. I am using ROOT 5.14/00c. I delete
>>> the objects in memory using TDirectory::RecursiveRemove(TObject*) and
>>> the TKeys using TDirectory::Delete(char *).
>>>
>>> Thanks
>>> Pierre-Luc Drouin
>>>
>>>
>>>
>>>
>>>
>>
>>
>
>
>
Received on Wed Apr 18 2007 - 18:19:36 CEST

This archive was generated by hypermail 2.2.0 : Wed Apr 18 2007 - 23:50:01 CEST