Jump to content

Memory Leak & Performance Optimization


Phodex Games
 Share

Go to solution Solved by Josh,

Recommended Posts

Hi,

I am in the final stage of the development of my game and now its time to bring the performance to a decent level. So I did some research and found out that over the time my framerate begins to drop from 60 - 40, maybe even further did not try it out. The effect is not so drastical if I put my player into an empty scene with no other objects and scripts running, but still existant and noticable. The results of my research showed that the script memory increases from time to time. Garbage collection happens, but still the memory is getting higher and higher. Of course I though it was due to my code and I had generated a memory leak, but then, when I tried the leadwerks build in FPS Demo Level I got a little nervous when I encountered the same issue. I tried to create a more extreme situation and started the level in Debug Mode and left it alone for about 30 minutes.

First it looked like this:

 5ae34f7ca3d2f_FPSTestLevel2018-04-2717-43-24-56.thumb.jpg.94352ed3da52f50b927fb2d66d1f8c9c.jpg

After 30 minutes:

5ae34f8751917_FPSTestLevel2018-04-2718-14-37-67.thumb.jpg.b81bc737674979e140cb6a0d26a583a9.jpg

After 40 minutes:

5ae34f8bea5da_FPSTestLevel2018-04-2718-24-16-24.thumb.jpg.9c3d22b405aa4e55a0519617ffa2c41b.jpg

When I took a closer look I saw that the Memory decreases, as the script memory increase, but for some reason that still meant an fps drop of about 8 frames. It recovers again over time. Also the memory usage went negativ? By the way I encounterd this behavior even on a empty map with only one camera...

So several question come up to me, some specific to this problem some of more general nature. As I am very happy with the result of my project and it turned out very professional, I also want to provide a good performance for my audience, so I really would need some help, due to my limited knowledge in this sector, as I am no well trained sofware engineer :):

1. What does the memory usage & script memory usage actually say ? I guess Mem usage is fonts, textures, etc. but what exactly is script usage? Is it how much RAM my code is taking?

2. In which unit is the mem usage provided here? I would guess its in bytes?

2. I this memory leak normal, or should I call it memory shift, as it seems to shift memory to the script usage?

3. Can I overcome that memory leak and how, as it seems to negativly affect my framerate?

3. If someone knows guides or information about optimizing lua code performance wise and how to overcome code bottlenecks it would be kind if you could share this with me :)

This is what I have already read and found: 

https://www.leadwerks.com/community/blogs/entry/1866-common-bottlenecks/

https://www.leadwerks.com/community/topic/13854-performance-question/?hl=performance

https://www.leadwerks.com/community/topic/6369-memory-leak/

I encounterd some more problems, but I try to solve them by my self first, as I did not much research on them. I for example have some lights flickering or disappearing when it should not disappear, or my lightquality change during runtime still results in disabled lights. When I fail solving them you will hear from me again soon :D

If you read so far I thank you very much and I really appreciate it, I hope I provided all needed information and all was clear to read and understand.

Link to comment
Share on other sites

  • Solution

Lua memory usage is calculated as follows:

luamem = 1024*lua_gc(Interpreter::L,LUA_GCCOUNT,0) + lua_gc(Interpreter::L,LUA_GCCOUNTB,0);

In debug mode on Windows system memory is calculated like this:

//Exact memory usage, but only works in debug mode:
_CrtMemState memstate;
_CrtMemCheckpoint(&memstate);
return memstate.lSizes[0]+memstate.lSizes[1]+memstate.lSizes[2]+memstate.lSizes[3]+memstate.lSizes[4];

The "Memory usage" stat that is displayed is the system memory minus the Lua memory. So there may be an error in that calculation. I used an int to store the lua memory value, maybe it went over the limit for a 32-bit signed integer?

You can get the system memory like this yourself: System::GetMemoryUsage()

However, I do not see evidence of anything wrong here. The framerate is stable after 40 minutes. If it really was leaking in a way that affected performance, you would continue to see a decrease over time. I think we can attribute the lower framerate after 20 minutes to your computer running some other task, or perhaps you just checked it at a moment when the garbage collector was running.

You actually scared me with the title of this thread, but your screenshots demonstrate a program working as it should, except I should have used a long value to calculate the memory displayed.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

1 hour ago, Josh said:

However, I do not see evidence of anything wrong here. The framerate is stable after 40 minutes. If it really was leaking in a way that affected performance, you would continue to see a decrease over time. I think we can attribute the lower framerate after 20 minutes to your computer running some other task, or perhaps you just checked it at a moment when the garbage collector was running.

Hmm I would not consider this as solved so quickly, because in my own project I experienced a drop from 60 to 40 and going down and it did not refresh, also when I am not wrong the 22 fps in the demo stayed and it was not just for a few seconds or so. Maybe I even messed up some screenshots, so the third one may be from another restart. The Problem is examining this problem takes some time. I am currently doing this. I will try System:GetMemoryUsage() and see if the memory goes up as well. I think I should put it to extreme and let it run for like an hour. I will respond again if I did that. I will also try running it in normal mode and just observe the fps without taking a look at memory. However, even if the demo level is alright, I still have to find the issue in my own project...

Link to comment
Share on other sites

If you have a mem leak you should see it pretty quickly.

The only thing that will provide a completely accurate reading is Debug mode on Windows.

You can also call gccollect() in Lua each frame to immediately dump any collected stuff, but don't do this in your final game!

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...