Hello all, I need advice…
I try to make MT application with a sensitive detector that accumulate hits in collection and process them at the end of event action.
I created MyHit.hh (like in many G4 examples):
//---------------------------------------------------------------
#ifndef MyHit_h
#define MyHit_h 1
#include <G4VHit.hh>
#include <G4THitsCollection.hh>
#include <G4Allocator.hh>
#include <G4ThreeVector.hh>
class MyHit : public G4VHit
{
public:
MyHit() : G4VHit(), _EnDeposit(0), _Pos(0.,0.,0.), _PatEnergy(0), _CopyNo(0), _PatID(-1)
{
_VolumeName = "";
}
inline void* operator new(size_t);
inline void operator delete(void*);
private:
G4double _EnDeposit; // Energy deposit
G4double _PatEnergy; // Particle kinetic energy
G4int _PatID; // Particle PDG code
G4int _CopyNo; //Physical detector copy number
G4ThreeVector _Pos; // Position of the hit
G4String _VolumeName; //Physical volume name
public:
// Set functions to store information on hits
…
// Get functions to acess information on hits
…
};
typedef G4THitsCollection MyHitsCollection;
extern G4ThreadLocal G4Allocator* MyHitAllocator;
inline void* MyHit::operator new(size_t)
{
if (!MyHitAllocator) { MyHitAllocator = new G4Allocator; }
return (void*)MyHitAllocator->MallocSingle();
}
inline void MyHit::operator delete(void* aHit)
{
if (!MyHitAllocator) { MyHitAllocator = new G4Allocator; }
MyHitAllocator->FreeSingle((MyHit*)aHit);
}
#endif
//---------------------------------------------------------------
and simple MyHit.cc:
//---------------------------------------------------------------
#include “MyHit.hh”
#include <CLHEP/Units/SystemOfUnits.h>
#include <G4UnitsTable.hh>
#include <G4VVisManager.hh>
#include <G4VisAttributes.hh>
using namespace CLHEP;
G4ThreadLocal G4Allocator* MyHitAllocator = NULL;
//---------------------------------------------------------------
So, during the tests of my application on a cluster with 64 threads, the application 100% crashed due to end of available memory… It looks like the “ProcessHits” function of my sensitive detector just continuously allocate the memory but nobody freed it!!!
G4bool ScintillatorSD::ProcessHits(G4Step* theStep, G4TouchableHistory*)
{
…
//Add hit info to collection
MyHit* Hit = new MyHit;
Hit->SetEnergyDeposit(theStep->GetTotalEnergyDeposit());
Hit->SetPosition(thePrePoint->GetPosition());
Hit->SetVolumeName(thePrePoint->GetPhysicalVolume()->GetName());
EnHitsCollection->insert(Hit);
…
}
I have found the presentation “Improving memory handling in Geant4 MT”):
but it looks like nobody use something compatible to process hits and collections.
Does somebody have any ideas how to catch or fix this issue?