I’ve been narrowing down this problem the whole day and found out when this happens.
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Texture2D tex = new Texture2D(GraphicsDevice, 2, 2);
}
And then, in Update()
int frameCount = 0;
protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
if(frameCount == 0)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
base.Update(gameTime);
frameCount++;
}
Then in frame 0, the android game freezes.
But if you call tex.Dispose() just after instantiating it, game doesn’t freeze.
One strange thing is that, in windows platform it’s OK not to call tex.dispose().
Can anyone explain what’s going on behind this strange behaviour, please?
I don’t do any android development (or ios) so this might not really apply to them, but in the .net / PC world you should never have the need to do GC.Collect() - you should leave it up to the .net framework to do the collecting when it’s needed.
Doing GC.Collect in your update could also make your game run slower as it would need to find which objects it needs to collect etc.
Disposing - it’s NOT ok to not call tex.dispose() - you should always clean up after yourself - especially if the life of tex is only for a short time in your application, If tex is there all through the life of the application - then you should still clean up and call dispose on the tex object in your Dispose() method.