Some advice with a possible memory leak.

Hi All,

A little while ago I discovered a major memory leak in my game. I have since fixed it, but this experience has made me start to really scrutinize memory usage. What I seem to be seeing is that some data on A map never disappears. It won't continuously reload if you repeatedly return to the map, but it also will never go away even if you return to title. Only if you fully close the game. I thought it was possibly due to other plugins in my game, but it also happens in a fresh base game. This extra data on a base game is pretty small, about 10-20 extra mb in ram on a very simple 3 map test game, but once other plugin data gets added that number grows to about 2-300 mb per 10-ish maps. At least in my current game, anyway.

Normally, I wouldn't think too much about this, but the thought occurred that if this persists and someone plays a large enough game long enough wouldn't that end up overloading someones ram? Hopefully I am misunderstanding something about how memory is allocated and eventually GCed but any advice or insight into either fixing this issue or knowing that it shouldn't be a problem would be greatly appreciated.
Are you using any plugins that could be related? Preloaders, performance upgrades? Are you taking steroids?

This is not default behavior. I ran my project and went between 5 different maps, RAM usage stayed at 40 MB the entire time.
That is weird, in my actual game I do use Galvs Preloader. But even in a new project created right now with only the default "made with MV" and "community basic" plugins the game loads in at 118-125mb and after I go through all three maps and return to the title menu it settles around 135 mb and doesn't ever return to the original 118-125mb as I would expect it to.
Are you using any plugins that could be related? Preloaders, performance upgrades? Are you taking steroids?

This is not default behavior. I ran my project and went between 5 different maps, RAM usage stayed at 40 MB the entire time.
But I'm shocked that you're only using 40mb. Just starting a completely fresh and empty game puts me at like 100mb
It was weird to me, too, so I tested again and this time it clocked at about 320MB usage.

I can't account for the discrepancy, unless I managed to continuously look at the wrong line last time 😛

But, again, that's constant. It spikes a bit when a new map is loaded, but within seconds it's back down to the same amount - there's no growth from moving between maps.
Yeah, I have the same behavior. Like it spikes then frees some memory as I go to each of the three maps and back to the title, but each time it does that, whats left is just a smidge higher. But it is only the first time. If I do the loop again it doesn't continue to build. It only adds to it if I go into a new map that I didn't do before. Like it seems like its storing data for faster load time upon return maybe? I honestly don't know. But if I am the only one experiencing this, I am at a loss for what to do.

Regardless, thank you for taking time to see if you're having a similar experience. I appreciate it.
The original engine had several memory leaks, but all of them were fixed by version 1.5.x
Any memory leak after that version would have come from plugins.

However, you never stated which version of the editor you are running, and which version of the core files your projects use - if you haven't updated your editor in a long time, your projects might still be of an old core with those memory leaks.

Other than that:
Preloaders are memory leaks by design - after all they tell the engine to load additional files into memory that are not yet needed by the engine, to reduce load times later at the cost of higher memory usage.
And because of that, automatic preloaders often do more damage than help, you should only use preloaders that require you to tell them when to load something and when to unload something.
it spikes then frees some memory as I go to each of the three maps and back to the title, but each time it does that, whats left is just a smidge higher. But it is only the first time. If I do the loop again it doesn't continue to build.
That's not what I'd think of as a memory leak. That kind of stabilisation suggests to me some kind of cache or memoisation, i.e. storing results in memory so you don't have to redo them next time.
Like it seems like its storing data for faster load time upon return maybe?
Yea, like that.

It could be the image cache: default max 10 MPix = 40 MB. You may have increased that limit with the Community_Basic plugin. A preloader is just another kind of cache, populated ahead of time instead of on demand. So it might (also) be the preloader, depending on what kinds of triggers it uses for loading resources (I haven't checked).

Also remember that RAM usage scales with the dimensions of the image, not the size of the file on disk. MV/Z use 4 bytes per pixel (1 byte per RGBA channel). E.g. the default gameover image is 50.7 KB on disk thanks to PNG compression, but its dimensions are 816 x 624 px, i.e. approx 500 000 px, therefore it will occupy ~2 MB in RAM.
The original engine had several memory leaks, but all of them were fixed by version 1.5.x
Any memory leak after that version would have come from plugins.

However, you never stated which version of the editor you are running, and which version of the core files your projects use - if you haven't updated your editor in a long time, your projects might still be of an old core with those memory leaks.

Other than that:
Preloaders are memory leaks by design - after all they tell the engine to load additional files into memory that are not yet needed by the engine, to reduce load times later at the cost of higher memory usage.
And because of that, automatic preloaders often do more damage than help, you should only use preloaders that require you to tell them when to load something and when to unload something.
Yeah lol I was there in the dark times pre 1.3 so I'm using 1.6.2 in both my actual game and my fresh test game. And in the test game I am not running any plugins.
As for preloaders I only use galvs to load about six images at start and any others I only load when needed to avoid the image (like a character) from blinking when switching images.

That's not what I'd think of as a memory leak. That kind of stabilisation suggests to me some kind of cache or memoisation, i.e. storing results in memory so you don't have to redo them next time.

Yea, like that.

It could be the image cache: default max 10 MPix = 40 MB. You may have increased that limit with the Community_Basic plugin. A preloader is just another kind of cache, populated ahead of time instead of on demand. So it might (also) be the preloader, depending on what kinds of triggers it uses for loading resources (I haven't checked).
Unfortunately, I am getting similar results in a fresh game with no plugins installed. But as for it not really being a memory leak, I see what you mean. But my issue with this is since the ram use increases in size whenever new data is added (a map with a new tileset or characters, etc.), if a game is big enough and someone plays a game continuously with out stopping eventually its going to get into ram issues. Which is why I disabled all of my plugins in my actual game trying to find the problematic plugin or plugins. But when having them all turned off still showed this behavior, I moved to a fresh game and still found this to be happening, with smaller numbers of course because there's less data.

Also thank you both so much for your insight.
Short answer: I wouldn't worry about it unless the numbers start getting ridiculous. 👋

Core RMMV has a few things that cause persistent-per-session RAM usage:
  • Image cache: as mentioned, default max of 40 MB, (de)populated on demand.
  • System sounds: the first 4 are cached on boot, the rest are loaded & cached on demand.
  • The full database: loads on game boot, includes blank records.
  • Config settings: loads on game boot, updates via in-game options menu.
On top of that there's all the temporary data, including the current scene instance, game windows, (self-)switches, variables, event data, picture data, many different sprites, etc. Some of this will reset when changing map or scene; pretty much all of it should reset on New Game.

There will also be overhead through the browser itself, which is NWJS (Chromium-based) for standalone/playtest: the app itself needs some RAM, plus a bit extra to handle the game's scripts. The core engine uses various Web APIs, including WebAudio and WebGL: it's possible some (parts) of these get loaded on demand and/or memoised for speed, causing a gradual controlled increase in memory usage per session. Windows itself does similar things, e.g. SysMain for frequently-launched apps.

Re GC: the solution is to find and fix the bad code. But I don't think that's an issue here.
Spoiler: technical
Garbage collection can be tricky: the app can only deallocate memory that can no longer be accessed from its allocator. Memory leaks are caused when data is allocated to memory, but references to it are not removed after that data is no longer needed. It is possible for that to happen on a one-time basis, e.g. overwriting the old forgotten data with new forgotten data, which is relatively harmless. It's much more obvious when it happens repeatedly, particularly if it's in the app's private working set, because all that unused data continues to stack up.
Thank you so much for your in depth response. I might need to reevaluate my game. If I play through what I have finished in one session, the ram usage climbs to around 1300-1500 mb. And I start to notice small but noticeable performance issues. If I stick to one area, I can pretty much play for as long as want because I’m not loading anything new in but if I tear through it all, that’s when I notice a problem. Blarg…

Regardless, thank you all for all of your responses. With any luck I'll figure out what specifically is pushing me over and fix it. I just wish the console didn't freeze after a few minutes of use.
Do you have a different computer you can test on?
Do you have a different computer you can test on?
I do, I will try both the test and the full game on it a little later today and see if I get similar results. Thanks for the suggestion.
Do you have a different computer you can test on?
Sadly, no luck. I get similar results on either machine. I guess that is normal behavior. looks like the next step is tracking down the plugins that are the most guilty amplifying the data saved into ram. It feels like a bandaid but that's all I got.

A semi related questions, in task manager, when I'm playtesting, sometimes there are 4 game.exe's and sometimes there are 5. Is this normal?
Not for me. Are your project's core scripts updated to 1.6.1 or higher? (you can check at the top of rpg_core.js).

I think I recall something about older versions not correctly terminating the playtest processes if the console was open when you quit...but I might be confusing something between MV and MZ. Don't take my word for it without looking it up.
screenshot.jpg

I did a quick test with a small project in MV and saw 5 processes grouped (View -> Group by type) under nwjs (playtest app); I also tried with MZ and saw 7. One process is the game window, named appropriately; I assume the others are async workers or something.

The process count does not have to remain static, either, e.g. when I launch the MV editor, it has only 2 processes: this increased to 7 on launching playtest. Something similar with MZ. There seem to be other factors involved, too, since sometimes it's 5 or 3 instead of 7.

It's common for a single application to launch multiple processes. E.g. Firefox (17) in my screenshot: I assume that's 1 main, +1 per tab (8), +1 per logic core (8)?

Whatever the cause, I don't see any strange resource consumption, and when I close a playtest/editor, all its processes disappear correctly. If yours aren't disappearing after the window closes, it is probably a bug, like Turan mentions. In that case, you can try to End Task them via the Task Manager.
I did a quick test with a small project in MV and saw 5 processes grouped
lol, I didn't even mentally parse those were multiple processes. I thought it was a PID 😛
Not for me. Are your project's core scripts updated to 1.6.1 or higher? (you can check at the top of rpg_core.js).
The core files of both my game and the test game I made are at least 1.6.1. I just double checked.

Whatever the cause, I don't see any strange resource consumption, and when I close a playtest/editor, all its processes disappear correctly. If yours aren't disappearing after the window closes, it is probably a bug, like Turan mentions. In that case, you can try to End Task them via the Task Manager.
Same, everything closes fine when I close out the project. I was just concerned with the fact that the amount of processes in the task manager would change sometimes and I was a tiny bit curious if it could be related to my issue with the ram usage slowly growing the longer someone plays a game. And by playing I mean by exploring. Staying in the same ares for any length of time is fine.

The issue I am seeing is if you continue to explore new areas that ram usage grows for me. And this is minor on a base game where the ram usage never goes above 200mb and the growth difference of only maybe 10-20 mb per every 5 maps or so. But in my full game with all of the plugins, images, etc. it balloons to about 3-400 mb, which is expected, but that left over data also balloons so by the time I am about 30 maps into my game I've spiked to 1200-1500mb and am experiencing minor performance issues. Like the first hit in battle won't display the damage numbers or sometimes text in a message wont display its color. So my guess is, that it looks like I need to try and reduce plugins as much as possible while keeping as much of their functionality that I can. As well as trying to optimize events and characters sets as much as I can so less sheets get loaded. Granted that's good practice normally and I was already doing that, but I think I need to push that further and hope it's enough.
So a few questions and troubleshooting things.
First, have you touched your PIXI js file at all? I saw some misinformation on reddit floating around that you can increase performance by grossly increasing the GC frame value. Iirc, they basically had garbage collection wait 30 minutes or something before performing any clean up. Bad idea.
Not saying you followed that particular advice, just whether or not you might have downloaded and/or edited PIXI.js yourself.

Second,
Could you post utilization before and after doing map transfers like you have explained. If you could, could you set up a PerfMon graph and export it? (just pause the data collection then screen shot it)
Here's a pretty good step by step walk through on accessing PerfMon and setting it up to monitor system RAM. For some reason, this article is for troubleshooting some unrelated software, but it's just one of the first results that came up with a decent step-by-step walk through.

PerfMon Tutorial provided by some...unrelated software.
First, have you touched your PIXI js file at all?

Second,
Could you post utilization before and after doing map transfers like you have explained. If you could, could you set up a PerfMon graph and export it? (just pause the data collection then screen shot it)

No I never touched PIXI. That is default in both my game and the fresh sample game

I wasn't sure if you meant the test game or my full game so I did both.
Here is what I got for the test game:
Spoiler: Test Game
RamGraph.png
I just wanted to point out the lower ram upon returning to the title screen. Its minimal here but present. Game started at 105 mb and ended at like 130mb.

And here are the Start and end to me playing my game until I started to have performance issues:
Spoiler: My Actual Game
RamGraphMyGamePlayThrough.png
RamGraphMyGamePlayThrough2.png
The ram at the start of the game leveled off at around 300mb by the time I was at the end of the session it was at around 1350mb. And I traveled through 70 maps. To note the lag happened during a graphics heavy part, but frames were already slightly dropping a little while before this point, this was just the point at which it fully stuttered as it processed a bunch of stuff.

I hope I did this correctly as you needed. Also I appreciate you showing my this tool.

Original thread: https://forums.rpgmakerweb.com/threads/some-advice-with-a-possible-memory-leak.162073/