Std::bad_alloc

Hi,

I must do something wrong since I am booking in total about 3000 hists but root crashes (5.28.00). If I reduce it to about 1000 hists everything runs smooth. I am booking the hists in a seperate function as you can see below. I was just wondering if thats the problem because I create a lot of new hists? The filling is done in the main loop and safed afterwards.

thanks for your help

Here my setup:

const int EF_num_trig = 11;
const int L1_num_trig = 5;
const int ref = 15;
void triggerEff::BookHistos(){
    int et_bins = 600;
    double et_min = 0;
    double et_max = 600;
    double eta_bins = 800;
    double eta_min = -4;
    double eta_max = 4;
    int emfrac_bins = 140;
    double emfrac_min = 0;
    double emfrac_max = 1.4;
    int WIDTH_bins = 800;
    double WIDTH_min = 0;
    double WIDTH_max = 8; 
    // Define histograms  
    m_h_RunNumber 	                    	= new TH1F("RunNumber", "RunNumber", 250000, 0., 250000.);
    m_h_RunNumber	    	                ->Sumw2();
    m_h_vxp_n                               = new TH1F("vxp_n", "vxp_n", 600, 0., 600.);
    m_h_vxp_n                               ->Sumw2();
    m_h_event_counter                       = new TH1I("event_counter", "event_counter", 2, 0., 2.);
    m_h_event_counter	    	            ->Sumw2();
    m_h_jet_quality                         = new TH1F("jet_quality", "jet_quality", 4, 0., 4.);
    m_h_jet_quality                         ->Sumw2();
    m_h_jet_leading_noTrig					= new TH1F("jet_leading_noTrig", "jet_leading_noTrig", et_bins, et_min, et_max);
    m_h_jet_leading_noTrig					->Sumw2();
    TString label;
    TString L1[L1_num_trig];
    L1[4] = "_L1_j10";
    L1[3] = "_L1_j15";
    L1[2] = "_L1_j30";
    L1[1] = "_L1_j55";
    L1[0] = "_L1_j75";
    TString EF[EF_num_trig];
    EF[10] = "_EF_j10";
    EF[9] = "_EF_j15";
    EF[8] = "_EF_j20";
    EF[7] = "_EF_j30";
    EF[6] = "_EF_j40";
    EF[5] = "_EF_j55";
    EF[4] = "_EF_j75";
    EF[3] = "_EF_j100";
    EF[2] = "_EF_j135";
    EF[1] = "_EF_j180";
    EF[0] = "_EF_j240";
    TString refer[ref];
    refer[0] = "EF_j10_ref";
    refer[1] = "EF_j15_ref";
    refer[2] = "EF_j20_ref";
    refer[3] = "EF_j30_ref";
    refer[4] = "EF_j40_ref";
    refer[5] = "EF_j55_ref";
    refer[6] = "EF_j75_ref";
    refer[7] = "EF_j100_ref";
    refer[8] = "EF_j135_ref";
    refer[9] = "EF_j180_ref";
    refer[10] = "EF_j240_ref";
    refer[11] = "EF_j10j15j20_ref";
    refer[12] = "EF_j10_passThrough";
    refer[13] = "EF_fj10_passThrough";
    refer[14] = "EF_j10fj10_passThrough";
    for(int r = 0; r < ref; ++r){
        myfile->cd();
        myfile->mkdir(refer[r]);
        myfile->cd(refer[r]);
        label = "leading_dR";
        m_h_jet_leading_dR[r]                      = new TH1F(label, label, et_bins, et_min, et_max);
        m_h_jet_leading_dR[r]                      ->Sumw2();
        label = "jet_Et";
        m_h_jet_Et[r]                              = new TH1F(label, label, et_bins, et_min, et_max);
        m_h_jet_Et[r]                              ->Sumw2();
        label = "jet_Et_wo";
        m_h_jet_Et_wo[r]                           = new TH1F(label, label, et_bins, et_min, et_max);
        m_h_jet_Et_wo[r]                           ->Sumw2();
        label = "jet_eta";
        m_h_jet_eta[r]                             = new TH1F(label, label, eta_bins, eta_min, eta_max);
        m_h_jet_eta[r]                             ->Sumw2();
        label = "jet_phi";
        m_h_jet_phi[r]                             = new TH1F(label, label, 400, -4, 4);
        m_h_jet_phi[r]                             ->Sumw2();
        label = "leading_jet_Et";
        m_h_leading_jet_Et[r]                      = new TH1F(label, label, et_bins, et_min, et_max);
        m_h_leading_jet_Et[r]                      ->Sumw2();
        label = "leading_jet_eta_Et";
        m_h_leading_jet_eta_Et[r]                  = new TH2F(label, label, et_bins, et_min, et_max, eta_bins, eta_min, eta_max);
        m_h_leading_jet_eta_Et[r]                  ->Sumw2();
        label = "leading_jet_emfrac_Et";
        m_h_leading_jet_emfrac_Et[r]               = new TH2F(label, label, et_bins, et_min, et_max, emfrac_bins, emfrac_min, emfrac_max);
        m_h_leading_jet_emfrac_Et[r]               ->Sumw2();
        label = "leading_jet_WIDTH_Et";
        m_h_leading_jet_WIDTH_Et[r]                = new TH2F(label, label, et_bins, et_min, et_max, WIDTH_bins, WIDTH_min, WIDTH_max);
        m_h_leading_jet_WIDTH_Et[r]                ->Sumw2();
        for(int k = 0; k < L1_num_trig; ++k){
            label = "jet_Et";
            label += L1[k];
            m_h_L1_Et[k][r]                               = new TH1F(label, label, et_bins, et_min, et_max);
            m_h_L1_Et[k][r]                               ->Sumw2();
            label = "leading_jet_Et";
            label += L1[k];
            m_h_leading_jet_L1_Et[k][r]                   = new TH1F(label, label, et_bins, et_min, et_max);
            m_h_leading_jet_L1_Et[k][r]                   ->Sumw2();
            label = "leading_jet_eta_Et";
            label += L1[k];
            m_h_leading_jet_L1_eta_Et[k][r]               = new TH2F(label, label, et_bins, et_min, et_max, eta_bins, eta_min, eta_max);
            m_h_leading_jet_L1_eta_Et[k][r]               ->Sumw2();
            label = "leading_jet_emfrac_Et";
            label += L1[k];
            m_h_leading_jet_L1_emfrac_Et[k][r]            = new TH2F(label, label, et_bins, et_min, et_max, emfrac_bins, emfrac_min, emfrac_max);
            m_h_leading_jet_L1_emfrac_Et[k][r]            ->Sumw2();
            label = "leading_jet_WIDTH_Et";
            label += L1[k];
            m_h_leading_jet_L1_WIDTH_Et[k][r]             = new TH2F(label, label, et_bins, et_min, et_max, WIDTH_bins, WIDTH_min, WIDTH_max);
            m_h_leading_jet_L1_WIDTH_Et[k][r]             ->Sumw2();
            label = "matching_jet_Et";
            label += L1[k];
            m_h_matching_jet_L1_Et[k][r]                  = new TH1F(label, label, et_bins, et_min, et_max);
            m_h_matching_jet_L1_Et[k][r]                  ->Sumw2();
            label = "matching_jet_eta_Et";
            label += L1[k];
            m_h_matching_jet_L1_eta_Et[k][r]              = new TH2F(label, label, et_bins, et_min, et_max, eta_bins, eta_min, eta_max);
            m_h_matching_jet_L1_eta_Et[k][r]              ->Sumw2();
            label = "matching_jet_emfrac_Et";
            label += L1[k];
            m_h_matching_jet_L1_emfrac_Et[k][r]           = new TH2F(label, label, et_bins, et_min, et_max, emfrac_bins, emfrac_min, emfrac_max);
            m_h_matching_jet_L1_emfrac_Et[k][r]           ->Sumw2();
            label = "matching_jet_WIDTH_Et";
            label += L1[k];
            m_h_matching_jet_L1_WIDTH_Et[k][r]            = new TH2F(label, label, et_bins, et_min, et_max, WIDTH_bins, WIDTH_min, WIDTH_max);
            m_h_matching_jet_L1_WIDTH_Et[k][r]            ->Sumw2();
        }
        for(int k = 0; k < EF_num_trig; ++k){
            label = "jet_Et";
            label += EF[k];
            m_h_EF_Et[k][r]                               = new TH1F(label, label, et_bins, et_min, et_max);
            m_h_EF_Et[k][r]                               ->Sumw2();
            label = "leading_jet_Et";
            label += EF[k];
            m_h_leading_jet_EF_Et[k][r]                   = new TH1F(label, label, et_bins, et_min, et_max);
            m_h_leading_jet_EF_Et[k][r]                   ->Sumw2();
            label = "leading_jet_eta_Et";
            label += EF[k];
            m_h_leading_jet_EF_eta_Et[k][r]               = new TH2F(label, label, et_bins, et_min, et_max, eta_bins, eta_min, eta_max);
            m_h_leading_jet_EF_eta_Et[k][r]               ->Sumw2();
            label = "leading_jet_emfrac_Et";
            label += EF[k];
            m_h_leading_jet_EF_emfrac_Et[k][r]            = new TH2F(label, label, et_bins, et_min, et_max, emfrac_bins, emfrac_min, emfrac_max);
            m_h_leading_jet_EF_emfrac_Et[k][r]            ->Sumw2();
            label = "leading_jet_WIDTH_Et";
            label += EF[k];
            m_h_leading_jet_EF_WIDTH_Et[k][r]             = new TH2F(label, label, et_bins, et_min, et_max, WIDTH_bins, WIDTH_min, WIDTH_max);
            m_h_leading_jet_EF_WIDTH_Et[k][r]             ->Sumw2();
            label = "matching_jet_Et";
            label += EF[k];
            m_h_matching_jet_EF_Et[k][r]                  = new TH1F(label, label, et_bins, et_min, et_max);
            m_h_matching_jet_EF_Et[k][r]                  ->Sumw2();
            label = "matching_jet_eta_Et";
            label += EF[k];
            m_h_matching_jet_EF_eta_Et[k][r]              = new TH2F(label, label, et_bins, et_min, et_max, eta_bins, eta_min, eta_max);
            m_h_matching_jet_EF_eta_Et[k][r]              ->Sumw2();
            label = "matching_jet_emfrac_Et";
            label += EF[k];
            m_h_matching_jet_EF_emfrac_Et[k][r]           = new TH2F(label, label, et_bins, et_min, et_max, emfrac_bins, emfrac_min, emfrac_max);
            m_h_matching_jet_EF_emfrac_Et[k][r]           ->Sumw2();
            label = "matching_jet_WIDTH_Et";
            label += EF[k];
            m_h_matching_jet_EF_WIDTH_Et[k][r]            = new TH2F(label, label, et_bins, et_min, et_max, WIDTH_bins, WIDTH_min, WIDTH_max);
            m_h_matching_jet_EF_WIDTH_Et[k][r]            ->Sumw2();
        }
    }
}//end triggerEff::Bookhistos()

You seem to create quite many 2D histograms so maybe the total amount of required RAM exceeds the maximum allowed by your system for a user’s process (which is maximally about 3GB on 32bit systems) or even the total amount of the available RAM plus “swap space”.
Try “ulimit -a”, then try to run “top” in one window (observe the “VIRT” / “RES” / “SHR” / “%MEM” fields) and your root script in another one.

Hi,

yes I was thinking in the same direction. It crashes though very quick that I cant really see anything with “top”. Is there another way to book these hists? Or is this just the maximum root can handle?

thanks Michael

Well, add “sleep(2);” somewhere in the “most outer” loop of the histogram booking code and run “top -d 1”.

OK,

so here the output:

VIRT ~ 4000m
Res ~ 4g
SHR ~ 12m
%MEM ~ 50

any ideas?

thanks Michael

Check the output of “uname -a” and make sure that you are running a 64bit kernel.
Check the output of “ulimit -a” and make sure that the relevant values are high enough, e.g. “virtual memory”, “data seg size”, “max memory size” (either “unlimited” or more than 4GB, as your program tries to use that amount and dies).
Finally, run your program under a debugger and, as soon as it dies, ask “where”.

Hey,

well the values are all fine (unlimeted and 64) and the gdb just tells me that its in the bookhistos loop where it crashes so exactly what was expected. I just know files with far more histograms. Is this really all I can book?

thanks michael

Well, you could also try something like (“YourExecutable” or “$ROOTSYS/bin/root.exe”):

OK,

so this is the output any idea of what I can do?

3182 new/new[] failed and should throw an exception, but Valgrind
==3182== at 0x4C89B25: VALGRIND_PRINTF_BACKTRACE (valgrind.h:3720)
==3182== by 0x4C89D58: operator new[](unsigned int) (vg_replace_malloc.c:258)
==3182== by 0x6EAD509: TArrayF::Set(int) (in /afs/cern.ch/atlas/software/releases/17.0.6/sw/lcg/app/releases/ROOT/5.28.00h/i686-slc5-gcc43-opt/root/lib/libCore.so)
==3182== by 0x8EB3266: TH2F::TH2F(char const*, char const*, int, double, double, int, double, double) (in /afs/cern.ch/atlas/software/releases/17.0.6/sw/lcg/app/releases/ROOT/5.28.00h/i686-slc5-gcc43-opt/root/lib/libHist.so)
==3182== by 0x80509C1: triggerEff::BookHistos() (in /afs/cern.ch/user/m/mstoebe/testarea/analysis/Analyse.exe)
==3182== by 0x80514A8: triggerEff::Loop(bool, bool) (in /afs/cern.ch/user/m/mstoebe/testarea/analysis/Analyse.exe)
==3182== by 0x804D735: main (in /afs/cern.ch/user/m/mstoebe/testarea/analysis/Analyse.exe)
3182 cannot throw exceptions and so is aborting instead. Sorry.
==3182== at 0x4C89B25: VALGRIND_PRINTF_BACKTRACE (valgrind.h:3720)
==3182== by 0x4C89D66: operator new[](unsigned int) (vg_replace_malloc.c:258)
==3182== by 0x6EAD509: TArrayF::Set(int) (in /afs/cern.ch/atlas/software/releases/17.0.6/sw/lcg/app/releases/ROOT/5.28.00h/i686-slc5-gcc43-opt/root/lib/libCore.so)
==3182== by 0x8EB3266: TH2F::TH2F(char const*, char const*, int, double, double, int, double, double) (in /afs/cern.ch/atlas/software/releases/17.0.6/sw/lcg/app/releases/ROOT/5.28.00h/i686-slc5-gcc43-opt/root/lib/libHist.so)
==3182== by 0x80509C1: triggerEff::BookHistos() (in /afs/cern.ch/user/m/mstoebe/testarea/analysis/Analyse.exe)
==3182== by 0x80514A8: triggerEff::Loop(bool, bool) (in /afs/cern.ch/user/m/mstoebe/testarea/analysis/Analyse.exe)
==3182== by 0x804D735: main (in /afs/cern.ch/user/m/mstoebe/testarea/analysis/Analyse.exe)
==3182==
==3182== HEAP SUMMARY:
==3182== in use at exit: 3,017,967,541 bytes in 54,230 blocks
==3182== total heap usage: 80,671 allocs, 26,440 frees, 3,020,229,135 bytes allocated
==3182==
==3182== Searching for pointers to 54,230 not-freed blocks
==3182== Checked 3,024,190,196 bytes
==3182==
==3182==
==3182== Valgrind’s memory management: out of memory:
==3182== newSuperblock’s request for 4194304 bytes failed.
==3182== 4168294400 bytes have already been allocated.
==3182== Valgrind cannot continue. Sorry.

–Michael

“i686-slc5-gcc43-opt” is a 32bit version not a 64bit one (i.e. try to use “x86_64-slc5-gcc43-opt” and don’t forget to recompile all your source code from scratch).

Thanks that was the problem. I thought if I use a 64bit version to run it that should be fine but I didnt know that I had to compile everything. Good call

–Michael