'dispose' in JS is removeChild?

Isn't THIS a reference to the bitmap object?

ImageManager.loadNormalBitmap = function(path, hue) { var key = path + ':' + hue; if (!this._cache[key]) { var bitmap = Bitmap.load(path); bitmap.addLoadListener(function() { bitmap.rotateHue(hue); }); this._cache[key] = bitmap; } return this._cache[key];};So as long as this._cache[key] exists, the bitmap will still be in memory. The sprite will be removed, but not the bitmap. And I see nothing that removes it from this._cache except ImageManager.clear, which appears to never actually be called.
If this is how caching works then there could be serious performance issues with larger maps that contains lots of events. In theory when a scene is switched everything should be blown away and re built. It makes sense to cache for the web I suppose. but even then you'll have sever performance issues. 

Another way that this should have been dealt with is a state, you update the state, like In ReactJS, and then read from it. It makes the application super fast and super responsive. With how they cache things here I fear that a map with 20+ events could cause serious lag, unless they are smart enough to not activate and or draw said events until the player is with in "eye distance" of those things.

So ... If we use that logic and apply it to how RPG maker caches things then its fair to say that caching wont be too much of an issue, because its only fetched when the player enters the area of said events, sprites and so on. Or when the player activates a window like pressing esc or chatting. (those things make sense to be cached)

About what were talking about here:

When things are popped off the scene or the a window is disposed it makes sense to cache them so we don't have to use precious resources to re-create them especially for rebuilding them. How ever as a programmer it makes sense that if you want to recreate them you pop them off the cache and let GC take care of them. The issue is that if you depend on x a lot then recreating x could cause performance issues.

So ...

Cache what you can, but know that at first creating the cache will be slow, maybe noticeably so. But after that things should be rather fast. 
Isn't THIS a reference to the bitmap object?

-- snip --
If you want to clear the bitmap completely you'll have to also delete it from the cache, but you don't want to do this often, if at all. When did you clear the cache in Ace? Did you ever? When you ran dispose in Ace it only cleared the instance of the bitmap, not the cache. Except if you made the mistake I did once where I set a new bitmap to the reference in the cache, ran dispose later and Ace gave me back an empty bitmap from cache every time I requested it thereafter.

Another thing to consider: if you blit (the .blt function) parts or the whole of a source bitmap to a fresh bitmap on a sprite, that's not a reference to the original bitmap anymore but a unique object. Clearing that sprite/bitmap clears it from memory completely when the GC gets around to it. The IconSheet, portraits and -- I'm pretty sure -- character sprite sheets are all blitted parts of the original full image files.

If you consider how most of MV's resources are formatted, you rarely want to clear the cached objects because you'll be redrawing from most of them constantly. Character sprites are on sheets of multiple sprites, and so one image will be used for up to 8 different sprites. The same goes for portraits.
When did you clear the cache in Ace? Did you ever? When you ran dispose in Ace it only cleared the instance of the bitmap, not the cache.
Good point, and I hadn't even thought of that.


So the conclusion is, we don't have to dispose of sprites & bitmaps in MV like we did in Ace? The garbage collector will take care of it all for us?
If you want to clear the bitmap completely you'll have to also delete it from the cache, but you don't want to do this often, if at all. When did you clear the cache in Ace? Did you ever? When you ran dispose in Ace it only cleared the instance of the bitmap, not the cache. Except if you made the mistake I did once where I set a new bitmap to the reference in the cache, ran dispose later and Ace gave me back an empty bitmap from cache every time I requested it thereafter.


Another thing to consider: if you blit (the .blt function) parts or the whole of a source bitmap to a fresh bitmap on a sprite, that's not a reference to the original bitmap anymore but a unique object. Clearing that sprite/bitmap clears it from memory completely when the GC gets around to it. The IconSheet, portraits and -- I'm pretty sure -- character sprite sheets are all blitted parts of the original full image files.


If you consider how most of MV's resources are formatted, you rarely want to clear the cached objects because you'll be redrawing from most of them constantly. Character sprites are on sheets of multiple sprites, and so one image will be used for up to 8 different sprites. The same goes for portraits.
We never cleared the cache in Ace. That's why if you loaded too many images, your game will start slowing down over-time.


Not to mention, if you're using plugins to cache audio, video, and other things, this will simply explode even faster.


Ideally you would never have to clear the cache, since the purpose of the cache is to provide fast access to data, but unfortunately we still may have memory limitations to work with in this day and age.
So in my understanding, when I made a bitmap for example and I want to remove it, by simply making a sprite go to null, makes it removed from the cache?
No, the variable you set to null was holding a reference to the Sprite object in memory. Assuming it's not referenced from anywhere afterwards, it will be deleted by the garbage collector. Now for the underlying Bitmap, this depends:


- If you loaded the image via new Bitmap(...) or Bitmap.load(...) and it was only referenced by the Sprite, the garbage collector will get it, too.


- If you loaded the image via ImageManager.load<...>, it's still referenced by the ImageManager and will stay in cache.
Ahh, that's getting clear. So is there a way we can tell ImageManager to ditch the whole Sprite for example when we terminate it? Or does leaving it as is won't affect the overall performance of the game?
you would find it in cache and delete it
Ahh, that's getting clear. So is there a way we can tell ImageManager to ditch the whole Sprite for example when we terminate it? Or does leaving it as is won't affect the overall performance of the game?
Every resource that remains in memory will take up memory.


Eventually, you're going to run out of memory.


So yes, it isn't necessarily a good idea to just always leave it around, but you should also make sure that it will be a problem for end-users.


For example, perhaps your game is set up so that it will never hit memory limitations. For example, if you ever only load in 10 small bitmaps, then it's not really an issue unless they're running on an old raspberry pi or something,

Original thread: https://forums.rpgmakerweb.com/threads/dispose-in-js-is-removechild.50479/