Memory usage in G4HadronicInteraction::ApplyYourself()

Geant4 Version: 10.6.3
Operating System: ubuntu on Windows 10
Compiler/Version: gcc 9.4.0
CMake Version: 3.16.3


Hi all,

I’ve put together an app to estimate differential scattering cross sections by repeatedly calling G4HadronicInteraction::ApplyYourself(G4HadProjectile, G4Nucleus), and I’ve noticed that this function causes increased memory usage every time you call it. I’d first thought it was a memory leak caused by some object like the G4Nucleus that needed to be freed up after every call, but that’s not the case (as far as I can tell - I haven’t seen any likely attributes of the G4HadProjectile, G4HadronicInteraction, G4ParticleHPManager, or G4HadFinalState that would seem to need freeing up either).

My best guess now is that Geant’s step-tracking apparatus kicks in at ApplyYourself() or lower, and whatever system is responsible for eventually writing step information to ROOT files is already keeping track of the G4HadFinalState objects returned by ApplyYourself(). I don’t need to keep track of these, as I count the results into histograms myself in the program. Is there a way to clear that information or prevent it from being stored in the first place, so that I don’t see this expensive usage of memory while my program runs? Or, is there some other thing to blame for the memory used?

That function is pure virtual. Which hadronic process or processes (subclass of G4HadronicInteraction) are you calling? You’ll need to look at that specific process’ .cc file to see what ApplyYourself() is doing.

If you’re calling ApplyYourself() directly, then it might be the vector of secondaries. The hadronic process owns a G4HadFinalState data member, which is cleared and reused (specifically to avoid memory leakage) from event to event. Following the normal process/tracking flow, G4HadFinalState::Clear() should get called at the beginning of the process, but it’s not obvious if that happens when you call ApplyYourself() outside of tracking.

Sorry, G4ParticleHPElastic is the specific interaction, I should have said that instead of G4HadronicInteraction everywhere in my question.

G4HadFinalState::Clear() and G4HadFinalState::ClearSecondaries() don’t seem to work, at least not when calling them on the G4HadFinalState pointer returned by ApplyYourself(). I’m not sure if that pointer is the same as the object kept by the interaction, but I think it is. The one the interaction owns (as far as I can tell) lives in a G4Cache<G4HadFinalState*> owned by the G4ParticleHPElasticFS (subclass of G4ParticleHPFinalState) which is owned by the G4ParticleHPChannel which is owned by the G4ParticleHPElastic. The pointer is returned with the G4Cache::Get() method, which I would assume returns the same object in memory every time, so calling Clear and ClearSecondaries on the object returned by the function should be working

Actually Clear() gets called on the cached object every time, so that shouldn’t be the problem