Forcing MV to unload assets?

I'm having a problem with MV filling up the memory of iOS hardware after showing a series of images and playing sounds.


Is there any JS command to force it to clear assets out of the memory that are no longer needed?
No, that's the garbage collector's job, which runs periodically and destroys objects no longer referenced. Are you using plugins? I'm assuming you do, because a base RPG Maker game is rather uhhh... generic. Anyway, it's possible that your plugins are designed badly. If a lot of data is referenced by a plugin, but never used, the Garbage Collector can not release the memory, because something is still holding it, and the garbage collector does not know if it will be used, so it does not touch it. This is how memory leaks happen.


These issues are very easy to come by in very large plugin systems, such as complex menus, battle systems, anything that works with Audio or Images (especially as these always take up a relative lot of memory to use). You should check to make sure your plugins were well-designed.


Additionally, these memory leaks aren't going to just show up on iOS and nowhere else. You can run the game on your desktop/laptop/whatever, and use the debugging tools included with NWJS (chrome) to try to find the source of the memory leaks. That should help you narrow down where all your memory is going.
No, that's the garbage collector's job, which runs periodically and destroys objects no longer referenced. Are you using plugins? I'm assuming you do, because a base RPG Maker game is rather uhhh... generic. Anyway, it's possible that your plugins are designed badly. If a lot of data is referenced by a plugin, but never used, the Garbage Collector can not release the memory, because something is still holding it, and the garbage collector does not know if it will be used, so it does not touch it. This is how memory leaks happen.


These issues are very easy to come by in very large plugin systems, such as complex menus, battle systems, anything that works with Audio or Images (especially as these always take up a relative lot of memory to use). You should check to make sure your plugins were well-designed.


Additionally, these memory leaks aren't going to just show up on iOS and nowhere else. You can run the game on your desktop/laptop/whatever, and use the debugging tools included with NWJS (chrome) to try to find the source of the memory leaks. That should help you narrow down where all your memory is going.



Great points as always. 🙂


I disabled some addons and now I only have a few of Yanfly's running, which I trust to not do that kind of thing.


Though it still gives me the occasional memory warning, it's not crashing the game like it was previously so that's a start.


Where would I be able to see memory usage through the console? I use the "window" command to see everything that's running, but I'm not sure where to look for which assets are loaded into memory.
You've gotta look beyond just the console. The chrome debugger gives you a lot of buttons to press! You should probably start with these buttons:


sy1bvii.png
You've gotta look beyond just the console. The chrome debugger gives you a lot of buttons to press! You should probably start with these buttons:





I'm using Cordova so I'm not sure if I have access to those tools. :\
I'm using Cordova so I'm not sure if I have access to those tools. :\


Additionally, these memory leaks aren't going to just show up on iOS and nowhere else. You can run the game on your desktop/laptop/whatever, and use the debugging tools included with NWJS (chrome) to try to find the source of the memory leaks. That should help you narrow down where all your memory is going.
Oh, I thought we were talking about building for Android, which is where I had seen Chrome used in the past. My mistake.
At this point, I know exactly where my memory is going: it's being used by images.


What I don't know is if there's any way to force MV to clear the image cache on command...
I don't really suggest clearing the cache every now and then since that would mean that most images that you're constantly using will then have to be frequently loaded back from their files which will bring a lot of "load time" to your game, esp if these images are large. You might end up freeing memory for a while but then end up having a laggy game due to the constant reloading of resources.


It would be better to make sure than none of those images won't be used soon, and make sure any unneeded images are not referenced so that the GC can remove them. If it still takes up a lot of memory with just the actual images that are constantly being used, I think it's time to think about optimizing the images or using less of them because if your game is already optimized (no unused images saved in cache etc) and it still uses a lot of memory, clearing the cache often will surely result to the load issues. 


Anyway if you really want to do it, I think this is the function


ImageManager.clear()


It might not delete them directly, but at least it unloads everything that the cache holds so hopefully the GC will then remove those from the memory since they aren't referenced anymore.
Anyway if you really want to do it, I think this is the function


ImageManager.clear()


It might not delete them directly, but at least it unloads everything that the cache holds so hopefully the GC will then remove those from the memory since they aren't referenced anymore.



Perfect!


And yeah, I actually did an image/sound crunch recently and it didn't seem to help. :\


I'm going to create a testing system that will sit there and play the game on its own to see how it's managing the memory. I was running Yanfly's main menu manager until recently and it seems as though shutting it off has helped a great deal. I didn't realize it was so resource intensive...


In any case, thanks much and I'll see what I can do with this. 🙂
I don't really suggest clearing the cache every now and then since that would mean that most images that you're constantly using will then have to be frequently loaded back from their files which will bring a lot of "load time" to your game, esp if these images are large. You might end up freeing memory for a while but then end up having a laggy game due to the constant reloading of resources.


It would be better to make sure than none of those images won't be used soon, and make sure any unneeded images are not referenced so that the GC can remove them. If it still takes up a lot of memory with just the actual images that are constantly being used, I think it's time to think about optimizing the images or using less of them because if your game is already optimized (no unused images saved in cache etc) and it still uses a lot of memory, clearing the cache often will surely result to the load issues. 


Anyway if you really want to do it, I think this is the function


ImageManager.clear()


It might not delete them directly, but at least it unloads everything that the cache holds so hopefully the GC will then remove those from the memory since they aren't referenced anymore.



Another question in this same vein: is there a similar command to unload sounds, etc.?


Also, one to reset the game completely as if it had just booted up?
I don't think the game saves sounds in a cache or something.. at the very least Ace doesn't seem to do it (coz I've had problems playing a sound then trying to delete it while it's playing, windows tells me it cannot delete the file coz it's still in use) not sure with MV.
Anyway if you really want to do it, I think this is the function


ImageManager.clear()


It might not delete them directly, but at least it unloads everything that the cache holds so hopefully the GC will then remove those from the memory since they aren't referenced anymore.



Where would I input this function? I'm having a problem where one of my images are a few pixels off, like it sits about 2-3 pixels above the grid square. I've tried closing the program, saving the images under different names, and even restarting my computer but the problem persists. I'm wondering if there is a corrupted version of the image sitting somewhere that the program just won't let go of.


Here's an image of the problem. The yellow square should sit flush with the box but is pushed up a few pixels. Would appreciate any help with this.

imageproblem.PNG

Original thread: https://forums.rpgmakerweb.com/threads/forcing-mv-to-unload-assets.73612/