Remove bitmap dispose in animation. Is it a good idea?

As you already know. Sometime, if we play an animation, it takes much time to load the bitmap and leads to FPS drop. Especially if we use larger image for the animation. However, I found a little trick that allow you to save the bitmap to cache, so i won't be loaded again in next time you play the animation.


The trick is so simple. Just comment out bitmap.dispose in Sprite_Base.


dz8SYc6.png



The question is whether this is a good idea or not? I just tested it in a small project and works perfectly. But I don't know if we use it in a whole game.


Sorry for my bad english ~
That is not a trick to save the bitmap to cache. The bitmap is already saved in the cache the first time it's loaded (Cache.animation). What you're doing is just not manually disposing of the bitmaps. Since you make the variable point to nil, the bitmap is lost to you anyway - you lose the handle to it, and it's not going to be reloaded next time you need it - it will be loaded from Cache.animation just as it always is. So while the end result may be something along the lines of what you're seeking, you're not achieving it by saving the bitmap to cache.


If you were going to do the above, you may as well comment out those entire two blocks. No point in reducing something then checking its value if you're not going to do anything afterwards.


If you're not disposing it, but still setting it to nil at the end of that method, either the GC is going to dispose of it anyway, or your memory usage is going to gradually increase the longer you play the game (especially as you see more and more animations played). I don't think either is a very good practice.
Disposing the bitmap actually does dispose the bitmap stored in Cache.

@ani_bitmap1 = Cache.animation(animation1_name, animation1_hue)@ani_bitmap2 = Cache.animation(animation2_name, animation2_hue)This grabs a reference to the bitmap stored in cache, and later when you say
Code:
@ani_bitmap1.dispose
The same referenced bitmap is disposed.I don't know how this affects the cache.
There you go then 🙂 I thought Cache.animation gave a copy.
Yeah, I knew it. Just because I removed the dispose function, then I said the bitmap was saved in Cache. We don't need to reload same file using Bitmap.new. Because it's so time consuming for large image. Since the concept of Cache itself is to store data in order to faster access (Both RGSS3 and our computer).


I would like to hear another opinion if there's any 🙂
That dispose is just in case you use too much hue change for animations. 

Code:
  def self.hue_changed_bitmap(path, hue)    key = [path, hue]    unless include?(key)      @cache[key] = normal_bitmap(path).clone      @cache[key].hue_change(hue)    end    @cache[key]  end
Oh, that's makes sense. I didn't think of the hue changed bitmap. So basically, if I remove the dispose function, the image with same filename will be stored in different bitmap object depends on the variation of its hue which may take our memory usage more. But, I don't want to my game causes more lag either :/
An animation bitmap would be around 1 - 5mb in ram (I tried with Attack11 animation, it would be around 4mb). So if you have around 1 - 2gb ram, this wouldn't be a big matter, except you use too many big animations and/or hue change them.

Anyway, you can try these if you want a better performance:

1, Dispose the animation(s) that is not used recently (for example, for hue change cache, you have a maximum bitmap number is around 50, when a new bitmap created, dispose the bitmap at the beginning of cache hash).

2, Cache most used animations, dispose all other ones.
Okay, I just tested myself. I played all default animations by iteration including mine. And the result is

Spoiler
cache.jpg
When I re-enable the dispose function

Spoiler
cache2.jpg
And, I would like to take the second option. Skill animations are the most used animation.


Another idea is, what if I add an option to determine whether to use Cache to store animation or not? And also have an option to clear the cached animations as well
Doesn't that defeat the purpose caching?


Without knowing anything about how memory works, I would assume that if something is "cached" in the program memory, then you would expect the memory used to be higher than if it's not being cached.


I'm not sure how disabling the dispose is a trick to cache the animation.

Original thread: https://forums.rpgmakerweb.com/threads/remove-bitmap-dispose-in-animation-is-it-a-good-idea.20738/