This project has moved. For the latest updates, please go here.

Performance - Lots of memory allocations

Nov 3, 2010 at 2:58 AM

This library is totally awesome :D

Though, when running on the Xbox I am noticing a lot of garbage adding up. Around 11KB / second in my particular case (450 objects, no boxing and no strings).

I did a quick search of the Farseer classes (version 79230) for the term "new " and got 580+ results. A lot of these are Vector2s which I am pretty sure are safe, but other ones like FixedArray are probably not. I am guessing these allocations are the source of all the garbage, especially the ones in the Collision class.

I am considering modifying the code to use pooling instead of all the new statements but that's a rather large and cumbersome undertaking so I am wondering if anyone has any known fixes or if these leaks are planned to be plugged in the future?

Nov 3, 2010 at 10:40 AM

The FixedArray is put in place to make reduce garbage collections by not allocating on the heap. They do not produce any garbage as far as I'm aware of.

I checked the garbage generation a few months back and we had 0 allocations in our performance benchmarks, however, the library have changed since then and there might be some garbage from the library itself. I will investigate the problem later today.

Nov 3, 2010 at 3:19 PM

Awesome, thanks Genbox

Nov 8, 2010 at 3:46 PM

I fired up CLRProfiler on FP3.0 (release version - not latest source). The only significant Farseer allocations I could see were emanating from DistanceProxy.Set(). In my simple prototype (only one dynamic body), it was creating around 30Kb of Vector2[] and Vertices in 20 seconds.

Not sure if this particular allocation has been fixed in the latest source, or if others have been introduced :)

Jan 4, 2011 at 9:01 PM

I'm seeing the same behaviour as Roonda, using CL82585. The DistanceProxy struct contains a member of type Vertices, which is a subclass of List<Vector2>. Calling DistanceProxy.Set() will allocate a new Vertices list, resulting in quite a lot of garbage.

I've only just started using Farseer, so I'm not sure of the best way to address this. As a test, I switched the Vertices member for a FixedArray of Vector2's. While this works in addressing the garbage issue, I assume it will fall apart for polygons with more vertices than the fixed array. I'm conveniently not using arbitrary shapes (just rectangles and circles), so I haven't encountered this yet.

Jan 6, 2011 at 10:55 PM

I'll take a look at it. Thanks for bringing it to my attention.