I’ve got a handle on my texture memory, but I have a major system memory leak. What are the best ways to reduce system memory and what are the common causes for system memory leaks?
Incorrect use of globals is the #1 source of leaks.
To help find leaks, you can use a Global locker (don’t leave it on in production code).
- Get this code form SSK2’s external lib area (Credit to Niklas Frykholm):
https://raw.githubusercontent.com/roaminggamer/SSK2/master/ssk2/external/global_lock.lua
-
Put it any new lua file, for example: glock.lua
-
In main.lua BEFORE running your main game code (not necessarily the top of the file), do this:
local glock = require “glock” glock.lock( _G_ )
You can later unlock like this:
local glock = require "glock" glock.unlock( \_G\_ )
That way you can just lock around one set of code/files.
If a global is created while locked, you’ll get a verbose message in the console.
Another option to debugging this is to find and learn how to use a “Lua Profiler”.
This is high-level stuff though.
I’d say the number one source of memory leaks is creating objects and not properly destroying them.
Localise everything like your life depends on it.
@sgs I think we are in agreement, but I probably wasn’t clear.
When I say globals are the #1 issue…
I mean that people store display object references in globals that never get cleared and so when they remove the objects, they don’t get garbage collected.
In the end it all comes back to the use of globals when they should be not be used.
If people could simply get used to using the keyword ’ local’ life would be so much better for them and for us.
I really don’t know any other reason for memory leaks, that is not properly destroying objects/variables.
Usually, I don’t have memory leaks in my projects.
What I do is:
First, I don’t use globals period. Hate them and to me is just lazy programming using them. Functions have parameters/arguments for a reason. Use them. It’s only faster to use them in your head…when you have a problem you will waste much more time trying to solve it.
Second, try to create a system that is easy for you to remember all the objects you create so you can easily destroy them.
What I do is, with timers i create a table timers={}, every time i need a timer i just use timers[#timers+1]=timer.performWithDelay(…)
when I need to remove all my timers I just use a for i=#timers, 1 -1 do…
same for transitions, network requests, and sounds.
All objects I put them inside local groups when needed that are inside a global group. all timers, transitions, etc are attached to the global group so at the end I just need to take care of 1 variable…the global group. I have a recursive function that detects them all and deletes them according to them. (my algorithm is a little more complex than this, but it’s just to give you an idea)
All variables are locals so I don’t need to worry about them.
I don’t use Corona storyboard or composer to make transitions from one scene to another. when I started corona, the storyboard was buggy and after a while, it passed to composer…I didn’t want to learn every year a new way to do things so I created my self a transition scene function. Till now I’m still using it.
This works for me…never had a problem with memory leaking. You can try to create a system that works for you also.
Incorrect use of globals is the #1 source of leaks.
To help find leaks, you can use a Global locker (don’t leave it on in production code).
- Get this code form SSK2’s external lib area (Credit to Niklas Frykholm):
https://raw.githubusercontent.com/roaminggamer/SSK2/master/ssk2/external/global_lock.lua
-
Put it any new lua file, for example: glock.lua
-
In main.lua BEFORE running your main game code (not necessarily the top of the file), do this:
local glock = require “glock” glock.lock( _G_ )
You can later unlock like this:
local glock = require "glock" glock.unlock( \_G\_ )
That way you can just lock around one set of code/files.
If a global is created while locked, you’ll get a verbose message in the console.
Another option to debugging this is to find and learn how to use a “Lua Profiler”.
This is high-level stuff though.
I’d say the number one source of memory leaks is creating objects and not properly destroying them.
Localise everything like your life depends on it.
@sgs I think we are in agreement, but I probably wasn’t clear.
When I say globals are the #1 issue…
I mean that people store display object references in globals that never get cleared and so when they remove the objects, they don’t get garbage collected.
In the end it all comes back to the use of globals when they should be not be used.
If people could simply get used to using the keyword ’ local’ life would be so much better for them and for us.
I really don’t know any other reason for memory leaks, that is not properly destroying objects/variables.
Usually, I don’t have memory leaks in my projects.
What I do is:
First, I don’t use globals period. Hate them and to me is just lazy programming using them. Functions have parameters/arguments for a reason. Use them. It’s only faster to use them in your head…when you have a problem you will waste much more time trying to solve it.
Second, try to create a system that is easy for you to remember all the objects you create so you can easily destroy them.
What I do is, with timers i create a table timers={}, every time i need a timer i just use timers[#timers+1]=timer.performWithDelay(…)
when I need to remove all my timers I just use a for i=#timers, 1 -1 do…
same for transitions, network requests, and sounds.
All objects I put them inside local groups when needed that are inside a global group. all timers, transitions, etc are attached to the global group so at the end I just need to take care of 1 variable…the global group. I have a recursive function that detects them all and deletes them according to them. (my algorithm is a little more complex than this, but it’s just to give you an idea)
All variables are locals so I don’t need to worry about them.
I don’t use Corona storyboard or composer to make transitions from one scene to another. when I started corona, the storyboard was buggy and after a while, it passed to composer…I didn’t want to learn every year a new way to do things so I created my self a transition scene function. Till now I’m still using it.
This works for me…never had a problem with memory leaking. You can try to create a system that works for you also.