RAM Memory usage rises exponentially to freeze computer when in multithreaded mode

I’m running a simple Geant4 simulation to look at gamma rays from
targets illuminated by neutrons. The geometry is simple with a
monoenergetic general particle source, a block target of material a
distance away embedded in an air half space and a detector. Data is
collected when a gamma ray intersects the detector volume. Within
SteppingAction, I open a ROOT file and store various properties of the
gamma in it and then close the file. I have been using it in single
thread mode for a long time and it generates good results. I recently
recompiled Geant4 in multithreading mode and I decided to modify the
program to run in multithreaded mode. I had to modify the file
open/write/close by opening a separate file for each thread and adding a
mutex lock for the file open/write/close part. The rest of the changes
were straightforward (e.g., add an ActionInitialization module). Now
when I run it in multithreaded mode, my memory usage starts to rise
exponentially and freezes my computer if I do not terminate it. This
happens for a single user thread or multiple user threads. If I comment
out the line in main(0)
G4MTRunManager * runManager = new G4MTRunManager;
and replace it with
G4RunManager * runManager = new G4RunManager;
the memory problem does not happen.

I have already gone through my code ensuring that "new"s and "delete"s
are paired and I have run the program valgrind (which helps find memory
leaks) but it did not provide any useful information (to me at least). I
have run another program (not mine) which has this memory problem but I
have also run Geant4 code in multithreaded mode without this problem
occurring. I am running Geant4 10.5 patch 01 on an i7 PC with 16GB RAM
and 64bit Linux (Fedora 28).

I have not posted the code since there is a lot of it and I am not
asking anyone to debug my code. But if there are
obvious parts that should be posted, I will do so. However, I was
wondering if there are any obvious known areas where these kinds of
memory leaks/runaways occur in migrating code to multithreaded mode.

Any help is gratefully appreciated. Thanks.

Hi,
To create ROOT output files, are you using ROOT or using Geant4 analysis tool? ROOT itself is not thread-safe.
Makoto

Hi Makato,
Thank you for your reply. I am using ROOT to populate TFiles - a separate one for each worker thread. I have mutex locks around those open file, fill and close file operations. Would that still be non thread-safe?
John

Hi John,
I’m not an expert of ROOT but I’m afraid so. Could you try our built-in analysis tool that is proven to be thread-safe and generates ROOT output file?
Makoto

Hi, Makato,
I have not used the gtools analysis routines before, so it may take a while, but I will give them a try. Thank you,
John

Hi Makato,
I used the gtools through the G4AnalysisManager to produce ROOT ntuples and histograms. I did it by modifying example B4a geometry (B4DetectorConstruction) and data collection modules (B4aSteppingAction.cc, B4aEventAction.cc, B4RunAction.cc). There is now no runaway memory and the program runs in significantly less time.

Thanks again,
John

Hi John,
Thanks for the update.
Makoto