Malloc failed error

Hello everyone,

I am trying to read a ROOT File, write the data in an array and then some calculation will done.I have around 221 million data number in this ROOT File. When I run my macro I have this error :

Internal Error: malloc failed


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


It looks like you are running on windows with an old ROOT version. Can you try to use AClic? (compiled version of your macro). For that do:

root [0] .x ReadFromNTTree.c++

Yeah, I am working on windows with root_v5.34.36 version. I tried it but I have same error.

We would need a macro reproducing the problem to help you further.

So what should I do?

Post here a macro reproducing your problem.

I can’t upload ROOT File because of its size.


#include <array>
#include <string>
#include <vector>
#include <math.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

#include "TApplication.h"
#include "TNtuple.h"
#include "TCanvas.h"
#include "TGraph.h"
#include "TH1D.h"
#include "TFile.h"
#include "TLeaf.h"


using namespace std;


	void ReadFromNTTree(){
	const int NRSamples = 221200000;
	const int NrNSamplesPerEvent = 34998;
	const int NREvents = NRSamples / NrNSamplesPerEvent;
	int Index = 0;
	const int Baseline_Sample = 50;
	int threshold = 2;
	int timestamp = 0;
	int Gate = 4500;
	int Pregate = 2;
	double t = 1;
	double T = 0.05;
	double Q1 = 0.0;
	double Q2 = 0.0;
	double Q = 0.0;

	long double FinalData[NREvents][NrNSamplesPerEvent];


	TFile* MyFile = new TFile("Badom.root");
	TTree* TREE = (TTree*)MyFile->Get("MY TREE");
	
	double Fdata = 0;

	TREE->SetBranchAddress("FDATA", &Fdata);

	TCanvas *Canvas = new TCanvas("Canvas", "Histogram");
	TH1D *EventHisto1 = new TH1D("EventHisto1", "CO60 Histogram", 1000.0, 0.0, 1000000000.0);

	
	cout << "Analyzing file ... " << endl << '\n';

	Long64_t NEntries = TREE->GetEntries();
	Long64_t EVENT_it = 0, SAMPLE_it = 0;
	cout << "NrEntries: " << NEntries << endl;
	for (Long64_t j = 0; j < NEntries; j++) {
		Index++;
		TREE->GetEntry(j);
		FinalData[EVENT_it][SAMPLE_it] = Fdata;
	
		SAMPLE_it += 1;
		//cout <<"Event is : "<<'\t'<< EVENT_it <<'\t'<<"sample is : "<<'\t'<< SAMPLE_it <<'\t'<<"data is:"<<'\t'<< Fdata << endl;
		if (Index == NrNSamplesPerEvent) {
			//cout << ++num <<'\t'<<index<< endl;
			Index = 0;
			EVENT_it += 1; SAMPLE_it = 0;
			cout << "****************** Event Nr is: " << EVENT_it << "*********************" << endl;

			for (int sam1 = 0; sam1 < NrNSamplesPerEvent; sam1++) {

				if (FinalData[EVENT_it-1][sam1] > threshold) {

				timestamp = sam1;

				break;
				}//if (FinalData[NREvents][NrNSamplesPerEvent][sam1]) > threshold)

			}//for (int sam1 = 0; sam1 < NrNSamplesPerEvent; sam1++)

			if (timestamp <= Pregate)
				Pregate = timestamp;

			for (int sam2 = timestamp - Pregate; sam2 < Gate; sam2++) {


				Q1 += (FinalData[EVENT_it-1][sam2]) * 2;
				Q2 += (FinalData[EVENT_it-1][sam2 + 1]) * 2;
				
			}//for(int sam8=0 ; sam8<NrSamples; sam8++)
				
			Q = (Q1 + Q2) / 2;
			//cout << Q << endl;
			EventHisto1->Fill(Q);
		   }//if (Index == NrNSamplesPerEvent)
	}//for (Long64_t i = 0; i<nentries; i++)
	

	Canvas->Divide();
	Canvas->cd(1);
	EventHisto1->Draw();
	Canvas->Update();
	
	}//void ReadFromTTree()

We cannot run the macro without it. Can you put it on some download site ?

Its size is about 1.43GB!

Ok … We cannot run your macro without it.

So I have no way to fix it :pensive:
Thank you :rose:

As I said you can put your file on some site (Dropbox etc …) and post the link to it here.

I’m uploading it in mediafire but it take long time to be upload. As it uploaded I’ll post the link. Thank you.

The error says that the (first, at least) problem is in the creation of the FinalData array, with size of 221,200,000 long doubles, which is too much memory. Is that huge array really necessary? Probably you could change your code so it is not needed at all; and if the array is unavoidable, maybe it can/should be double instead of long double (are the values in the tree really long double?).

Yes, This code should be able to analysis huge data. I should read data from ROOT File and then write it in an array for doing next calculations.Data size was the reason that lead me to use ROOT. Even if array be double ,the problem will not solve.

Finally I could upload the file! Use this link.

Your code does not run (cf @dastudillo post) the following lines make root exit (on Mac) without doing anything:

	const int NRSamples = 221200000;
	const int NrNSamplesPerEvent = 34998;
	const int NREvents = NRSamples / NrNSamplesPerEvent;

	long double FinalData[NREvents][NrNSamplesPerEvent]; 

The array FinalData is far too large to fit in memory.

Already I test it with double type but nothing happened.

try this:

void fateme(){
   const int NRSamples = 221200000;
   const int NrNSamplesPerEvent = 34998;
   const int NREvents = NRSamples / NrNSamplesPerEvent;

   double FinalData[NREvents][NrNSamplesPerEvent];

   printf("exit macro\n");
}

I get:

 % root
   ------------------------------------------------------------------
  | Welcome to ROOT 6.23/01                        https://root.cern |
  | (c) 1995-2020, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Sep 29 2020, 07:54:42                      |
  | From heads/master@v6-23-01-1364-g5837b8294c                      |
  | With Apple clang version 12.0.0 (clang-1200.0.32.2)              |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

root [0] .x fateme.C
%

So the macro does nothing and exit (it does not print the exit message) because the array FinalData is too big. You should change the logic of your macro to avoid having a such big array.

Even float makes a too big array.