Memory leak when reading TH2Poly from a tree

Dear expert,

I want to save the TH2Poly into a tree and read them in the other place.
The saving procedure works fun. But when I try to read the histograms, memory
occupy grows very fast.

Here are three simple scripts to show the problem:

testTH2Poly.C – Create a TH2Poly, fill it and then delete, no memory leak.

#include "TH2Poly.h"
#include "TRandom.h"
#include <iostream>

void testTH2Poly()
{
    for(int i = 0; i < 1000; ++i)
    {
        TH2Poly* th2 = new TH2Poly();
        th2->Honeycomb(0, 0, 1, 50, 50);
        for(int j = 0; j < 1000; ++j)
        {
            th2->Fill(gRandom->Gaus(0, 20), gRandom->Gaus(0, 20));
        }
        delete th2;
        std::cout << i << std::endl;
    }
}

genTH2Poly.C – Create a TH2Poly, fill it and then save into a tree, no memory leak.

#include "TH2Poly.h"
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include <iostream>
void genTH2Poly()
{
    TFile* fout = new TFile("TestTH2Poly.root","recreate");
    TTree* tree = new TTree("test","");
    TH2Poly* th2 = new TH2Poly();

    tree->Branch("TH2", &th2);

    for(int i = 0; i < 1000; ++i)
    {
        delete th2;
        th2 = new TH2Poly();
        th2->Honeycomb(0, 0, 1, 50, 50);
        for(int j = 0; j < 1000; ++j)
        {
            th2->Fill(gRandom->Gaus(20, 10), gRandom->Gaus(20, 10));
        }
        tree->Fill();
        std::cout << i << std::endl;
    }
    fout->cd();
    tree->AutoSave();
    fout->Close();
}

readTH2Poly.C – Loop all the TH2Poly from a tree created by genTH2Poly.C, and do nothing. Even I have enable SetAutoDelete(), the memory occupy still grows very fast. Loop a 130M file causes 1.4G memory usage.

#include "TH2Poly.h"
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
void readTH2Poly()
{
    TFile* fin = new TFile("TestTH2Poly.root");
    TTree* tree = (TTree*)fin->Get("test");

    TH2Poly* th2 = nullptr;
    TBranch* br_poly = nullptr;
    tree->SetBranchAddress("TH2", &th2, &br_poly);
    br_poly->SetAutoDelete(true);

    Long64_t numOfEntries = tree->GetEntries();

    for(Long64_t entry = 0; entry < numOfEntries; ++entry)
    {
        tree->GetEntry(entry);
        std::cout << entry << std::endl;
        br_poly->DropBaskets("all");
    }
}

Do you have any idea about that?

Best regards,
Ruiting

ROOT Version: v6.22/08
Platform: Linux
Compiler: cling/g++


Hello @Mary-Tim ,

valgrind --tool=massif should point exactly to the source of the memory allocations. Can you run readTH2Poly.C like valgrind --suppressions=$ROOTSYS/etc/valgrind-root.supp --tool=massif root.exe -l -b -q readTH2Poly.C+g and post the output file here?

Cheers,
Enrico

Hi @eguiraud ,

Thank you!
I tried to upload the log file but I got the message “Sorry, new users can’t put links in posts.”
So I past the plot from massif-visualizer at here:
Read a tree (28MB) which contents 200 TH2Polys causes 331MB memory usage.

Best regards,
Ruiting

Hi @eguiraud ,

@pcanal The problem is solved by enlarge the bufsize when create the branch of TH2Poly.
Thank you for your help!

Best regards,
Ruiting

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.