About

Independent game developer, founder of Awesome Games Studio. Former gameplay programmer at Tate Interactive studio. Born in 1987, living the indie dream since 2010.

Contact

E-mail:
madras/AT/madras/DOT/pl

Twitter: @MarcinDraszczuk

My Games

Yet Another Zombie Defense

Duel: The Art of Combat

Get Your Girlfriend Into Games

Links

www.bfilipek.com

Tag cloud

(all)

Archives

01 Dec - 31 Dec 2010
01 Jan - 31 Jan 2011
01 Mar - 31 Mar 2011
01 Jun - 30 Jun 2011
01 Nov - 30 Nov 2011
01 Dec - 31 Dec 2011
01 Jan - 31 Jan 2012
01 Feb - 29 Feb 2012
01 Mar - 31 Mar 2012
01 Apr - 30 Apr 2012
01 Jun - 30 Jun 2013
01 Sep - 30 Sep 2013
01 Nov - 30 Nov 2013
01 Dec - 31 Dec 2013
01 Feb - 28 Feb 2014

Search!

Last Comments

Stuff

Powered by Pivot - 1.40.7: 'Dreadwind' 
XML: RSS Feed 
XML: Atom Feed 

« Oozi progressing well… | Home | It's done! »

Oozi vs Garbage Collector

12 03 11 - 18:08 Yesterday I finally managed to go down with runtime garbage generation to zero bytes per second. You think it's easy task, just use pooling and avoid String concatenations? Well, that's what I thought some time ago. Unfortunately I was wrong. Here are some things that are good to know if you're going for garbage-free XNA-Xbox game.

1) StringBuilder isn't garbage free. Surprised? I was pretty sure that this class was designed to avoid garbage. Unfortunately, it works garbage-free only for String and char Append methods. If you want to append int, garbage will be generated. Fortunately I found a free implementation of garbage-free Append methods on Gavin Pugh's blog (thanks Gavin!).

2) XACT isn't garbage free. Furthermore, SoundEffect.Play() isn't garbage free as well. What can you do? The only thing you can do to play sounds and avoid garbage is to keep a pool of SoundEffectInstances. However, you can't keep more than 300 instances at once on Xbox, if you do it will crash. I included my code that manages a pool of SoundEffectInstances at the end of this post.

3) Foreach loop might generate garbage in some cases. Especially when you're iterating over a Collection, i.e. Dictionary.Keys collection. You can read more on foreach garbage here.

int soundEffectInstancesCount;
Dictionary< SoundEffect, List< SoundEffectInstance > > SingleInstancesCache
= new Dictionary< SoundEffect, List< soundeffectinstance > >();

public void playSingleSound( SoundEffect soundEffect, float volume )
{
List < SoundEffectInstance> instances;
if (!SingleInstancesCache.ContainsKey(soundEffect))
{
instances = new List(10);
SingleInstancesCache[soundEffect] = instances;
}
else
instances = SingleInstancesCache[soundEffect];

for (int iSoundInstance = 0; iSoundInstance < instances.Count; ++iSoundInstance)
{
SoundEffectInstance instance = instances[iSoundInstance];
if (instance.State == SoundState.Stopped)
{
instance.Play();
return;
}
}

SoundEffectInstance newInstance = soundEffect.CreateInstance();
SingleInstancesCache[soundEffect].Add(newInstance);
newInstance.Play();
soundEffectInstancesCount++;
if (soundEffectInstancesCount % 20 == 0)
System.Diagnostics.Debug.WriteLine("Sound effect instances count over "
+ soundEffectInstancesCount);
}


Trackback link: http://www.marcindraszczuk.pl/pivot/tb.php?tb_id=14