Memory use keeps increasing while calling contactTest()

mcc
Posts: 4
Joined: Fri Mar 02, 2012 5:43 pm

Memory use keeps increasing while calling contactTest()

Post by mcc »

I made a small game, partially just as a way to learn working with Bullet so I can use it on a larger project. You can play the game here or find the source code here (although it may not be obvious how to build it at first). The game mostly does not use Bullet directly, instead I used a library called "Polycode" which among other things wraps Bullet. What I do is I have a btCollisionWorld which I have filled with blocks (btBoxShapes); one block is the "player character". On each game tick I move the player box around according to keypresses, and move the player box down a bit to simulate gravity. On each of these moves I query the collision space using contactTest to see if I have collided with something and need to back off. So with three tests per tick, and 60 fps, that is about 180 calls to contactTest per second.

What I find is that while I am running my program, my memory usage, judged from watching the OS X "activity monitor", increases by about two megabytes per second. Moreover the game stutters every second or so in a way I am interpreting as slowdown from the game having to get more memory from the OS. And when the btCollisionWorld is deleted (this is happening, unless something very unexpected is going on, every time you die; I delete the btCollisionWorld and then recreate it) the memory is not reclaimed, the two-megabytes-per-second increase continues instantly from the exact amount of memory use I was taking before deleting the last btCollisionWorld.

Based on my initial tests I believe this apparent memory leak is happening "inside" of contactTest. If I comment out the call to contactTest, the memory increase no longer happens. If I comment out nearly everything I am doing *other* than calling contactTest, the memory leak is not abated.

I found this thread on the forum which recommends various things for debugging memory leaks. As it recommended, I commented in the define for BT_DEBUG_MEMORY_ALLOCATIONS in LinearMath/btAlignedAllocator.h and then attempted (my build process is funny :/) to recompile. I was unable to use _CrtDumpMemoryLeaks(); this appears to be a Windows-only function and I do not have Windows. What I could do, however, is

Code: Select all

	extern int gNumAlignedAllocs, gNumAlignedFree, gTotalBytesAlignedAllocs;
	ERR("ALLOCS %d FREE %d TOTAL %d\n", gNumAlignedAllocs, gNumAlignedFree, gTotalBytesAlignedAllocs);
...to read some constants from the btAlignedAllocator header, once a second. I ran the program for a few seconds and saw

Code: Select all

ALLOCS 564 FREE 221 TOTAL 0
ALLOCS 744 FREE 401 TOTAL 0
ALLOCS 924 FREE 581 TOTAL 0
ALLOCS 1104 FREE 761 TOTAL 0
ALLOCS 1284 FREE 941 TOTAL 0
ALLOCS 1464 FREE 1121 TOTAL 0
ALLOCS 1644 FREE 1301 TOTAL 0
ALLOCS 1824 FREE 1481 TOTAL 0
ALLOCS 2004 FREE 1661 TOTAL 0
ALLOCS 2184 FREE 1841 TOTAL 0
ALLOCS 2364 FREE 2021 TOTAL 0
ALLOCS 2544 FREE 2201 TOTAL 0
I then died (ie deleted the collision world) and ran a few more seconds; I saw

Code: Select all

ALLOCS 3138 FREE 2452 TOTAL 0
ALLOCS 3318 FREE 2632 TOTAL 0
ALLOCS 3498 FREE 2812 TOTAL 0
ALLOCS 3678 FREE 2992 TOTAL 0
ALLOCS 3858 FREE 3172 TOTAL 0
ALLOCS 4038 FREE 3352 TOTAL 0
ALLOCS 4218 FREE 3532 TOTAL 0
ALLOCS 4398 FREE 3712 TOTAL 0
I do not know what it means that gTotalBytesAlignedAllocs is 0 (maybe I failed to in fact enable BT_DEBUG_MEMORY_ALLOCATIONS?) however, it looks like as I run, gNumAlignedAllocs is continually growing faster than gNumAlignedFree, which makes it look like bullet is allocating something like 100-200 objects per second which never get deleted.

Like I said I didn't write most of the Bullet interface code myself, instead getting it from the Polycode library, so I'm not 100% certain on how it works. And there might be any number of bugs in my code as well. However the bit of code which calls contactTest is isolated enough there doesn't seem to be space for me or Polycode to be introducing bugs, unless there is special magic to using contactTest I am doing wrong.

If it helps:
The (very simple) code from my game which calls contactTest is here

The (more complex) Polycode library code which sets up Bullet is here and here (headers here and here)

What should I do to debug from here?
mcc
Posts: 4
Joined: Fri Mar 02, 2012 5:43 pm

Re: Memory use keeps increasing while calling contactTest()

Post by mcc »

Oh-- this is Bullet 2.78.
mcc
Posts: 4
Joined: Fri Mar 02, 2012 5:43 pm

Re: Memory use keeps increasing while calling contactTest()

Post by mcc »

I upgraded to Bullet 2.79 to see what would happen-- it did not help with the memory problem.

Then I did some more testing and discovered my initial testing was somehow wrong... the problem is NOT in contactTest, if I comment out contactTest I still get the ballooning memory. I have not yet found the problem but it now looks unlikely to be related to Bullet. Sorry to bother about this.