Game slow problem on Android

this needs to be set only once, when you create the class or when you initialize it, then you use it all the time, no need to do it again.

for example:

public class abc{
 List<sprite> entitiesUsed = new List<sprite>();
List<sprite> entitiesFree = new List<sprite>();
   public abc(){
        // here you add all the entitiedFree that you want in your game, all your enemies
      for ( int idx=0; idx< 50; idx++){
           entitiesFree.Add ( New sprite() );
      }
  }

  public void Update(GameTime gameTime) {
     // if you have your timer here , your timer will pick one item from the free list here and put it in the used list 

   // then you can do here also 
            foreach (var sprite in entitiesUsed )
            {
                sprite.Update(gameTime, sprites, gamePadState); 
            }
    }
//virtual draw
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            foreach (var sprite in entitiesUsed )
            {
                sprite.Draw(spriteBatch);
            }
        }
   
}
1 Like

Merci pour votre patience
je vais tester ça et reviens vers vous :slightly_smiling_face:

hi,
J’ai procédé comme suit , est-ce que je suis sur le bon chemin ou pas du tout ?

merci d’avance



Dans Game1:

public class Game1 : Game
    {
       private List<Sprite> sprites = new List<Sprite>(); // use 
        private List<Sprite> spritesFree = new List<Sprite>();  //free
    }

Dans LoadContent
//I put 1 because it displays it to me several times in superposition

protected  async override void  LoadContent()
{
	for (int idx = 0; idx < 1; idx++)
            {
               spritesFree.Add(new Player(texturePlayer));
            }
}

Dans Update :
protected override void Update(GameTime gameTime)
{
 if (spritesFree.Count > 0)
            {
                Sprite Sprite = spritesFree[0];
                spritesFree.RemoveAt(0);
                sprites.Add(Sprite);
            }
            //int elapsed; 
            for (int idx = sprites.Count - 1; idx >= 0; idx--)
            {
                Sprite bp = sprites[idx];
                bp.Update(gameTime, sprites, gamePadState);

                if (bp.IsRemoved)
                {
                    spritesFree.Add(bp);
                    sprites.RemoveAt(idx);
                }
            }

            //update game
            _virtualGamePad.Update(gameTime);
            foreach (var sprite in sprites)
            {
                sprite.Update(gameTime, sprites, gamePadState);
            }

}



looks good, did you try it with your game?

1 Like

hi,
yes i tried it
it’s true that it’s more fluid, works a lot better at the start and it takes longer before slowing down
maybe the leak is coming from somewhere else too?

thanks in advance

Check in you game if you have “= new” in some other places inside your main game loop, if you have that, then you are still creating new instances of those other objects and creating garbage again, do the same as Sprite for those cases and it will run smoother.

2 Likes

it works
thank you very much :smiley:
:+1:

Is it possible to run the Visual Studio or Android Studio profilers that will tell you what percent of the time each line of code is taking? I’ve found them very useful in the past for performance debugging. Otherwise you can try to write you own profiling code and measure how long different functions are taking.

1 Like

Thanks for the suggestion I will try that :grinning:

By the way I made a new video about object pooling and tested several methods at
Object Pooling

Please watch it and there is a link to a VS project in the video also, it has the classes and improvements I made to the pooling process so it will be a lot easier to manage and use it in your projects, it is free to download.

1 Like

Tnhak you :grinning: