memory allocation in windows phone 7

Post and discuss features you'd like to see in the BEPUphysics library.
Post Reply
jbosch
Posts: 43
Joined: Sat May 29, 2010 4:42 pm
Contact:

memory allocation in windows phone 7

Post by jbosch »

Hi,

I've been profiling my game based on BepuPhysics for windows phone, basing my tests in this article: http://player.microsoftpdc.com/Session/ ... 25791980b1

I've discovered that bepu generates a LOT (25%) of memory rellocation, and because of this the calls to the garbage collector generate serious performance issues. I would recommend the creators of the engine to watch the video above.

One important thing I've discovered profiling is that the use of the System.Collection.Generic.List<> needs to be avoided in XNA game, and this is more necessary in WP7, because the garbage collector is called when all the garbage has a size of just 1MB.
ss
The profiler doesn't show me the Bepu code, but it tells me that some Lists in this library create up to 20.000 instances in a gameplay that takes few seconds (robot strike bowling game). There are many authors saying that the List need to be replaced by typed arrays. All foreach statements also need to be replaced by for statements. It also better to have an array with 1000 elements, use and reuse its items as many times as possible because extending its capacity, that requires problematic rellocations.

So I would recomend to do some optimizations changes to the library if you want it to work in WP7.
Jesús Bosch
XNA BLOG: http://geeks.ms/blogs/jbosch
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: memory allocation in windows phone 7

Post by Norbo »

There is a known bug in v0.14.2 that is present on Windows, but not the Xbox360 or WP7. A HashSet testing class got reintroduced to the codebase accidentally on Windows in v0.14.2 which has a bad enumerator, causing garbage. The PC can handle it easily, however, and the Xbox360/WP7 use a different code path entirely.

The list allocations you see should be some enumerator as opposed to actual list instances. If you are seeing actual list instances being created in large quantities after initialization, please send a repro case as this implies a bug.

I am aware of the memory allocation behavior of lists and foreaches. However, it is possible to use lists and foreach loops without creating garbage so long as certain precautions are taken. The engine handles them safely and should run flat with regard to allocations.

There are a few cases where the engine will create garbage, however; when a simulation is 'ramping up' to a new, much larger level of activity (typically at initialization), some internal resource pools may need to be resized. This is basically a one-time cost though and should not cause issues.
jbosch
Posts: 43
Joined: Sat May 29, 2010 4:42 pm
Contact:

Re: memory allocation in windows phone 7

Post by jbosch »

am aware of the memory allocation behavior of lists and foreaches. However, it is possible to use lists and foreach loops without creating garbage so long as certain precautions are taken. The engine handles them safely and should run flat with regard to allocations.
Hi,

Wich are those mechanisms?

By the way, I got a new WP7, so I can do some tests with the real device if you want.
Jesús Bosch
XNA BLOG: http://geeks.ms/blogs/jbosch
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: memory allocation in windows phone 7

Post by Norbo »

Here's two pieces on the foreach issue:
http://blogs.msdn.com/b/etayrien/archiv ... filer.aspx
http://blogs.msdn.com/b/etayrien/archiv ... art-2.aspx

Technically they're too old to address WP7 directly, but there shouldn't be any reason why the WP7 would work substantially different from the Xbox360 or the PC with regards to foreaches.

The rest of the solution uses pooling to re-use instances and sufficient initial capacity on involved lists to prevent frequent resizing.

The worst offender in BEPUphysics with regard to garbage is probably the default broadphase. I've never seen it cause an actual performance problem due to garbage, but it generally does a few extra resizes than are strictly necessary. To cause those superfluous resizes frequently, the simulation has to change state extremely rapidly, so it almost never shows up. That broadphase is going to get rewritten in v0.16.0 as well.
By the way, I got a new WP7, so I can do some tests with the real device if you want.
The one I'm most interested in is the problem you reported earlier about behavior on the device being different than the emulator. More than one person has seen that sort of thing, and I'm at a loss of how to fix it without knowing why it happens (or if it's even in my control to fix). Any information you find in your attempts to fix the issue would help me too.
http://www.bepu-games.com/forums/viewto ... f=4&t=1001
Post Reply