I think you'd have to dispose of the bitmap first, and then the sprite. If you dispose of the sprite first, you've lost the link to the bitmap.
I tried disposing the sprite first but I still have the link to the bitmap. Because dispose does not equal nil. I still would dispose the bitmap first because it makes more sense.
@sprite = Sprite.new
@sprite.bitmap = Bitmap.new(1,1)
@sprite.dispose
puts @sprite.bitmap.disposed? # <<< returns false and there is still a reference.
@Theo:
I do remember that setting a value to nil is not necessary in RGSS. Only dispose is.
@Mithran:
Yes the Cache is safe to use.
The code-blocks below causes a crash. Remove the 1000000.times and it works.
Spoiler
class Mem_test def initialize @bitmap = Bitmap.new(1000, 1000) endend1000000.times do @a = Mem_test.new @a = nilendThis variation crashes just like the one from above:
class Mem_test def initialize @bitmap = Bitmap.new(512, 512) @sprite = Sprite.new @sprite.bitmap = @bitmap @sprite.dispose endend4000.times do @a = Mem_test.new @a = nilend
So I made it more realistic and changed the bitmap size to 512x512 and the .times to 4000 instead.
My conclusion is that bitmaps MUST be disposed. An 1x1 bitmap does nothing. But a larger bitmap starts making an impact on the system and it won't crash right away but it will after an hour or so.
So there is a memory limit for RPG Maker... Because it crashes as soon as my memory reaches 1.899.708k (+0.5MB is > 1.9GB so that's why the number I guess) but my memory usage total is only about ~25% according to the task manager. I don't see why RPG Maker can't use all available RAM?
Crap now I have to check all my scripts...
Update:
Maybe I was wrong... The Garbage Collector just isn't smart enough to collect in time for my test code.
New code (no crash):
Spoiler
class Mem_test def initialize @bitmap = Bitmap.new(512, 512) @sprite = Sprite.new @sprite.bitmap = @bitmap @sprite.dispose endend10000.times do |i| @a = Mem_test.new @a = nil GC.start if i % 500 == 0end
My 2nd conclusion:
So my 2nd conclusion is that disposing the bitmap is not required as long as the GC collects before you reach the 1.9GB RAM limit.
So if you work with big or with lots of bitmaps, then you have a few options:
- Option 1: a basic understanding of the GC is required (Yeah you can hope that the GC is smart enough to collect before that happens and usually it will, but not always.)
- Option 2: you have to dispose the bitmaps manually
- Option3: make sure that you always stay within the 1.9GB RAM limit & set your minimum game RAM requirements to 1.9GB.
- Option4: use a custom Cache (see post below)
I personally prefer option #2 or #4.
/off-topic
I did notice that the Cache indeed disposes it's bitmaps when the default code no longer needs it. This was another reason for me to use non-cached bitmaps because some of my custom scripts crashed because they were using cached bitmaps that were disposed (but the reference was still there). The code that calls Cache.clear is probably internal so I can't see what it does nor alter it.