0

I'm currently designing a 2D Game using a Game Engine I created and have decided to check how much memory I'm using for every second that passes.

I currently have a game screen that contains a total of 4097 game objects, each object contains at the very least, a sprite (bitmap) that is rendered to the screen each frame. Each sprite is a 32x32 pixel image.

The resulting MB I'm apparently using is roughly 1.10MB, is this too much, or am I doing okay? What other things should I take into consideration?

Also, just to show, this is how I'm checking the amount of memory I'm using:

double mb = MathHelper.ConvertBytesToMegabytes(GC.GetTotalMemory(true));
Console.WriteLine("Memory: " + mb);

and the "ConvertBytesToMegabytes" method:

public static double ConvertBytesToMegabytes(long bytes)
{
    return (bytes / 1024f) / 1024f;
}
  • this.... doesn't make any sense at all. Please be more specific about what you want to know and also which memories are you interested in. The images are probably located somewhere in the graphic card memory and not considered by the GC. But this question is not specific enough to give an answer (my opinion at least) – grek40 Apr 08 '17 at 09:28
  • I'm using GDI+, so I don't know if this helps at all. I just want to know what my limits should be in terms of memory usage. –  Apr 08 '17 at 09:32
  • your question is unclear. are you running into problems with 1MB memory usage? try to get amount of memory used using this approach http://stackoverflow.com/a/750595/4767498 – M.kazem Akhgary Apr 08 '17 at 09:33
  • 'WorkingSet64' says 115mb, PrivateMemorySize64 says 109mb. –  Apr 08 '17 at 09:39
  • This looks more realistic, but it still doesn't explain what your actual question is... did you want to know how to measure memory (there you go) or how much memory is appropriate (a useless question unless you list every single involved library, tool, file, ...) or something completely different? – grek40 Apr 08 '17 at 09:55
  • I just wanted to make sure I was checking in the right place for the amount of memory being used. –  Apr 08 '17 at 10:06
  • So thank you for that :) –  Apr 08 '17 at 10:06

2 Answers2

1

GC.GetTotalMemory:

A number that is the best available approximation of the number of bytes currently allocated in managed memory.

If you are using GDI+, I assume you are using the Image class. However, its data are not located in the managed memory, and therefore cannot be obtained via invoking the garbage collector. Your managed data cost 1.1 MB, and that's completely okay for modern machines.

The memory cost of your bitmaps can be quite easily calculated. Assuming all the sprites are present in the memory, are 32x32 pixels big, and use 32-bit pixels, this gives us 16781312 bytes for the pixel data, or 16 MB. You should rely on this calculation more than on memory reports from the Process class.

I suppose your initial concern was that the reported amount of memory was too low to be able to store all the bitmap data. As you can see, you simply used a wrong method to obtain it. For other (more or less unreliable and confusing) methods to obtain the amount of memory, refer to this question.

Community
  • 1
  • 1
IS4
  • 11,945
  • 2
  • 47
  • 86
  • When using that method it tells me I'm using aroudn 115MB, which seems more realistic. So does 32-bit pixels use 32 bytes of data? 8 bytes for each color (RGBA)? –  Apr 08 '17 at 10:09
  • 32-bit pixels use 32 bits = 4 bytes in total. Each color component uses 1 byte. I have forgot to divide by 8 in the answer, fixed now. – IS4 Apr 08 '17 at 10:10
  • Oh okay, thank you for that. How would I calculate the total memory used in bytes? –  Apr 08 '17 at 10:15
  • So using the approach you suggested (getting the current process) tells me I'm using 109mb in total, is this something to worry about then? –  Apr 08 '17 at 10:17
  • @MathewO'Dwyer First of all, check if Process manager reports the same amount of memory. If so, you can assume the number is reliable. 100 MB is higher than average, but for a game which you wouldn't usually run multiple times, I don't think it's a problem. Usually you shouldn't worry about time or memory usage of your application unless they are noticeable in some way. – IS4 Apr 08 '17 at 10:21
  • Is Process Manager apart of VS? I don't quite understand. I just find it a little odd that I'm using so much memory, especially if the images are only around 16m in total. –  Apr 08 '17 at 10:28
  • I've realised the source of large memory usage. I can't believe I forgot about it. All the images are drawn to a bitmap, which is 4096x4096 pixels, I checked memory used before it's created, and it goes from 15MB, to 70MB. So, now I'm okay with that haha. –  Apr 08 '17 at 10:35
0

Unless the engine is not rendering all of the 4097 game objects for camera reasons you should be using 12-17MB, assumming a normal 32x32 bitmap size of ~3KB

SaraFlower
  • 755
  • 1
  • 8
  • 15
  • The engine (most of the time) will only render what's within the view port. So... about 35 objects at once are visible. But each object still contains the bitmap, it's just not being rendered. I just tried getting the total memory by using 'currentProcess.WorkingSet64' and converting it to megabytes, now it says I'm using 115mb... –  Apr 08 '17 at 09:38