views:

660

answers:

8

I have an application that is used in image processing, and I find myself typically allocating arrays in the 4000x4000 ushort size, as well as the occasional float and the like. Currently, the .NET framework tends to crash in this app apparently randomly, almost always with an out of memory error. 32mb is not a huge declaration, but if .NET is fragmenting memory, then it's very possible that such large continuous allocations aren't behaving as expected.

Is there a way to tell the garbage collector to be more aggressive, or to defrag memory (if that's the problem)? I realize that there's the GC.Collect and GC.WaitForPendingFinalizers calls, and I've sprinkled them pretty liberally through my code, but I'm still getting the errors. It may be because I'm calling dll routines that use native code a lot, but I'm not sure. I've gone over that C++ code, and make sure that any memory I declare I delete, but still I get these C# crashes, so I'm pretty sure it's not there. I wonder if the C++ calls could be interfering with the GC, making it leave behind memory because it once interacted with a native call-- is that possible? If so, can I turn that functionality off?

EDIT: Here is some very specific code that will cause the crash. According to this SO question, I do not need to be disposing of the BitmapSource objects here. Here is the naive version, no GC.Collects in it. It generally crashes on iteration 4 to 10 of the undo procedure. This code replaces the constructor in a blank WPF project, since I'm using WPF. I do the wackiness with the bitmapsource because of the limitations I explained in my answer to @dthorpe below as well as the requirements listed in this SO question.

public partial class Window1 : Window {
    public Window1() {
        InitializeComponent();
        //Attempts to create an OOM crash
        //to do so, mimic minute croppings of an 'image' (ushort array), and then undoing the crops
        int theRows = 4000, currRows;
        int theColumns = 4000, currCols;
        int theMaxChange = 30;
        int i;
        List<ushort[]> theList = new List<ushort[]>();//the list of images in the undo/redo stack
        byte[] displayBuffer = null;//the buffer used as a bitmap source
        BitmapSource theSource = null;
        for (i = 0; i < theMaxChange; i++) {
            currRows = theRows - i;
            currCols = theColumns - i;
            theList.Add(new ushort[(theRows - i) * (theColumns - i)]);
            displayBuffer = new byte[theList[i].Length];
            theSource = BitmapSource.Create(currCols, currRows,
                    96, 96, PixelFormats.Gray8, null, displayBuffer,
                    (currCols * PixelFormats.Gray8.BitsPerPixel + 7) / 8);
            System.Console.WriteLine("Got to change " + i.ToString());
            System.Threading.Thread.Sleep(100);
        }
        //should get here.  If not, then theMaxChange is too large.
        //Now, go back up the undo stack.
        for (i = theMaxChange - 1; i >= 0; i--) {
            displayBuffer = new byte[theList[i].Length];
            theSource = BitmapSource.Create((theColumns - i), (theRows - i),
                    96, 96, PixelFormats.Gray8, null, displayBuffer,
                    ((theColumns - i) * PixelFormats.Gray8.BitsPerPixel + 7) / 8);
            System.Console.WriteLine("Got to undo change " + i.ToString());
            System.Threading.Thread.Sleep(100);
        }
    }
}

Now, if I'm explicit in calling the garbage collector, I have to wrap the entire code in an outer loop to cause the OOM crash. For me, this tends to happen around x = 50 or so:

public partial class Window1 : Window {
    public Window1() {
        InitializeComponent();
        //Attempts to create an OOM crash
        //to do so, mimic minute croppings of an 'image' (ushort array), and then undoing the crops
        for (int x = 0; x < 1000; x++){
            int theRows = 4000, currRows;
            int theColumns = 4000, currCols;
            int theMaxChange = 30;
            int i;
            List<ushort[]> theList = new List<ushort[]>();//the list of images in the undo/redo stack
            byte[] displayBuffer = null;//the buffer used as a bitmap source
            BitmapSource theSource = null;
            for (i = 0; i < theMaxChange; i++) {
                currRows = theRows - i;
                currCols = theColumns - i;
                theList.Add(new ushort[(theRows - i) * (theColumns - i)]);
                displayBuffer = new byte[theList[i].Length];
                theSource = BitmapSource.Create(currCols, currRows,
                        96, 96, PixelFormats.Gray8, null, displayBuffer,
                        (currCols * PixelFormats.Gray8.BitsPerPixel + 7) / 8);
            }
            //should get here.  If not, then theMaxChange is too large.
            //Now, go back up the undo stack.
            for (i = theMaxChange - 1; i >= 0; i--) {
                displayBuffer = new byte[theList[i].Length];
                theSource = BitmapSource.Create((theColumns - i), (theRows - i),
                        96, 96, PixelFormats.Gray8, null, displayBuffer,
                        ((theColumns - i) * PixelFormats.Gray8.BitsPerPixel + 7) / 8);
                GC.WaitForPendingFinalizers();//force gc to collect, because we're in scenario 2, lots of large random changes
                GC.Collect();
            }
            System.Console.WriteLine("Got to changelist " + x.ToString());
            System.Threading.Thread.Sleep(100);
        }
    }
}

If I'm mishandling memory in either scenario, if there's something I should spot with a profiler, let me know. That's a pretty simple routine there.

Unfortunately, it looks like @Kevin's answer is right-- this is a bug in .NET and how .NET handles objects larger than 85k. This situation strikes me as exceedingly strange; could Powerpoint be rewritten in .NET with this kind of limitation, or any of the other Office suite applications? 85k does not seem to me to be a whole lot of space, and I'd also think that any program that uses so-called 'large' allocations frequently would become unstable within a matter of days to weeks when using .NET.

EDIT: It looks like Kevin is right, this is a limitation of .NET's GC. For those who don't want to follow the entire thread, .NET has four GC heaps: gen0, gen1, gen2, and LOH (Large Object Heap). Everything that's 85k or smaller goes on one of the first three heaps, depending on creation time (moved from gen0 to gen1 to gen2, etc). Objects larger than 85k get placed on the LOH. The LOH is never compacted, so eventually, allocations of the type I'm doing will eventually cause an OOM error as objects get scattered about that memory space. We've found that moving to .NET 4.0 does help the problem somewhat, delaying the exception, but not preventing it. To be honest, this feels a bit like the 640k barrier-- 85k ought to be enough for any user application (to paraphrase this video of a discussion of the GC in .NET). For the record, Java does not exhibit this behavior with its GC.

+14  A: 

Here are some articles detailing problems with the Large Object Heap. It sounds like what you might be running into.

http://connect.microsoft.com/VisualStudio/feedback/details/521147/large-object-heap-fragmentation-causes-outofmemoryexception

Dangers of the large object heap:
http://www.simple-talk.com/dotnet/.net-framework/the-dangers-of-the-large-object-heap/

Here is a link on how to collect data on the Large Object Heap (LOH):
http://msdn.microsoft.com/en-us/magazine/cc534993.aspx

According to this, it seems there is no way to compact the LOH. I can't find anything newer that explicitly says how to do it, and so it seems that it hasn't changed in the 2.0 runtime:
http://blogs.msdn.com/maoni/archive/2006/04/18/large-object-heap.aspx

The simple way of handling the issue is to make small objects if at all possible. Your other option to is to create only a few large objects and reuse them over and over. Not an idea situation, but it might be better than re-writing the object structure. Since you did say that the created objects (arrays) are of different sizes, it might be difficult, but it could keep the application from crashing.

Kevin
It's thing is to crash. So, this is a wrong answer. Edit to your edit: In theory, it allows for 2 gb allocations. In reality, not even close.
mmr
@mmr: Kevin did say that it wasn't a great idea, so let's be a bit kinder. For that matter, it doesn't cause a crash.
Steven Sudit
@Steven Sudit-- it absolutely causes a crash, with an out of memory exception. This happens between 3 and 30 times an image processing call is made. That's why I think that there's a fragmentation issue happening or the like. I realize I was a bit harsh before, but his answer is still wrong. The .NET gospel seems to be to just let the GC work, but in this case, it doesn't. I've been in previous discussions (http://stackoverflow.com/questions/2714811/is-there-a-common-practice-how-to-make-freeing-memory-for-garbage-collector-easie/2714841#2714841) where people don't read all the caveats.
mmr
@Kevin-- I've removed my downvote. Those are useful links, reading them now.
mmr
@mmr sorry about that, I posted while I kept researching the problem without thinking it through.
Kevin
@mmr: Does GC.Collect cause a crash or do you crash from running out of memory?
Steven Sudit
You can also use your own pooling strategy so that the lack of compaction isn't a fatal problem.
Mark Simpson
@Steven Sudit-- crash from running out of memory. I'm currently hesitant to say that this is the right answer, although I'm sadly beginning to think that it is. If so, I will have to pursue a very... interesting strategy for memory management. No two images are the same size, so @Mark Simpson's strategy will be probably the right one, but a non-trivial implementation.
mmr
@mmr: It's not crazy to use a single array and ignore the parts that "hang off" to either side. Having said that, I suspect that the right answer is to figure out why it's apparently leaking. If you take a look at http://connect.microsoft.com/VisualStudio/feedback/details/521147/large-object-heap-fragmentation-causes-outofmemoryexception, it says that .NET 4.0's runtime fixes the LHO issue, so that's another option for you.
Steven Sudit
The first thing you should do is bust out the CLR profiler and check the state of your LOH. I'd bet the farm that it's got huge holes in it.
Mark Simpson
@Mark Simpson-- I'd bet that that's true. The question is, is there a way to force the LOH to compress? Because, from all these links, it doesn't look like it.
mmr
I don't think there is any way to compact it if you're using 3.5 or earlier.
Mark Simpson
I can't find any way of doing so, and I remember a presentation I went to a while back where they had a similar problem. He had to result to using the same large object over again. Clearing out the data and re-populating it.
Kevin
+18  A: 

Start by narrowing down where the problem lies. If you have a native memory leak, poking the GC is not going to do anything for you.

Run up perfmon and look at the .NET heap size and Private Bytes counters. If the heap size remains fairly constant but private bytes is growing then you've got a native code issue and you'll need to break out the C++ tools to debug it.

Assuming the problem is with the .NET heap you should run a profiler against the code like Redgate's Ant profiler or JetBrain's DotTrace. This will tell you which objects are taking up the space and not being collected quickly. You can also use WinDbg with SOS for this but it's a fiddly interface (powerful though).

Once you've found the offending items it should be more obvious how to deal with them. Some of the sort of things that cause problems are static fields referencing objects, event handlers not being unregistered, objects living long enough to get into Gen2 but then dying shortly after, etc etc. Without a profile of the memory heap you won't be able to pinpoint the answer.

Whatever you do though, "liberally sprinkling" GC.Collect calls is almost always the wrong way to try and solve the problem.

There is an outside chance that switching to the server version of the GC would improve things (just a property in the config file) - the default workstation version is geared towards keeping a UI responsive so will effectively give up with large, long running colections.

Paolo
+1 -- ""liberally sprinkling" `GC.Collect()` calls is almost always the wrong way to try and solve the problem."
Nate Bross
+1 for Ant. I've made good use of it.
Steven Sudit
+1 Excellent answer. Perfmon should be the start for finding if managed/unmanaged is the problem child.
Chris O
@Nate Bross, @Paolo-- see my answer to @dthorpe below. It turns out that because of the way .NET handles 16 bit images (it doesn't), I need to use GC.Collect explicitly there. That's what lead me to the GC.Collect 'liberal sprinkling'. It was just a hope that it would solve the larger problem.
mmr
+3  A: 

Use Process Explorer (from Sysinternals) to see what the Large Object Heap for your application is. Your best bet is going to be making your arrays smaller but having more of them. If you can avoid allocating your objects on the LOH then you won't get the OutOfMemoryExceptions and you won't have to call GC.Collect manually either.

The LOH doesn't get compacted and only allocates new objects at the end of it, meaning that you can run out of space quite quickly.

Matthew Steeples
+1  A: 

Have you tested for memory leaks? I've been using .NET Memory Profiler with quite a bit of success on a project that had a number of very subtle and annoyingly persistent (pun intended) memory leaks.

Just as a sanity check, ensure that you're calling Dispose on any objects that implement IDisposable.

Bob Kaufman
+1  A: 

You could implement your own array class which breaks the memory into non-contiguious blocks. Say, have a 64 by 64 array of [64,64] ushort arrays which are allocated and deallocated seperately. Then just map to the right one. Location 66,66 would be at location [2,2] in the array at [1,1].

Then, you should be able to dodge the Large Object Heap.

quillbreaker
ahh, block-copy/bit blitting... so fun :)
Matthew Whited
I've done things like this, but never for something which would see a lot of use, so I never got into all of the fun optimizations.
quillbreaker
+3  A: 

If you're allocating a large amount of memory in an unmanaged library (i.e. memory that the GC isn't aware of), then you can make the GC aware of it with the GC.AddMemoryPressure method.

Of course this depends somewhat on what the unmanaged code is doing. You haven't specifically stated that it's allocating memory, but I get the impression that it is. If so, then this is exactly what that method was designed for. Then again, if the unmanaged library is allocating a lot of memory then it's also possible that it's fragmenting the memory, which is completely beyond the GC's control even with AddMemoryPressure. Hopefully that's not the case; if it is, you'll probably have to refactor the library or change the way in which it's used.

P.S. Don't forget to call GC.RemoveMemoryPressure when you finally free the unmanaged memory.

(P.P.S. Some of the other answers are probably right, this is a lot more likely to simply be a memory leak in your code; especially if it's image processing, I'd wager that you're not correctly disposing of your IDIsposable instances. But just in case those answers don't lead you anywhere, this is another route you could take.)

Aaronaught
@Argonaught-- thanks for the links, looking at them now.Unfortunately, I cannot use the .NET native image libraries, because they, by design, do not allow for the use of 16 bit images, and that's pretty much all that's used in medical imaging. I'm forced to use outside libraries to load images into memory. While it would be nice to look at IDisposable, the fact that I'm using WPF and the fact that I can't use the Image class except to load the 16 bit image compressed to 8 bits for viewing means that the problem cannot be there.
mmr
I should also add, the native libraries do not return any of the memory that they allocate; they create and then destroy the memory, typically as large std::vector instances.
mmr
@mmr: That's fine, in fact it's especially important if the unmanaged code is creating massive `std::vector` instances. You `AddMemoryPressure` when the `std::vector` instances are (expected to be) created, and `RemoveMemoryPressure` when they are (expected to be) destroyed. Although, again, this will only get you so far if the unmanaged library is actually causing fragmentation; the net result is just that the GC will collect sooner ("more aggressively", as you put it).
Aaronaught
@Argonaught-- Currently, when I call a routine, the memory is allocated and then deallocated before the routine returns. The library is purely procedural, no objects are exposed to the outside nor is any memory persisted from one call to the next. Would this approach still help? Those man pages don't have that kind of detail. Is that vector allocation still on the .NET heap? I suppose it would have to be...
mmr
@mmr: Maybe it won't help, then. Unmanaged memory will get allocated on the unmanaged heap; managed memory gets allocated on the managed heap. The memory pressure methods are mainly for memory that persists in some way. If the unmanaged methods never hold on to any memory for longer than a single method call, then in all likelihood you have a leak somewhere, or you're experiencing the specific bug in @Kevin's link.
Aaronaught
A: 

The problem is most likely due to the number of these large objects you have in memory. Fragmentation would be a more likely issue if they are variable sizes (while it could still be an issue.) You stated in the comments that you are storing an undo stack in memory for the image files. If you move this to Disk you would save yourself tons of application memory space.

Also moving the undo to disk should not cause too much of a negative impact on performance because it's not something you will be using all of the time. (If it does become a bottle neck you can always create a hybrid disk/memory cache system.)

Extended...

If you are truly concerned about the possible impact of performance caused by storing undo data on the file system, you may consider that the virtual memory system has a good chance of paging this data to your virtual page file anyway. If you create your own page file/swap space for these undo files, you will have the advantage of being able to control when and where the disk I/O is called. Don't forget, even though we all wish our computers had infinite resources they are very limited.

1.5GB (useable application memory space) / 32MB (large memory request size) ~= 46

Matthew Whited
@Matthew Whited-- it actually is something we use all the time. This application is use to test different processing algorithms; I'll often run an algorithm, undo it, rerun it with different parameters, etc, just to see different results.
mmr
You will probably be limited to less than 50 of these `ushort` arrays and their related undo stacks. You could look at a hybrid memory/disk paging system as I have suggested. It should be fairly easily to implement using .Net but sorry I don't have time to draft up an example.
Matthew Whited
+2  A: 

Just an aside: The .NET garbage collector performs a "quick" GC when a function returns to its caller. This will dispose the local vars declared in the function.

If you structure your code such that you have one large function that allocates large blocks over and over in a loop, assigning each new block to the same local var, the GC may not kick in to reclaim the unreferenced blocks for some time.

If on the other hand, you structure your code such that you have an outer function with a loop that calls an inner function, and the memory is allocated and assigned to a local var in that inner function, the GC should kick in immediately when the inner function returns to the caller and reclaim the large memory block that was just allocated, because it's a local var in a function that is returning.

Avoid the tempation to mess with GC.Collect explicitly.

dthorpe
@dthorpe, I've definitely hit your first scenario. It works like this: 1) .NET will not handle 16 bit images natively. I have to use another library, but it will not display properly. 2) To display these images, I have to allocate an 8 bit image with the same number of elements with the dynamic range set per a user moving a box around on the screen. The DR is then set by the min/max of that box and a lookup table. That 8 bit image is allocated once per image. 3) Moving the box around on the image causes a crash after a second. For this, I must call GC.Collect explicitly; that solved it.
mmr
Why are you using an 8 bit image? 8 bits per pixel, or 8 bits per color channel? 8 bits per pixel will force/require use of a color palette which is tedious and time consuming and will allocate additional memory (for the color table) behind the scenes.
dthorpe
@dthorpe-- medical images are black and white. All color medical images are false colored (with the exception of things like optometry or dermatology) because the signals that are detected are beyond visible range. As such, I only show black and white. The remapping to 8 bit space from 16 requires that I have a byte array of the same dimensions as the ushort, and that that byte array get refilled with a native routine every time the user changes the mapping.
mmr
@mmr: Ah, 16 bits per color channel, 1 color channel. Got it.
dthorpe