Game fps severely drops

Hi everyone! I had a question about fps in my game.

So, when I would play about a minute of it, the fps would start dropping immensely going from 60 to around 30. I changed the game to 30 fps wondering what would happen, but now it drops to 15. 

If anybody has any tips as to how I should go about fixing this, that would be great. Please ask for any more information if need be.

Thank you.

PS I am using PonyWolf’s Visual Monitor to see my FPS, Texture and System Memory, and # of Stage Objects on screen. So I could provide these values if necessary.

sounds like you’re creating too many objects w/ or w/o bodies.

can you reduce the number of objects in your game/level as a test to see what happens?

Yes, provide the values you mentioned

Simulator (Win or OS X) or device (which specific device)

Are you deleting any objects that are no longer needed when they’re off screen?

Or - for some reason - are you running two instances of your scene at the same time?  It would explain why the frame rate halves.

OS X Simulator

Also, here are the logs containing all of the above values:

Jan 21 01:11:08.299 FPS: 60 Texture Memory: 4.56mb System Memory: 1.89mb Stage Objects: 167 Jan 21 01:11:08.795 FPS: 60 Texture Memory: 4.68mb System Memory: 2.25mb Stage Objects: 239 Jan 21 01:11:09.290 FPS: 60 Texture Memory: 4.7mb System Memory: 1.59mb Stage Objects: 287 Jan 21 01:11:09.786 FPS: 60 Texture Memory: 4.7mb System Memory: 1.99mb Stage Objects: 287 Jan 21 01:11:10.282 FPS: 60 Texture Memory: 4.7mb System Memory: 2.38mb Stage Objects: 287 Jan 21 01:11:10.778 FPS: 60 Texture Memory: 4.7mb System Memory: 2.75mb Stage Objects: 287 Jan 21 01:11:11.274 FPS: 60 Texture Memory: 4.7mb System Memory: 3.09mb Stage Objects: 287 Jan 21 01:11:11.771 FPS: 60 Texture Memory: 4.7mb System Memory: 1.75mb Stage Objects: 287 Jan 21 01:11:12.267 FPS: 60 Texture Memory: 4.7mb System Memory: 2.16mb Stage Objects: 287 Jan 21 01:11:12.763 FPS: 60 Texture Memory: 4.7mb System Memory: 2.5mb Stage Objects: 287 Jan 21 01:11:13.260 FPS: 57 Texture Memory: 4.7mb System Memory: 2.91mb Stage Objects: 287 Jan 21 01:11:13.755 FPS: 60 Texture Memory: 4.7mb System Memory: 1.44mb Stage Objects: 285 Jan 21 01:11:14.250 FPS: 60 Texture Memory: 4.7mb System Memory: 1.83mb Stage Objects: 287 Jan 21 01:11:14.747 FPS: 60 Texture Memory: 4.7mb System Memory: 2.21mb Stage Objects: 287 Jan 21 01:11:15.243 FPS: 60 Texture Memory: 4.7mb System Memory: 2.49mb Stage Objects: 285 Jan 21 01:11:15.738 FPS: 60 Texture Memory: 4.7mb System Memory: 3.02mb Stage Objects: 287 Jan 21 01:11:16.235 FPS: 60 Texture Memory: 4.7mb System Memory: 1.58mb Stage Objects: 287 Jan 21 01:11:16.731 FPS: 58 Texture Memory: 4.7mb System Memory: 1.94mb Stage Objects: 287 Jan 21 01:11:17.226 FPS: 60 Texture Memory: 4.7mb System Memory: 2.22mb Stage Objects: 287 Jan 21 01:11:17.723 FPS: 58 Texture Memory: 4.7mb System Memory: 2.59mb Stage Objects: 287 Jan 21 01:11:18.219 FPS: 59 Texture Memory: 4.7mb System Memory: 3.03mb Stage Objects: 287 Jan 21 01:11:18.714 FPS: 60 Texture Memory: 4.7mb System Memory: 1.58mb Stage Objects: 287 Jan 21 01:11:19.210 FPS: 60 Texture Memory: 4.7mb System Memory: 1.95mb Stage Objects: 289 Jan 21 01:11:19.707 FPS: 59 Texture Memory: 4.7mb System Memory: 2.22mb Stage Objects: 287 Jan 21 01:11:20.203 FPS: 60 Texture Memory: 4.7mb System Memory: 2.73mb Stage Objects: 289 Jan 21 01:11:20.699 FPS: 59 Texture Memory: 4.7mb System Memory: 3.04mb Stage Objects: 287 Jan 21 01:11:21.195 FPS: 60 Texture Memory: 4.7mb System Memory: 1.59mb Stage Objects: 287 Jan 21 01:11:21.690 FPS: 60 Texture Memory: 4.7mb System Memory: 2.06mb Stage Objects: 287 Jan 21 01:11:22.186 FPS: 60 Texture Memory: 4.7mb System Memory: 2.42mb Stage Objects: 287 Jan 21 01:11:22.684 FPS: 59 Texture Memory: 4.7mb System Memory: 2.79mb Stage Objects: 285 Jan 21 01:11:23.179 FPS: 60 Texture Memory: 4.7mb System Memory: 1.47mb Stage Objects: 287 Jan 21 01:11:23.674 FPS: 60 Texture Memory: 4.7mb System Memory: 1.9mb Stage Objects: 289 Jan 21 01:11:24.171 FPS: 59 Texture Memory: 4.68mb System Memory: 2.26mb Stage Objects: 289 Jan 21 01:11:24.667 FPS: 60 Texture Memory: 4.68mb System Memory: 2.58mb Stage Objects: 287 Jan 21 01:11:25.163 FPS: 60 Texture Memory: 4.68mb System Memory: 2.83mb Stage Objects: 287 Jan 21 01:11:25.659 FPS: 59 Texture Memory: 4.68mb System Memory: 3.16mb Stage Objects: 287 Jan 21 01:11:26.155 FPS: 60 Texture Memory: 4.68mb System Memory: 1.83mb Stage Objects: 287 Jan 21 01:11:26.651 FPS: 60 Texture Memory: 4.68mb System Memory: 2.17mb Stage Objects: 287 Jan 21 01:11:27.148 FPS: 60 Texture Memory: 4.68mb System Memory: 2.56mb Stage Objects: 287 Jan 21 01:11:27.643 FPS: 60 Texture Memory: 4.68mb System Memory: 3.03mb Stage Objects: 289 Jan 21 01:11:28.138 FPS: 60 Texture Memory: 4.68mb System Memory: 2.02mb Stage Objects: 275 Jan 21 01:11:28.636 FPS: 60 Texture Memory: 4.68mb System Memory: 2.67mb Stage Objects: 287 Jan 21 01:11:29.132 FPS: 60 Texture Memory: 4.68mb System Memory: 1.89mb Stage Objects: 287 Jan 21 01:11:29.628 FPS: 60 Texture Memory: 4.68mb System Memory: 2.74mb Stage Objects: 287 Jan 21 01:11:30.125 FPS: 60 Texture Memory: 4.7mb System Memory: 3.73mb Stage Objects: 283 Jan 21 01:11:30.623 FPS: 59 Texture Memory: 4.7mb System Memory: 2.8mb Stage Objects: 271 Jan 21 01:11:31.119 FPS: 60 Texture Memory: 4.7mb System Memory: 3.87mb Stage Objects: 283 Jan 21 01:11:31.614 FPS: 60 Texture Memory: 4.7mb System Memory: 2.9mb Stage Objects: 289 Jan 21 01:11:32.110 FPS: 60 Texture Memory: 4.7mb System Memory: 4.55mb Stage Objects: 289 Jan 21 01:11:32.608 FPS: 57 Texture Memory: 4.7mb System Memory: 3.52mb Stage Objects: 287 Jan 21 01:11:33.104 FPS: 60 Texture Memory: 4.7mb System Memory: 5.22mb Stage Objects: 287 Jan 21 01:11:33.599 FPS: 60 Texture Memory: 4.7mb System Memory: 4.11mb Stage Objects: 287 Jan 21 01:11:34.094 FPS: 60 Texture Memory: 4.7mb System Memory: 2.85mb Stage Objects: 287 Jan 21 01:11:34.591 FPS: 60 Texture Memory: 4.7mb System Memory: 4.88mb Stage Objects: 287 Jan 21 01:11:35.089 FPS: 58 Texture Memory: 4.7mb System Memory: 3.59mb Stage Objects: 287 Jan 21 01:11:35.585 FPS: 60 Texture Memory: 4.7mb System Memory: 5.85mb Stage Objects: 287 Jan 21 01:11:36.082 FPS: 60 Texture Memory: 4.7mb System Memory: 4.67mb Stage Objects: 287 Jan 21 01:11:36.575 FPS: 60 Texture Memory: 4.7mb System Memory: 3.37mb Stage Objects: 287 Jan 21 01:11:37.073 FPS: 60 Texture Memory: 4.7mb System Memory: 6mb Stage Objects: 287 Jan 21 01:11:37.571 FPS: 60 Texture Memory: 4.68mb System Memory: 4.7mb Stage Objects: 283 Jan 21 01:11:38.065 FPS: 60 Texture Memory: 4.68mb System Memory: 7.53mb Stage Objects: 287 Jan 21 01:11:38.563 FPS: 53 Texture Memory: 4.68mb System Memory: 6.28mb Stage Objects: 287 Jan 21 01:11:39.057 FPS: 60 Texture Memory: 4.68mb System Memory: 4.98mb Stage Objects: 287 Jan 21 01:11:39.556 FPS: 60 Texture Memory: 4.68mb System Memory: 8.24mb Stage Objects: 287 Jan 21 01:11:40.081 FPS: 60 Texture Memory: 4.68mb System Memory: 6.81mb Stage Objects: 287 Jan 21 01:11:40.593 FPS: 60 Texture Memory: 4.7mb System Memory: 5.42mb Stage Objects: 287 Jan 21 01:11:41.091 FPS: 60 Texture Memory: 4.64mb System Memory: 8.71mb Stage Objects: 289 Jan 21 01:11:41.590 FPS: 46 Texture Memory: 4.64mb System Memory: 7.53mb Stage Objects: 289 Jan 21 01:11:42.097 FPS: 60 Texture Memory: 4.64mb System Memory: 5.99mb Stage Objects: 287 Jan 21 01:11:42.634 FPS: 30 Texture Memory: 4.7mb System Memory: 4.62mb Stage Objects: 287 Jan 21 01:11:43.159 FPS: 48 Texture Memory: 4.7mb System Memory: 8.42mb Stage Objects: 283 Jan 21 01:11:43.670 FPS: 59 Texture Memory: 4.7mb System Memory: 6.85mb Stage Objects: 287 Jan 21 01:11:44.162 FPS: 60 Texture Memory: 4.68mb System Memory: 5.35mb Stage Objects: 279 Jan 21 01:11:44.740 FPS: 36 Texture Memory: 4.7mb System Memory: 9.77mb Stage Objects: 289 Jan 21 01:11:45.283 FPS: 60 Texture Memory: 4.7mb System Memory: 8.3mb Stage Objects: 287 Jan 21 01:11:45.890 FPS: 60 Texture Memory: 4.7mb System Memory: 6.62mb Stage Objects: 289 Jan 21 01:11:46.428 FPS: 40 Texture Memory: 4.7mb System Memory: 11.23mb Stage Objects: 287 Jan 21 01:11:47.029 FPS: 56 Texture Memory: 4.7mb System Memory: 9.54mb Stage Objects: 291 Jan 21 01:11:47.604 FPS: 60 Texture Memory: 4.7mb System Memory: 7.79mb Stage Objects: 287 Jan 21 01:11:48.167 FPS: 33 Texture Memory: 4.7mb System Memory: 6.06mb Stage Objects: 275 Jan 21 01:11:48.967 FPS: 32 Texture Memory: 4.7mb System Memory: 11.14mb Stage Objects: 287 Jan 21 01:11:49.460 FPS: 60 Texture Memory: 4.7mb System Memory: 9.45mb Stage Objects: 289 Jan 21 01:11:49.956 FPS: 60 Texture Memory: 4.7mb System Memory: 7.73mb Stage Objects: 287 Jan 21 01:11:50.569 FPS: 55 Texture Memory: 4.58mb System Memory: 6.34mb Stage Objects: 287 Jan 21 01:11:51.124 FPS: 60 Texture Memory: 4.58mb System Memory: 11.57mb Stage Objects: 287 Jan 21 01:11:51.672 FPS: 51 Texture Memory: 4.58mb System Memory: 9.73mb Stage Objects: 287 Jan 21 01:11:52.196 FPS: 60 Texture Memory: 4.58mb System Memory: 7.86mb Stage Objects: 287 Jan 21 01:11:52.695 FPS: 56 Texture Memory: 4.58mb System Memory: 12.78mb Stage Objects: 287 Jan 21 01:11:53.238 FPS: 60 Texture Memory: 4.58mb System Memory: 12.11mb Stage Objects: 287 Jan 21 01:11:53.765 FPS: 59 Texture Memory: 4.58mb System Memory: 10.36mb Stage Objects: 287 Jan 21 01:11:54.358 FPS: 56 Texture Memory: 4.58mb System Memory: 8.51mb Stage Objects: 287 Jan 21 01:11:54.949 FPS: 23 Texture Memory: 4.58mb System Memory: 9.24mb Stage Objects: 289 Jan 21 01:11:55.478 FPS: 60 Texture Memory: 4.7mb System Memory: 13.09mb Stage Objects: 287 Jan 21 01:11:56.101 FPS: 60 Texture Memory: 4.7mb System Memory: 11.21mb Stage Objects: 289 Jan 21 01:11:56.702 FPS: 33 Texture Memory: 4.7mb System Memory: 9.24mb Stage Objects: 287 Jan 21 01:11:57.401 FPS: 38 Texture Memory: 4.7mb System Memory: 7.29mb Stage Objects: 283 Jan 21 01:11:58.061 FPS: 30 Texture Memory: 4.7mb System Memory: 14.28mb Stage Objects: 283 Jan 21 01:11:58.791 FPS: 60 Texture Memory: 4.7mb System Memory: 12.24mb Stage Objects: 287 Jan 21 01:11:59.531 FPS: 54 Texture Memory: 4.7mb System Memory: 10.21mb Stage Objects: 287 Jan 21 01:12:00.521 FPS: 38 Texture Memory: 4.7mb System Memory: 8.06mb Stage Objects: 287 Jan 21 01:12:01.368 FPS: 60 Texture Memory: 4.7mb System Memory: 15.53mb Stage Objects: 287 Jan 21 01:12:02.185 FPS: 60 Texture Memory: 4.7mb System Memory: 13.5mb Stage Objects: 287 Jan 21 01:12:03.043 FPS: 28 Texture Memory: 4.7mb System Memory: 11.43mb Stage Objects: 289 Jan 21 01:12:03.865 FPS: 57 Texture Memory: 4.7mb System Memory: 9.27mb Stage Objects: 287 Jan 21 01:12:04.734 FPS: 30 Texture Memory: 4.7mb System Memory: 16.94mb Stage Objects: 287 Jan 21 01:12:05.723 FPS: 34 Texture Memory: 4.7mb System Memory: 14.63mb Stage Objects: 287

Towards the end as you can see there are brief moments where the FPS drops to about half.

I am not sure why it says there are 287 Stage Objects, because there are only 60 asteroids, one player with a health bar and two thrusters, two buttons, and a radar system with blips for some of the asteroids. This should only account for about half of the # of stage objects that is being printed out.

Do physics bodies count as stage objects?

Objects that are no longer within a certain range of the player are removed and only a maximum of 60 objects are allowed besides the player. 

I made sure I am running one instance of the level. I only called the creation function for the level once in main.lua:

level.createLevel(10, 60)
  1. I’m not sure about this, but based on the name (current stage is the top group that owns all other groups and objects) I’m assuming stage objects are any display object (not physics bodies) that have been created and still exist somewhere. 

This is ALL display objects that have been created till that point and not deleted, regardless of whether you are showing them in the visible area of the screen or not.

One way you could have MORE stage objects than you think should be counted:

  • create a scene with objects
  • change scenes to a new scene and create more objects w/o destroying last scene
  1. I think your removal code is not working as expected.  Furthermore, if those objects all have bodies… then they are slowing down your game.
  1. I am not switching between scenes, there is currently only one scene so far in this game. However, one thing I did notice was that when I moved the max limit for asteroids from 60 to 59 as a test, the number of stage objects went down by 4, from 287 to 283. 

I am not sure why, though. This means that the asteroids account for 240 of the display objects. 

  1. I will put together a test to see if the code is actually removing the asteroids.

That tells me, for every asteroid you think you’re making you’re making 4.  So I think you’ve got a code bug.

I’m curious what your test will be as  you may run into issues.  By the fact that you’re trying to measure the state of the system you are affecting it.  Specifically you need a way to count objects.  Counting them requires a reference.  Having a reference keeps them from garbage collecting.

You may very well need a mentor/experienced coder to look at your code for you if you stay stuck.

Hi,

I can’t speak to the frame-rate-drop issue, but I believe a bug in Visual Monitor is what’s causing a too-high object count to be reported.

See below for fix:

local function deepNumChildren(group) local function countChildren(agroup) local count = 0 \<== 'count' was originally being initialized to '1' if agroup.numChildren and not agroup.isMonitor then count = count + agroup.numChildren for i = agroup.numChildren, 1, -1 do count = count + countChildren(agroup[i]) end end return count end return countChildren(group) end

Btw, ‘deepNumChildren’ is a function within the VisualMonitor source code.

After initializing ‘count’ to ‘0’ instead of ‘1’, it began working as expected (i.e. When I create one display object, the object count reported increases by only one).

Hope this helps :slight_smile:

I checked the code on the monitor and it is not very accurate/useful for FPS monitoring.

A much better solution is to time each frame and return an average of those rather than what it is doing which is returning a snapshot frame rate based on the current frame every <game fps>/10.

Oh, so would I add the values of each FPS snapshot add them up and then divide them by 10?

yes - that will give a average FPS and is more representative of how fast your game is actually running.

I did this fix, and there are now 143 stage objects. I think that is a more accurate number.

This will give a true figure

local function countAllDisplayObjects(group) local count = 0 local function countChildren(agroup) if agroup.numChildren and not agroup.isMonitor then count = count + agroup.numChildren for i = agroup.numChildren, 1, -1 do countChildren(agroup[i]) end end end countChildren(group) return count end print( countAllDisplayObjects(display.getCurrentStage()) )

Is this about right?

 FPSValues[#FPSValues + 1] = min( display.fps, int( 1000 / ( curTime - prevTime ) ) ) count = count + 1 if (count % 10 == 0) then for i = 1, #FPSValues do trueFPS = trueFPS + FPSValues[i] end trueFPS = trueFPS / (#FPSValues + 1) print(trueFPS) FPSValues = {} end
FPSValues[#FPSValues + 1] = min( display.fps, int( 1000 / ( curTime - prevTime ) ) ) count = count + 1 local FPSValues = 0 if (count % 10 == 0) then for i = 1, #FPSValues do FPSValues = FPSValues + FPSValues[i] end local avgFPS = FPSValues / #FPSValues print("FPS = "..avgFPS) FPSValues = {} end

Thank you. However, the problem now comes with fixing this problem.

When I remove an asteroid, the # of stage objects decreases by 2. Also I notice that the more I hit asteroids, the more the System Memory value increases, and the more the FPS drops. 

So after removing a radar, I discovered that the asteroids are being created and removed properly.

Every time I added an asteroid, a “blip” is added to the radar, hence why the removal of an asteroid results in a loss of two stage objects.

However, my previous observations still stands:

I notice that the more I hit asteroids, the more the System Memory value increases, and the more the FPS drops. 

My first guess is, “You’re not correctly removing/clearing references to the objects and they aren’t getting garbage collected.”

Questions:

  1. Are you tracking references to asteroids, bullets, effects, etc. in more than a single variable?

  2. Are you perhaps using a table to track objects?

  3. If you’re using a table are you doing this:

    asteroids[#asteroids+1] = new_asteroid – TERRIBLE and naive way; leads to issues like you’re seeing.

or this:

asteroids[new\_asteroid] = new\_asteroid -- BEST WAY
  1. If you’re doing the first one… why?  That is a bad way to track objects in a action game.

_ Note:   To anyone who says it is faster, so it is better… “You are ‘Verrückt’.” _

By the time you have enough objects in a table to notice the speed difference you’ll have crushed your FPS by having too many display objects.

  1. Do you do it because you need to count asteroids?  If so, are you aware that SSK provides a table count helper

    local numAsteroids = table.count( asteroids )

  2. Do you use a finalize event listener on your asteroids?  If you don’t… you should.

    local asteroid = … your code to make an asteroid asteroids[asteroid] = asteroid – put it in a tracking table. function asteroid.finalize( self ) asteroids[self] = nil – stop tracking in ‘asteroids’ table. end asteroid:addEventListener(“finalize”)

Thank you for that well written explanation. However, I do not use a table to track asteroids. 

Also, I do have a finalize listener, and I have confirmed that it runs properly (or at least runs) :

asteroidFinalize = function(self) local masterM = require "levels-management.moduleaccessor" masterM.enemyHUD.getHUD() masterM.enemyHUD.ignoreObject(self) ignoreList({"enterFrame"}, self) end