MZ Potential Memory Leak with NWJS

I noticed after testing my RMMZ game a bunch of times in a row that my CPU was at 100%, and that lots of instances of nwjs were open and taking up the majority of my memory. I had just been working in some Scripts that use arrays, so I was thinking that might have to do with it, so I figured I'd ask here.

Are there efficient ways to search for if something is causing a memory leak? I've looked on the Memory tab of the debug log, but I'm not sure how to efficiently read the data. I've seen the number raising to ~150-190 kB/s and in red, so I'm not sure if that is bad and might be causing this. So any insight on this would be helpful!
Memory.png


In addition, since I haven't used arrays a whole lot in my JavaScript experience, I wanted to see if anyone could spot anything wrong with my code that could be causing it to use up a lot of memory, or if there's a way to do this more efficiently that doesn't use as much memory.

Here's one of the arrays I have:
Spoiler: Arrays 1
JavaScript:
let activeMission = $gameVariables.value(159);

activeMission.push("EscapeFromThePartyPlace"); // 1
activeMission.push("Channel6Acting"); // 2
activeMission.push("OutlawBarParty"); // 3

And then this one has a bit more code:
Spoiler: Arrays 2
JavaScript:
let activeMission = $gameVariables.value(159);
let eventure = $gameVariables.value(117);
let failedMission = $gameVariables.value(161);
var index;
var removed;

if (activeMission.includes("EscapeFromThePartyPlace") && eventure != "Party Sanctuary") {
    failedMission.push("EscapeFromThePartyPlace");
    index = activeMission.indexOf("EscapeFromThePartyPlace");
    removed = activeMission.splice(index, 1);
}

I also have a few Conditional Branches that check for things like this:
JavaScript:
$gameVariables.value(159)[1] != undefined;

There are some more instances of this type of code throughout, but this is the main code that I added that I thought might be a contributor.
By the sound of your problem description your issue seems to be the 100% CPU.
I suggest you go to the performance and record a session where you play and hit 100% CPU. This will give you much more data on what costs performance-wise.
Keep your mind open and don't exclusively focus on memory leaks.

Good hunting!

*hugs*
- Zeriab
I'm not seeing sufficient evidence of a memory leak. 18 MB is not a lot, and neither is an increase of 156 kB/s.

In fact, there's no need to worry about memory usage rising. This is normal for JavaScript, which has no explicit memory management, and has to rely on a so-called "Garbage Collector" (GC) instead. As your game runs, its memory requirements will keep growing as the code requires more and more variables, then the GC will automatically scan the memory to find variables that are no longer accessible (no longer in use), and will set that memory free.

In an RPG Maker game, this may happen when your player teleports to another map. Since you're loading a different tileset, the variable holding the tileset image will now hold a different tileset image, losing any reference to the previous tileset data in the process. The GC will eventually come around, notice the old tileset data, and collect it to free the memory it's using. This doesn't happen instantly, and there's no way to predict when GC will run.

As a result you need to monitor your memory usage over a long period of time (which can be several minutes to a couple of hours), as your memory usage slowly increases, then suddenly drops, then slowly increases again, etc. If the memory usage trend increases (the maximum and minimum memory usage keeps growing with each GC run), you might have a memory leak. Otherwise you're safe.
Just to add - it's not abnormal to see most of your CPU getting used, especially if you have several programs open and one is a video game. If it's consistently sitting at 100% for an extended period while you play, that might not be right, but it's not unusual for programs to use the resources available to them and occasionally peak that high.
Thanks for the responses, everyone!

I noticed that when I'm regularly working in the editor, my CPU is around 2-4%, and when I playtest for the first time, it only goes up to about 8-10%. So it seems like it just builds up after closing and reopening playtests a bunch (which is how it got up to 100% last night). So it probably won't be a real issue in the end for anyone playing my game at least. It's just a slight inconvenience to have to completely close out of all of the nwjs processes and RPG Maker every so often when I'm playtesting and developing, which seems a little odd to me, since I haven't had this happen in the past before.

It's good to know that this sounds like it's mostly normal though. I have a pretty powerful PC with 16 GB of RAM, so I was just surprised to see so much of the CPU being used up by an RPG Maker game.
I've experienced this too when I playtest several times (multiple instances of nwjs remain open, even after the playtest has been closed), though it happens somewhat randomly and I cannot reproduce it reliably. I usually check my Task Manager once I hear my computer's fan being loud - it's not a big deal, but it's annoying. I've not experienced it with playing a deployed game so far, though.

It seems to be a bug that was reported back in May: https://forums.rpgmakerweb.com/index.php?threads/a-couple-of-bugs-version-1-2-1-steam.136906/

Are you using the standalone version or the Steam version, @Sarena? I'm using the Steam version and have considered trying the standalone version to see if the same problem occurs.
Are you using the standalone version or the Steam version, @Sarena? I'm using the Steam version and have considered trying the standalone version to see if the same problem occurs.

I'm also using the Steam version, with my project on version 1.3.2 (most recent). But that's a good idea to try the standalone version to see if that still happens, thanks!

Glad to know it's not just me though! Hopefully they make a patch to fix it.
If anyone experiencing this notices it again, can you try this to see if you're able to get an error report? I've been trying to nail this one down to replicate it.

1. Type event viewer in the search bar in the bottom left corner of your desktop. (Win 10)

2. Check to see if any errors have popped up mentioning MZ and if so, please copy the text of the error and share it either here, or in a DM to me.

If you share it here, put it in a spoiler since it might be a lot of text.

Also, if you could share exactly what you're doing when it happens that would be helpful. :LZSjoy:
If anyone experiencing this notices it again, can you try this to see if you're able to get an error report? I've been trying to nail this one down to replicate it.

1. Type event viewer in the search bar in the bottom left corner of your desktop. (Win 10)

2. Check to see if any errors have popped up mentioning MZ and if so, please copy the text of the error and share it either here, or in a DM to me.

If you share it here, put it in a spoiler since it might be a lot of text.
I'll be sure to let you know if it happens to me again, thank you for the info on what to send! 🙂

Also, if you could share exactly what you're doing when it happens that would be helpful. :LZSjoy:

I was creating my own menu system that uses arrays (some of the code in my first post). This is basically just a few events on a map that the player is teleported to (the teleporting happens through the VisuStella MZ Main Menu Core). Here's a video of the types of things I was testing when I ran into this:



I'll keep you updated as I continue to test in the future though! Luckily it's only happened to me that one night, but if I notice it again, I'll be sure to post here or DM you.

Original thread: https://forums.rpgmakerweb.com/threads/potential-memory-leak-with-nwjs.139793/