1

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.

Kaliber64
  • 586
  • 1
  • 5
  • 16
  • I cant reproduce this problem. I took your code and ran it just fine for a long time on my computer, with the memory at a steady 55.2mb. Maybe there is some version or OS thing? I tried on OSX 10.8.4 with Python 2.7 64bit, using latest pymunk trunk. Please add what you are running to your question. – viblo Aug 03 '13 at 11:58
  • @viblo Windows 7 64x. I'v tried many variations. I found a small 1 add 1 remove scenario that never leaked. but I do anything else with it and it leaks. I have 3 projects build around pymunk and they all have the same problem. I never noticed because operating at normal it would take a long time to add and remove that many objects. Often the garbage collector will have a huge list of bodys and polys that were uncollectable if that means anything. – Kaliber64 Aug 03 '13 at 15:50
  • I tried on my laptop with Windows 7 64x and Python 2.7 (32bit) and still could not reproduce it, memory consumption steady at around 50 mb. What python and pymunk version do you use? – viblo Aug 04 '13 at 08:45
  • However, something strange happened when I increased the number of objects to 500, then it looks like I get the problem on OSX (but on windows it still worked fine without increasing mem usage) – viblo Aug 04 '13 at 09:42
  • More strange things: The memory rise, but only the first 3 or so iterations of the while loop. Then it settles down on a steady 300mb in this case. It takes a little bit of time to do this which is why I didnt notice it at first. – viblo Aug 04 '13 at 10:05
  • Exactly around 200-500 it rises just a few times. easily 2000 items will go up to 1.8 gbs in a few seconds and python will crash with a MemoryError. I do suspect this isn't a problem on all platforms. Or maybe it is and didn't throw enough objects at it. If the memory usage didn't compound the objects themselves aren't that memory hungry. – Kaliber64 Aug 04 '13 at 21:29
  • I ran with memory_profiler and it seems like all allocations happens in the Chipmunk step function. I wonder if it might preallocate some collision structure or similar.. – viblo Aug 05 '13 at 03:29
  • Should I go over to Chipmunks forum? Or should I just bear with it XD. I've started on other things because I've spent like a week getting no where on this problem. Ignoring the leak. I tried copying existing bodys/shapes. removing them. Delete the old space and make a new one then add them back in. It causes a "all ready added twice?" message but if I don't their not there. The new space does not fix the memory issue. I think since I didn't remove them before deleting the space the added FLAG was set to True. – Kaliber64 Aug 05 '13 at 09:52
  • I forgot to remove them before deleting the space causing the message. I ran out of Edit time XD. – Kaliber64 Aug 05 '13 at 09:58
  • Yes, it would probably be a good idea to ask in the Chipmunk forum. – viblo Aug 05 '13 at 10:42

0 Answers0