Quote:
Others say that although this is important, they don't actually do much in terms of memory issues.
When not using
using
-statements the CLR
eventually comes around to cleaning up those objects but there's no guarantee on when this will happen. So it can lead to inconsistent behaviour. When using
using
-statements you're definitely on the safe side in this regard
*
but there can be other issues.
However, my first observation is that you don't actually use
using
-statements on all disposable objects which you create: The
Bitmap.FromFile(..)
-call creates a bitmap that you don't dispose of. I suggest changing it like this (within the
With
-statement):
Using tileBmp As Bitmap = Bitmap.FromFile(ArtFilePrefix & _TextureRange(0).StartAddress + _TextureBlock.OffsetTextureTable(.TextureIndex) & ".png")
MapGfx.DrawImage(tileBmp,
New Rectangle(TileWide * 16, TileHeight * 16, 16, 16),
New Rectangle(0, 0, 16, 16),
GraphicsUnit.Pixel)
End Using
Other than that: Do you compile your project for x86 architecture? Then the 16MB RAM of your PC don't help - the 32bit address space of a x86 process can't utilize that much and because of memory fragmentation the practical limit can be even lower than the theoretical one.
I'd suggest to first change your code as shown above. If the problem persists and you currently compile your project for x86, change it to x64.
*
Edit: With
using
-statements you're on the safe side as in that the CLR will clean up those objects with the
next garbage collection (not instantly, as my above wording might imply). Since garbage collections are not guaranteed to happen before your application tries to allocate an amount of memory that will lead to an OutOfMemory-exception it's still possible that one occurs but it's far less likely opposed to not using
using
-statements.