1

I need to process many bitmaps in a short time and i perform that by using threads. Now, i remember about disposing all of them, i have no memory leaks. Generally my app opens few threads (i control count of them - now i execute only 4 at the same time), and each of one process one big bitmap and does some additionall graphic processing.

Everything works fine until i process let me say - up to 100 bitmaps in a row. My app uses opendialog, so i open 100 first bitmaps then my app is automatically processing them. After it's finished, i open next batch of 100 bitmaps and so on (so between those batches there is a significant pause). This works fine. And the app is all the time opened. I see no memory leaks, after processing of last batch it stays stable, i can process more.

Problem appears when i select more bitmaps in a single batch - more than 300, so my app process all of them without a second of pause. Near the end i'am getting GDI+ Out of Memory error and my app hangs with 1.7 GB memory consumed.

My guess is that system is not able to release memory in so short amount of time (and my app is reserving more). Is it possible? How to deal with this? I don't want to just add a dumb delay in process. I'd like to manage that.

Update: The reason is simple, the App was mistakenly compiled as 32 bit and so had only ~2GB available memory. While it takes time for GC to free memory while app is still reserving more for more bitmaps, app reaches the limit, and hangs.

I compiled the app to 64 bit and it works flawlessly. In addition i will control memory usage, so it isn't working so unattended as now. Thanks for all tips and interesting points to improve!

  • 2
    It is very hard to tell something about it without any code to see how you are processing the images.. – Jordy van Eijk Mar 19 '13 at 09:47
  • 1
    read this http://stackoverflow.com/questions/233596/best-practice-for-forcing-garbage-collection-in-c-sharp ...what you need probably is to use force garbage collection to clean... – user123_456 Mar 19 '13 at 09:48
  • How many bitmaps are you processing in parallel? (i.e. how many tasks or threads are you starting?). NOTE: You do *not* need to force garbage collection - if .Net gets low on memory, it will do that automatically. Doing it yourself will not help in any way. – Matthew Watson Mar 19 '13 at 10:29
  • I process atm 4 bitmaps in threads, but they create some additional bitmaps while processing. I think the right answer is i mistakenly assumed that i compiled 64bit version, while it's 32bit. That's the reason program hangs at ~1.5 GB memory consumed. Thanks fo all tips anyway! – Tomasz Szkudlarek Mar 19 '13 at 10:38
  • @user123_456 The top-rated answer to the question you linked says: *"The best practise is to not force a garbage collection."* That is correct, by the way. So I think you might be misunderstanding what it says. – Cody Gray - on strike Mar 19 '13 at 18:09

2 Answers2

1

Depends on how long that "short time" said by you is, you definitely can look on Memory Mapped Files. Using them you dramatically reduce RAM memory pressure, and don't loose to much in speed. but, as I said first, it depends on how fast you need them to process.

So I would definitely suggest to look on this option too, measure and see if it fits your needs.

Dave Markle
  • 95,573
  • 20
  • 147
  • 170
Tigran
  • 61,654
  • 8
  • 86
  • 123
1

Is your application compiled as a 32 bit process? If so, you will run out of memory at 2Gb no matter how much RAM the machine has. Compiling as 64 bit will allow you to make full use of all the system memory, and will use swap space if you begin to run out of RAM (this will of course slow down the application, but may be a better solution than throwing out of memory exceptions).

Sean Reid
  • 911
  • 9
  • 19
  • Sean Reid - you are right. I didn't notice that the 64 bit version of compiler is not included in Express version of VS, so i compiled 32 bit version, and the memory usage at the point when app hangs really says that. I will try to compile it in 64 bit compiler and in addition i'll try to force garbage collector maybe ;-) – Tomasz Szkudlarek Mar 19 '13 at 10:36
  • I tested 64 bit version - no problems at all. I have a peak at aprox 2 GB memory reserved, but then, after all images are processed memory is released and app stays stable. – Tomasz Szkudlarek Mar 19 '13 at 15:30