Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Memory Leak with Script?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Zephiles
Advanced Cheater
Reputation: 0

Joined: 04 Feb 2016
Posts: 56

PostPosted: Tue Oct 11, 2016 10:39 am    Post subject: Memory Leak with Script? Reply with quote

In the script I have provided, I am pretty sure that I have some memory leak(s) somewhere.

If I select Yes to run the scripts for dynamic addresses, Cheat Engine will start to use a lot of memory, which seems to continue endlessly. If I select No, then Cheat Engine will not use any additional memory to run the scripts. To go along with this, if I select Yes and then re-run it and select No, then Cheat Engine will still not use any additional memory, but the memory used beforehand will not be returned.

Can someone try to help me with figuring out where the memory leak(s) are?



TTYDLuaWIP.lua
 Description:

Download
 Filename:  TTYDLuaWIP.lua
 Filesize:  5.05 KB
 Downloaded:  320 Time(s)

Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Tue Oct 11, 2016 11:02 am    Post subject: Reply with quote

Probably the DestroyArray does not work as your expect.

When a Lua table work as an array, it only see from index(or key) 1 to consecutive next integer index that its value is not nil.

So suppose the 1st time you run DestroyArray, the Timers array has 10 items in index 1,2,...10, the 10 items will be destroy and nil.
But in next time, it cannot see an non-nil value at index 1, it will stop enum the array ( ie. #tempExArray = 0 ), since the Timers added at index which is an ever increasing TC counter, so no items in Timers will be destroy and nil after 1st run of DestroyArray.

A quick fix may be instead of enumerate the table as array, enum all its key. ie.
Code:

for i=1,#tempExArray do --> change to
for i in pairs(tempExArray) do


bye~

_________________
- Retarded.
Back to top
View user's profile Send private message
predprey
Master Cheater
Reputation: 24

Joined: 08 Oct 2015
Posts: 486

PostPosted: Tue Oct 11, 2016 12:27 pm    Post subject: Reply with quote

collectgarbage("collect")

force a garbage collection at the end of every OnTimer cycle.

bye~
Back to top
View user's profile Send private message
Zephiles
Advanced Cheater
Reputation: 0

Joined: 04 Feb 2016
Posts: 56

PostPosted: Tue Oct 11, 2016 2:31 pm    Post subject: Reply with quote

I tried applying both of the solutions provided, and neither seem to be changing anything as far as I can see.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

Joined: 09 May 2003
Posts: 25262
Location: The netherlands

PostPosted: Tue Oct 11, 2016 2:39 pm    Post subject: Reply with quote

try this before destroying the timers and see if it makes any difference (Setting a method property to nil will explicitly free the function reference)
Code:

for i=1,#Timers do
  Timers[i].OnTimer=nil
end

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
predprey
Master Cheater
Reputation: 24

Joined: 08 Oct 2015
Posts: 486

PostPosted: Tue Oct 11, 2016 2:52 pm    Post subject: Reply with quote

Zephiles wrote:
I tried applying both of the solutions provided, and neither seem to be changing anything as far as I can see.


use this. i tested it, and the memory stays around the same at ~32.9MB. you probably missed out adding collectgarbage() to one of the timer functions.

after clicking yes for the dynamic addressing, CPU usage remained steady at ~2-3%. opening memory browser will cause a spike in my CPU usage of ~7%, opening userdefined symbols will cause another spike of ~7%. thereafter closing the forms will not reduce the CPU load, and destroying the arrays then executing the dynamic addressing script again will cause CPU load to spike to 15% again.

EDIT: problem solved. download removed.


Last edited by predprey on Tue Oct 11, 2016 3:34 pm; edited 2 times in total
Back to top
View user's profile Send private message
Zephiles
Advanced Cheater
Reputation: 0

Joined: 04 Feb 2016
Posts: 56

PostPosted: Tue Oct 11, 2016 2:59 pm    Post subject: Reply with quote

Dark Byte wrote:
try this before destroying the timers and see if it makes any difference (Setting a method property to nil will explicitly free the function reference)
Code:

for i=1,#Timers do
  Timers[i].OnTimer=nil
end

This also doesn't seem to be changing anything.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Tue Oct 11, 2016 3:01 pm    Post subject: Reply with quote

I've never bothered looking into it, but timers make Lua allocate a few KB every time they're run. This can end up outpacing Lua's garbage collector.

Example:
Code:
stop = false

local gcT = createTimer()
gcT.Interval = 100
gcT.OnTimer = function()
  print(collectgarbage("count"))
  gcT.Enabled = not stop
end

local timers = {}
for i=1,20 do
  timers[i] = createTimer()
  timers[i].Interval = 1
  timers[i].OnTimer = function(t) t.Enabled = not stop end
end

After opening CE, the process's private memory usage was 38.56 MB. After executing that script for 1 minute, private memory usage was at 127.2 MB (logarithmic growth).

To prevent this, make the garbage collector more aggressive:
Code:
collectgarbage("setpause",120)
collectgarbage("setstepmul",400)

Using the previous example, private memory usage started at 38.3 MB and never got above 40MB.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Zephiles
Advanced Cheater
Reputation: 0

Joined: 04 Feb 2016
Posts: 56

PostPosted: Tue Oct 11, 2016 3:08 pm    Post subject: Reply with quote

ParkourPenguin wrote:
I've never bothered looking into it, but timers make Lua allocate a few KB every time they're run. This can end up outpacing Lua's garbage collector.

Example:
Code:
stop = false

local gcT = createTimer()
gcT.Interval = 100
gcT.OnTimer = function()
  print(collectgarbage("count"))
  gcT.Enabled = not stop
end

local timers = {}
for i=1,20 do
  timers[i] = createTimer()
  timers[i].Interval = 1
  timers[i].OnTimer = function(t) t.Enabled = not stop end
end

After opening CE, the process's private memory usage was 38.56 MB. After executing that script for 1 minute, private memory usage was at 127.2 MB (logarithmic growth).

To prevent this, make the garbage collector more aggressive:
Code:
collectgarbage("setpause",120)
collectgarbage("setstepmul",400)

Using the previous example, private memory usage started at 38.3 MB and never got above 40MB.

Alright, this worked. Just to clarify, would those same values be applied to my table, or should they be adjusted at all?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Tue Oct 11, 2016 3:24 pm    Post subject: Reply with quote

It doesn't really matter as long as memory usage is under control and your script is still running fast enough. The default values are 200/200; adjust them to your liking as specified here.
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Zephiles
Advanced Cheater
Reputation: 0

Joined: 04 Feb 2016
Posts: 56

PostPosted: Tue Oct 11, 2016 3:32 pm    Post subject: Reply with quote

ParkourPenguin wrote:
It doesn't really matter as long as memory usage is under control and your script is still running fast enough. The default values are 200/200; adjust them to your liking as specified here.

OK, thanks for the help!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites