I've been writing a game in Python that requires being able to add and remove a lot of objects from the physics engine. In extreme testing scenarios I can run out of memory is seconds. In normal cases it will take a while to accumulate. After two days of work I found a small test scenario that displays exactly whats happening. My Platform is Windows 7 64x.
import pymunk
import time
from os import system
space=pymunk.Space()
width=5
height=5
poly=[(-width/2.0,-height/2.0),(-width/2.0,height/2.0),(width/2.0,height/2.0),(width/2.0,-height/2.0)]
while(True):
#time.sleep(.5) #
system("pause")
bodys=[pymunk.Body(mass=5,moment=pymunk.moment_for_poly(5, poly)) for a in range(200)] #num of objects here
print "1"
shapes=[pymunk.Poly(bod, poly) for bod in bodys]
print "2"
space.add(bodys)
print "3"
space.add(shapes)
print "4"
space.step(.5) #step has no effect on leak
print "5"
space.remove(space.bodies)
print "6"
space.remove(space.shapes)
print "7"
#space.remove(shapes)
#print "6"
#space.remove(bodys)
#print "7"
So if you play around just a little. There was a time this didn't hang and would climb outta memory. But currently it hangs at removing shapes. Neither shape remove will do anything. My game never hangs just gobbles memory.
At a test of 200 per loop every few seconds it will "jump" 50 mbs starting at 50mb and ended(for me) around 300mb. I thought I might just be out running the garbage collector but large numbers of objects in or out just hemorrhage memory even if just once. I Let the program run without adding or removing objects. No memory is reclaimed.
Adding and removing just one object every loop had NO leak and ran on very low and constant memory.
I find a situation where pymunk doesn't leak but applying that method fixes nothing. (slower adding and removing or not using add(list) and remove(list).) Pymunk all by itself one object won't leak. One object at a time in my game is.
I have tried my darnedest to find any stray references or reference cycles. This example shows without much if any complex referencing and has a memory reclaiming issue.
Any input would be appreciated?
Additional INFO: adding and removing 1 object per frame at 60 fps climbs memory at around 3 mbs a second. My engine was in a separate thread till I figured out that wasn't the problem. Running synchronously still leaked.