How do you really tell if your game has a memory leak?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How do you really tell if your game has a memory leak?
So this question isn't about diagnosing where they come from but more how you know if one is occurring. I've been told the task manager is a good place to check, but it seemingly updates so slowly that it's hard to tell. Over the course of my game, memory usage seems to steadily rise from ~70mb to ~76, with a particular jump (though small) when I enter the level gamestate and then quit. There is memory collection happening because the memory does seem to fall occasionally, but I don't know whether any of this is normal. Should I do an extended play session to see if performance starts to drop? Right now it stays at a steady 60.
-
- Party member
- Posts: 559
- Joined: Wed Oct 05, 2016 11:53 am
Re: How do you really tell if your game has a memory leak?
You could use the following function:
And see if the memory it reports continues to rise and rise. However, do note that due to lua's garbage collector's nature, you will likely see that the memory will rise for a while, and then have a sharp drop when the collector does a full sweep. You can slightly tweak the garbage collector how often this occurs, but either way you'd likely see some up-and-down pattern when the memory usage is graphed out. Something like jprof can help you gather memory usage data, and narrow down any problem areas.
Windows' task manager mostly works in particularly nasty or obvious cases (consuming megabytes of RAM rapidly), because it might not budge too much if the memory usage slowly rises, or your game does big memory allocations every so often. Sometimes Windows might allocate more memory than you currently need, and slowly adjust it down, in my experience.
Code: Select all
collectgarbage("count") -- reports used lua memory in kilobytes
Windows' task manager mostly works in particularly nasty or obvious cases (consuming megabytes of RAM rapidly), because it might not budge too much if the memory usage slowly rises, or your game does big memory allocations every so often. Sometimes Windows might allocate more memory than you currently need, and slowly adjust it down, in my experience.
Re: How do you really tell if your game has a memory leak?
Right, thanks. So it seems like there's no real obvious way to tell. I'm guessing for now I should just keep working on things and keep an eye out. I'm probably worrying over nothing.
Re: How do you really tell if your game has a memory leak?
It's quite uncommon to have a memory leak when you're not using FFI. The most common cause of memory usage growth is not a memory leak; it's allocating objects and then disposing of them without releasing them, as Lua won't bother to garbage-collect them early because it doesn't "see" that they are big from the Lua side. For Lua, it's OK to have a few thousands of objects lingering before cleaning them up. It isn't a problem with pure Lua, but when the Lua objects are actually userdata (as Löve objects are) and they are big, then it becomes one.
The solution in that case is to either avoid creating temporary objects too often, or actively releasing them (using Object:release() ) before disposing of them.
The solution in that case is to either avoid creating temporary objects too often, or actively releasing them (using Object:release() ) before disposing of them.
- BrotSagtMist
- Party member
- Posts: 659
- Joined: Fri Aug 06, 2021 10:30 pm
Re: How do you really tell if your game has a memory leak?
Practical example for a memory leak:
Have a textbox, have it have a zoom in/out function.
We reload our font with
_Font_ is overwriten each time so logically we would assume the old font data is discardet as it is no longer referenced.
This is not the case because the generatet font file is outside of the scope of the normal garbage collector, it will pile up.
So give it like 5 minutes of zooming in and out and your window crashes.
Have a textbox, have it have a zoom in/out function.
We reload our font with
Code: Select all
Font = love.graphics.newFont( zoom)
This is not the case because the generatet font file is outside of the scope of the normal garbage collector, it will pile up.
So give it like 5 minutes of zooming in and out and your window crashes.
obey
Re: How do you really tell if your game has a memory leak?
Right. Fortunately I'm not doing anything like this. I preload all resources in one place and reuse them, even canvases.
Re: How do you really tell if your game has a memory leak?
That's not a memory leak, that's what I mentioned as "the most common cause of memory usage growth". Adding a `collectgarbage("collect")` at some point (e.g. immediately after that line) should keep memory usage under control.BrotSagtMist wrote: ↑Mon May 20, 2024 3:31 pm Practical example for a memory leak:
Have a textbox, have it have a zoom in/out function.
We reload our font with_Font_ is overwriten each time so logically we would assume the old font data is discardet as it is no longer referenced.Code: Select all
Font = love.graphics.newFont( zoom)
This is not the case because the generatet font file is outside of the scope of the normal garbage collector, it will pile up.
So give it like 5 minutes of zooming in and out and your window crashes.
- BrotSagtMist
- Party member
- Posts: 659
- Joined: Fri Aug 06, 2021 10:30 pm
Re: How do you really tell if your game has a memory leak?
This is the very definition of a memory leak.
And of course its not anymore if you call garbage collect, i mean.... thats the point.
What are you trying to say here?
And of course its not anymore if you call garbage collect, i mean.... thats the point.
What are you trying to say here?
obey
-
- Party member
- Posts: 559
- Joined: Wed Oct 05, 2016 11:53 am
Re: How do you really tell if your game has a memory leak?
Memory leaks can get a bit wonky to define in any language that has an automatic garbage collector, that may or may not act upon some rapid increase of garbage. Some of them might clean it more aggressively, while others might struggle to keep up, unless you specifically manually call for garbage collection like how you can in lua. However, personally, I tend to liken memory leaks to some orphaned (inaccessible) data that still remains in memory, that may never be released. If an automatic garbage collector can clean that up, it's not exactly a leak in my mind, but rather a space leak because the program is using excess memory it doesn't need. Of course, that's just arguing over semantics.
- BrotSagtMist
- Party member
- Posts: 659
- Joined: Fri Aug 06, 2021 10:30 pm
Re: How do you really tell if your game has a memory leak?
There is no fancy wording here.
Take the first line from wikipedia "a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations[1] in a way that memory which is no longer needed is not released."
Which i read as a clear black and white statement: You have a memory leak if you do not call garbage collect.
And since it is automatically called in lua i gave an example of where it is needed manually.
Take the first line from wikipedia "a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations[1] in a way that memory which is no longer needed is not released."
Which i read as a clear black and white statement: You have a memory leak if you do not call garbage collect.
And since it is automatically called in lua i gave an example of where it is needed manually.
obey
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 10 guests