Huge performance issues after porting to android

Hi, my game was working perfectly on PC using very few resources but after starting an Android project and copying over everything to that, the game lags horribly.

I have a list of vector2s and every second or so it generates 100 vector2s and puts them into the list, every every time it hits 1200 vector2s, I get rid of 200 of them because I don’t need them anymore. And I have 2 instances of this class so 2 lists like that in total.

I’m thinking this is the problem but I’m not sure how to optimize this and I also wanted to know if there’s a tool that could allow me to determine what’s causing the lag? I tried android device monitor but wasn’t able to do much with it.

Other than the list thing, I do some bad things like using toString() on an int every update so I can DrawString() it and calling a small interpolation function inside a draw call. Not sure if those would really cause the game to drop to like 20 fps.

Are the lists cached? From the sound of it, it may be the garbage collector, but there are many other things that could be going on. Additionally, are you batching your sprites efficiently, presuming this is a 2D game?

I recommend running a profiler on it. If you’re using Visual Studio, go to Analyze > Performance Profiler and choose the option relevant (Ex. CPU Usage) then hit Start. That should give you more information about what’s the bottleneck in your code.

I don’t think the lists are cached and I’m drawing the whole scene on a rendertarget2d which I then draw in the main Game1 draw function.

I couldn’t get the performance profiler to work on android but on PC for a 1 minute session the draw call for the 2 lists took 3800 ms of the CPUs time. This seems to be the slowest part:

Which SpriteSortMode are you using? If it’s Immediate, then that means each draw call is separate, which is not optimal. What does isColored() do?

If the lists aren’t cached and new ones are created each frame, then that’s a lot of garbage being generated each frame, which will cause the GC to kick in more frequently.

The 2 spritebatch begins I have right now are BackToFront and this is isColored():

public bool isColored(float y) {
    for (int j = 0; j < coloredPoints.Count; j += 2) {
        int h = coloredPoints[j];
        int l = coloredPoints[j + 1];
        if (y >= h && y <= l) return true;
    }
    return false;
}

it just check if the number is within a range of numbers and coloredPoints is a tiny list usually 0 to 4 vectors in it but it also creates some garbage. I’m not sure how to fix the garbage problem with the big 1200 vector list, should I be reusing the vector objects created instead of creating new ones?

The vectors themselves aren’t the issue, but the lists. If you keep them around and clear them before you repopulate them, that’ll help a lot regarding garbage.

Based off the information given so far:

  • There are minimal differences in what you’re drawing if a point is colored vs not colored. Can you perhaps cache the information on whether a point is colored beforehand to avoid looking for it each frame? Or perhaps store more information about it in a custom struct instead of using a Vector2?
  • You mentioned there are 2 SpriteBatch Begin calls; are there separate ones for each instance of that class? If so, you should be able to consolidate them into one. I’d need more information to confirm this.

Hey I just wanted to say that I fixed this issue. The problem wasn’t so much the GC, it was just the emulator I was using and my computer. After switching to another computer and installing HAXM the game runs completely fine. Though I will still fix the amount of garbage it generates because its unnecessary.

1 Like