Re: dynamical versus non dynamical for TH1F

From: John Idarraga <idarraga_at_cern.ch>
Date: Sat, 23 Jan 2010 22:08:17 -0500


Hello Marc,

Memory consumption would be basically the same in both cases, but they are allocated in different places in memory. When you use the 'new' operator memory for objects is allocated in the 'heap', which is believed (by your program) to be a infinite pool of memory. When you call the constructor of a class as TH1F myhisto(...), objects are allocated in the 'stack', and that space has a limitation per frame(function, member) set by the linux kernel. Now, when you run with CINT (interpreted) I don't know how does this exactly works. But the following has the same behaviour in both compiled and interpreted code. When using 'new', you are responsible of taking care of the memory allocated, you should call 'delete' yourself. In the other case objects 'die' when reaching the end of the scope and destructors are called automatically.

Now, your 'new' allocated objects remain alive after the end of the scope where they have been created, which is very often a desired effect, then you take care later (you must think of a way to do so .. otherwise this leads to memory leakage and that kind of very common problems ... probably not a reason to freak out in a simple script producing a few plots ... but still). There are plenty of other reasons for using 'new', you probably already have a bunch of ideas. Now, about speed, operations in the stack could be a bit faster (according to the assembly code I have seen ... I am not an expert, I might be mistaken), but it doesn't count when you use an object like TH1F, because when calling members things behind happen the same way in both cases. And what's happening inside the members is what should be taking the most time. So I think you can consider CPU time to be the same. There are some limit cases where it does make a difference ... I think it doesn't really matter in the scope of this discussion. Again I am not an expert ... may be someone has something more interesting to say about that.

Now, there is a lot to read about this. Check this for example ... there is plenty of documentation on this subject.

http://ee.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.8.html

Usually documentation about debugging with gdb start with a discussion on how programs deal with memory. Knowing those details helps a lot while programming.

Summary. Use 'new' allocated objects if you need your objects to make it out of the scope where they have been created. That is the case when you make an histogram and then you want to manipulate things in the TCanvas where it has been plotted. Sometimes you absolutely need objects to remain in memory like in the case where you read from a file opened with the TFile class. Then think about taking care of your memory allocated later on.

Let me know if this helps.

cheers,

John

On Sat, 2010-01-23 at 17:48 +0100, Marc Escalier wrote:
> Hello,
>
> i'm using a basic root program (.C) that allows to make some
> histograms of variables of a given ntuple (a .root file)
>
> Up to now, i created the histogram dynamically with
> TH1F *myhisto=new TH1F(...)
>
> i was wondering what it the more optimal (in terms of memory, cpu usage,
> or any other criterious you know better than me) way to do ? :
>
> *this kind of dynamical allocation
>
> or
>
> *a non dynamical allocation
> (i mean :
> TH1F myhisto(...)
> )
>
> (just in case : i do always the same plots and there variables
> are always in the ntuple)
>
> thanks !
>
> (i'll reply in private to answers to prevent spamming you all)
>
Received on Sun Jan 24 2010 - 04:08:30 CET

This archive was generated by hypermail 2.2.0 : Sun Jan 24 2010 - 05:50:02 CET