Hi everyone,
My game is… finished BUT my tester found that after a couple of minute (like 10 minutes) the game starts to slow down and then crash. I discover by looking at the memory usage in windows that the game goes linearly from 200 meg of usage to 1 GB.
So I put in comments a lot of thing and discover that it’s entirely related to my weapon system. Each time the player or the enemies shoot a lazer (or any other weapon), the memory increase of approximatly 1 meg. If I do nothing, memory is stable.
I check if the class were deleted (i put a “print” inside of del) and everything seems fine. I also remove each node… So I don’t understand where all this memory is lost. Or maybe the garbage collector do not work well??? I’m new to Python so maybe I’m missing something. Please help because this project is due really soon and I was to write a report on Panda3D telling that everything was perfect but with this memory problem, I don’t know what I will do!
So here is the code of the weapon itself:
from pandac.PandaModules import Vec3, CollisionSphere, CollisionNode
from direct.interval.IntervalGlobal import *
import sys
import types
class WeaponDarkLazer:
def __init__(self, world, basePos, baseHpr):
self.world = world
self.myID = world.getNextID()
self.pos = basePos
self.baseHpr = baseHpr
self.hitDammage = 1
self.speed = 60
self.autodestruction = 3
self.totalElapsed = 0
self.isDead = 0
self.loadModels()
self.loadSound()
self.playSound()
self.print_top_100()
def doKill(self, id):
if (id == self.myID):
self.isDead = 1
return 1
else:
return 0
def getPos(self):
self.returnValue2 = Vec3(self.leftBeam.getX(), self.leftBeam.getY() + (self.speed*self.totalElapsed), self.leftBeam.getZ())
return self.returnValue2
def getHpr(self):
return self.root_lazer.getHpr()
def remove(self):
self.root_lazer.removeNode()
def loadSound(self):
self.lazersound = loader.loadSfx("sound/lazer.wav")
self.lazersound.setVolume(0.6)
self.lazersound.setLoop(False)
def playSound(self):
self.lazersound.play()
def loadModels(self):
self.root_lazer = self.world.attachNode()
self.root_lazer.setPos(0, 0, 0)
self.root_lazer.setHpr(self.baseHpr)
#Load the beams
self.leftBeam = loader.loadModel("models/darklazer/cylinder.egg")
self.leftBeam.reparentTo(self.root_lazer)
self.leftBeam.setColor(0, 1, 0, 1)
self.leftBeam.setHpr(0, 90, 0)
self.leftBeam.setScale(0.04, 0.04, 0.5)
self.leftBeam.setPos(self.pos)
self.leftBeam.setX(self.leftBeam.getX() - 0.5)
self.leftBeam.show()
self.rightBeam = loader.loadModel("models/darklazer/cylinder.egg")
self.rightBeam.reparentTo(self.root_lazer)
self.rightBeam.setColor(0, 1, 0, 1)
self.rightBeam.setHpr(0, 90, 0)
self.rightBeam.setScale(0.04, 0.04, 0.5)
self.rightBeam.setPos(self.pos)
self.rightBeam.setX(self.rightBeam.getX() + 0.5)
self.rightBeam.show()
#Load la bulle de collision
self.cslazer1 = CollisionSphere(self.pos.getX() - 0.5, self.pos.getY() + 0.5, self.pos.getZ(), 0.2)
self.cslazer2 = CollisionSphere(self.pos.getX() + 0.5, self.pos.getY() + 0.5, self.pos.getZ(), 0.2)
self.cnodePathlazer = self.root_lazer.attachNewNode(CollisionNode('cnode'))
self.cnodePathlazer.node().addSolid(self.cslazer1)
self.cnodePathlazer.node().addSolid(self.cslazer2)
self.cnodePathlazer.setTag('collisionType', 'weapon')
self.cnodePathlazer.setTag('damage', str(self.hitDammage))
self.cnodePathlazer.setTag('ID', str(self.myID))
#self.cnodePathlazer.show()
self.world.addToCollisionSystem(self.cnodePathlazer)
def move(self, elapsed):
self.totalElapsed += elapsed
if (self.totalElapsed < self.autodestruction):
self.root_lazer.setFluidPos(self.root_lazer,Vec3(0,self.speed*elapsed,0))
else:
self.isDead = 1
def isWeaponDead(self):
return self.isDead
And here is how I move the weapon at each refresh:
if (len(self.enemyProjectile) > 0):
for weapon in self.enemyProjectile:
if (weapon.getType() == "phantom"):
weapon.move(elapsed, self.fighter.getFighter())
else:
weapon.move(elapsed)
for deadID in self.enemyDeadWeaponList:
if (weapon.doKill(deadID) == 1):
self.world.fighterReceiveExplosion()
for deadID in self.enemyDeadWeaponSilentList:
weapon.doKill(deadID)
if (weapon.isWeaponDead() == 1):
self.enemyProjectile.remove(weapon)
weapon.remove()
del self.enemyDeadWeaponList[:]
del self.enemyDeadWeaponSilentList[:]
Please help me!!!
Jaff
P.S: If you have technique to hunt memory leak, please explain them well! Thanks!!!