Memory and FPS issue on Windows

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
ThePC007
Prole
Posts: 36
Joined: Sat Jan 30, 2016 3:00 pm
Contact:

Memory and FPS issue on Windows

Post by ThePC007 »

Hey guys,

I'm currently working on a sandbox game that could be compared to games like Terraria or Starbound in that there's a 2D world filled with blocks that the player can manipulate.
The world consists of chunks (similar to how it works in Minecraft) which are loaded by a number of threads when the camera approaches them.

This is what the game currently looks like:
Image

Each colored square represents a chunk. I zoomed out a little in order to make the borders where the chunks are being loaded visible. The camera / view starts at the top left corner of the world and going right or down makes the game load and render more chunks.

Now, I have two problems with with the game:
The first one is the memory usage. When I load the chunks the memory usage increases. When I return to the main menu of the game, the memory usage doesn't return to its original value, though, despite removing the tables which store the chunks. This issue seems to be more present on Windows than Linux, however it seems like the game suffers from it on both platforms. Please note that the memory that's increasing is not in use by lua (I've checked with collectgarbage("count")), but rather by the Löve framework itself (I guess?).
The second problem is that the more chunks I load, the slower the game runs. Again, the slowdown still persists even after returning to the main menu. This problem is, however, only persistent on Windows. I tested the game on Xubuntu and the fps go back to their original value (around 300 fps) after returning to the main menu, while still staying low on Windows. Please note that the slowdown that's happening when loading more chunks before going to the main menu is understandable, as the game is rendering more and more chunks and it's doing so without any sprite batches whatsoever (Don't worry, I'll fix that later.).

If you wish to download the love and test the game out for yourself, here are the controls:
  • SPACE - to launch / start the game when it's in the launcher / main menu
  • WASD - move the camera / view around (please move right and / or down, as the camera starts in the top left corner of the world)
  • 1 AND 2 - zoom out / in (warning: be careful when zooming out; there's a bug that makes the view flip when you zoom out too much)
  • ESC - return to main menu
Thanks in advance,
- ThePC007
Attachments
broken_game.love
(17.79 KiB) Downloaded 627 times
Last edited by ThePC007 on Wed Feb 10, 2016 7:56 am, edited 1 time in total.
Löve is love, Löve is life.
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: Memory and FPS issue on Windows

Post by drunken_munki »

Hey dude

I ran your .love, I can confirm that the game constantly increases memory use and drops fps from 500 fps, 50 MB ram to 250 fps 100 MB after a minute or so of wandering to the bottom right.

After escaping back to main the fps went back to 500 and the ram in use stayed about the same.

System: Windows 7 x64 with AMD cpu / gcard.

I tried to look through the code but there were too many subfolders and files for my head to get around right now.
User avatar
ThePC007
Prole
Posts: 36
Joined: Sat Jan 30, 2016 3:00 pm
Contact:

Re: Memory and FPS issue on Windows

Post by ThePC007 »

drunken_munki wrote:Hey dude

I ran your .love, I can confirm that the game constantly increases memory use and drops fps from 500 fps, 50 MB ram to 250 fps 100 MB after a minute or so of wandering to the bottom right.

After escaping back to main the fps went back to 500 and the ram in use stayed about the same.

System: Windows 7 x64 with AMD cpu / gcard.

I tried to look through the code but there were too many subfolders and files for my head to get around right now.
Thanks for checking it out. I'm surprised that the fps went back after escaping to the main menu, despite the fact that you were running Windows. Leaves me wondering whether it's a hardware problem on my end or a problem with Windows 10 (which I am running).

I guess I might list some files that might be important:
  • mods/vanilla/rooms/ingame/update.lua - This code is run every update when you are in-game. It's what chooses which chunks need to be loaded and handles the communication with the chunk loading threads. It also handles the controls for the cam, so if you'd like to make it move faster, this is the file you'd need to edit to do so.
  • mods/vanilla/rooms/first/threads/chunk_loading.lua - This is the code of the chunk loading thread.
  • mods/vanilla/functions/ - This is where the functions are located. Particularly, the functions load_chunk.lua, render_chunk.lua and request_load_chunk.lua may be worth looking into.
Löve is love, Löve is life.
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Memory and FPS issue on Windows

Post by pgimeno »

Your drawn_chunks table grows as squares are made visible. That causes mods.vanilla.rooms.ingame.draw to loop over a growing number of elements.

Found it by making this change:

Code: Select all

--- main.lua.old	2016-02-09 13:22:52.000000000 +0100
+++ main.lua.new	2016-02-09 18:26:22.000000000 +0100
@@ -31,6 +31,8 @@
 end
 
 function love.run()
+profi = require('ProFi')
+profi:start()
 	if love.math then
 		love.math.setRandomSeed(os.time())
 	end
@@ -50,6 +52,8 @@
 			for name, a,b,c,d,e,f in love.event.poll() do
 				if name == "quit" then
 					if not love.quit or not love.quit() then
+profi:stop()
+profi:writeReport('report.txt')
 						return a
 					end
 				end
(sorry about the lack of indentation, I do that with temporary debug code so it stands out and I can find and remove it easily), and then including this file: https://github.com/mindreframer/ProFi.lua

The clue was that most of the time was spent in world_get_draw. Then I confirmed it by printing #draw_chunks.
User avatar
ThePC007
Prole
Posts: 36
Joined: Sat Jan 30, 2016 3:00 pm
Contact:

Re: Memory and FPS issue on Windows

Post by ThePC007 »

pgimeno wrote:Your drawn_chunks table grows as squares are made visible. That causes mods.vanilla.rooms.ingame.draw to loop over a growing number of elements.

Found it by making this change:

<snip>

(sorry about the lack of indentation, I do that with temporary debug code so it stands out and I can find and remove it easily), and then including this file: https://github.com/mindreframer/ProFi.lua

The clue was that most of the time was spent in world_get_draw. Then I confirmed it by printing #draw_chunks.
Thanks, I guess, but I already know that this is what's causing the slow down while the player is in-game. (That's what I meant with
ThePC007 wrote:Please note that the slowdown that's happening when loading more chunks before going to the main menu is understandable, as the game is rendering more and more chunks and it's doing so without any SpriteBatches whatsoever (Don't worry, I'll fix that later.).
)
The problem is that both the slow down and the high memory usage still persist when going back to the main menu, which sets the drawn_chunks table to nil and stops drawing the chunks.

EDIT: Since you are bringing up ProFi, though, maybe I should try and use that to figure out what's causing the slow down. I could implement it into my project, load a lot of chunks and return to the main menu. Then I'd hopefully see what's causing the problem.

EDIT 2: Trying to write a report using ProFi results in this error:

Code: Select all

ProFi.lua:248: report.txt: Permission denied
EDIT 3: Apparently I need to run this as an admin on Windows to have the permission to write the report.
Last edited by ThePC007 on Tue Feb 09, 2016 8:21 pm, edited 1 time in total.
Löve is love, Löve is life.
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Memory and FPS issue on Windows

Post by pgimeno »

Sorry, it seems I skipped that part. I can see the memory increase. Collecting garbage doesn't help. There seems to be a leak somewhere.

But I can't see the slowdown. The FPS are at 468 in the menu, then at 430 at start; I wander around until they are at about 200, then return to the main menu. 468 again. Then back to the game. 430 again. This is Linux, I wonder if that has any influence.
User avatar
ThePC007
Prole
Posts: 36
Joined: Sat Jan 30, 2016 3:00 pm
Contact:

Re: Memory and FPS issue on Windows

Post by ThePC007 »

pgimeno wrote:Sorry, it seems I skipped that part. I can see the memory increase. Collecting garbage doesn't help. There seems to be a leak somewhere.

But I can't see the slowdown. The FPS are at 468 in the menu, then at 430 at start; I wander around until they are at about 200, then return to the main menu. 468 again. Then back to the game. 430 again. This is Linux, I wonder if that has any influence.
I only have the fps problem on my Windows 10 machine. My other one running Xubuntu doesn't have this issue and drunken_munki, who's running Windows 7 reported that he also doesn't have it, so it's either a problem with Windows 10 (would be great if someone else who is also running Windows 10 could confirm that, by the way) or a hardware problem on my end.
Given that the memory that's leaking is not in use by lua, it seems like it's a bug in the Löve framework. Should I file a bug report?

EDIT:
Alright, I've implemented ProFi, loaded a lot of chunks and went back to the main menu. After a while, the fps went back to 300 - 400, just like on Linux (although it took noticeable longer for it to do so). When I removed it again, I had the same problem as before - the fps stayed low. Does Löve have some kind of built-in garbage collection or something that could have been triggered by the execution of ProFi? (Although, that wouldn't make much sense I guess, since the memory usage problem still persists.) Seriously, this is confusing.
Löve is love, Löve is life.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Memory and FPS issue on Windows

Post by bobbyjones »

Maybe you are just producing a lot of garbage. If you produce a ton of garbage then gc would chase after it costing frames maybe. And love does not have a GC that would interfere with your code(I think anyways.) I don't know if gc would run for more than one frame tho. I havent had the chance to look at code yet tho
User avatar
ThePC007
Prole
Posts: 36
Joined: Sat Jan 30, 2016 3:00 pm
Contact:

Re: Memory and FPS issue on Windows

Post by ThePC007 »

bobbyjones wrote:Maybe you are just producing a lot of garbage. If you produce a ton of garbage then gc would chase after it costing frames maybe. And love does not have a GC that would interfere with your code(I think anyways.) I don't know if gc would run for more than one frame tho. I havent had the chance to look at code yet tho
That might actually be it. Lua 5.1 uses tricolor garbage collection, which (if I understand it correctly) would use more than just a frame to collect all the garbage (as opposed to stop-the-world garbage collection). This still doesn't explain my memory problem, though.
Löve is love, Löve is life.
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Memory and FPS issue on Windows

Post by pgimeno »

I've added collectgarbage("collect") in the update function of the main menu and it doesn't do anything.

You use threads. Is it possible that the threads are holding references to the data?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests