I have a picture box that is displaying an image. The memory usage sits around 30 MB normally, but the image updates frequently and, even though the old images are disposed of, it can cause the memory to run out before garbage collection has a chance to run. Is there a more efficient way to display frequently updating images? Would I need to allocate a section of memory and manage it myself?
Asked
Active
Viewed 178 times
0
-
Old images may be disposed of, but that doesn't mean the memory is going to get reclaimed immediately. You can try calling `GC.Collect();` at the end of the update method. If the image is getting updated as frequently as you say, this is isn't the best thing to do, but it could give you an idea of whether long-standing garbage memory is your problem. – Abion47 Dec 11 '16 at 02:38
-
@Abion47 When GC runs, the memory usage goes back down to normal. I can make it go up to over a gig and it goes right back down to ~30 MB every time GC runs. It's just not freeing the memory frequently enough. I just confirmed it with `GC.Collect()`. – FlyingMonkey Dec 11 '16 at 03:58
-
1Some code would help understand if you're using managed resources, which you should explicitly release – Luc Morin Dec 11 '16 at 04:16
-
Calling *GC.Collect();* is generally a bad idea. Take look at this article [When to call GC.Collect()](https://blogs.msdn.microsoft.com/ricom/2004/11/29/when-to-call-gc-collect/) Probably you are doing something wrong but without any code we can't see what exactly. A general rule though. After disposing the object set it to *null* too! – γηράσκω δ' αεί πολλά διδασκόμε Dec 11 '16 at 09:54
-
I made a new project with similar code and loading a very large image into a picture box and GC runs much more frequently. I can't get it to go over 200 MB. On my project, GC cleans up all of the memory that's being used, but it does it much less frequently. If it was a memory leak, it wouldn't go back down to 30 MB every time GC runs. Unfortunately, providing code is very difficult because it spans four very large classes. I'll see if I can replicate the issue in a single class. – FlyingMonkey Dec 11 '16 at 19:27
1 Answers
0
I found a post with the same issue and the answers explain what's happening pretty well.
Garbage Collection not happening even when needed
More memory isn't really an option since this program should run on an average computer. I'm going to have to figure out a more efficient way to update the image.

Community
- 1
- 1

FlyingMonkey
- 51
- 8
-
This isn't an answer so much as it's an update on the question. As such, you should edit it into your question. – Abion47 Dec 12 '16 at 08:00
-
@Abion47 Aside from calling `GC.Collect()`, there isn't really a solution. I'm just going to have to figure out a different way to do it. – FlyingMonkey Dec 12 '16 at 16:48
-
Are you updating the image with different images, or is this something you can achieve with just one image instance? – Abion47 Dec 12 '16 at 20:32
-
@Abion47 It's multiple images that get merged together into one image. The contents change, and if they change too fast, it runs out of memory. I'm going to see if I can redo it and avoid declaring new objects for the images. I didn't bother avoiding it the first time through because I didn't know that garbage collection wasn't set up to handle it. – FlyingMonkey Dec 13 '16 at 00:19
-
Well, it's not the objects themselves that's the problem. `Bitmap` objects are actually incredibly cheap. It's the underlying image data that's the issue. One thing you could try is instead of merging them into one image, you can have them be a series of images that are positioned and sized to look like one image. (The image merging by its nature requires that every image has to essentially be stored in memory twice, doubling their footprint.) – Abion47 Dec 13 '16 at 00:22