Skip to content

Instantly share code, notes, and snippets.

@dpiparo
Last active August 9, 2025 15:53
Show Gist options
  • Select an option

  • Save dpiparo/b722347634681728de6f61fa9f63496e to your computer and use it in GitHub Desktop.

Select an option

Save dpiparo/b722347634681728de6f61fa9f63496e to your computer and use it in GitHub Desktop.
Invoke interpretation within a sub-process to avoid memory hoarding
auto memory()
{
ProcInfo_t info;
gSystem->GetProcInfo(&info);
auto mem = info.fMemResident / 1024.;
printf("Res memory = %g Mbytes\n", mem);
return mem;
}
void SubProcessLine(const char *code)
{
// Really suboptimal from the runtime point of view, but proves the point.
// Execution of a callable within a process can be optimised wrt to what follows...
ROOT::TProcessExecutor pe(1);
pe.Map([code]() { return gInterpreter->ProcessLine(code); }, 1);
}
void ProcessLine(const char *code)
{
gInterpreter->ProcessLine(code);
}
void pe()
{
gInterpreter->ProcessLine("TH1F h;");
sleep(1);
const auto initMem = memory();
auto deltaMemSubpr = 0.;
auto deltaMem = 0.;
auto nIters=512;
for (auto i = 0; i < nIters; ++i) {
SubProcessLine("h");
if (0 == (i + 1) % (16)) deltaMemSubpr = memory();
}
for (auto i = 0; i < nIters; ++i) {
ProcessLine("h"); // This hoards memory
if (0 == (i + 1) % 16) deltaMem = memory();
}
deltaMemSubpr -= initMem;
std::cout << "Delta memory subprocess = " << deltaMemSubpr << std::endl;
deltaMem -= initMem;
std::cout << "Delta memory classic = " << deltaMem << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment