Prevent or Predict Out of memory exception

Go To StackoverFlow.com

0

I'm looking to move forward with a strategy that will detect when the system is about to run out of memory and restart the system using Application.Restart();

The WebBorwser control is causing me take this route because even after disposing a webbrowser control attached to a form the memory usage does not decrease and everywhere I've read says this is a IE issue and we are at their mercy. (Although I do notice if I open IE, run the same websites, the memory increases but properly reduces when I close the tab. ...)

I'm not looking to move to webkit because watin is not compatible and I'm not looking to to use GeckoFX because it does not support javascript or jquery support and I'm not looking to use external browser instances because embedded browser controls are part of my interface design.

So now what I want to do is create a method that dynamically checks available memory against total memory and performs a restart when the situation gets critical to properly dispose of resources the GC cannot reach and I cannot programatically figure out how to destroy.

I believe I can do this and restart my automation from where I left off and cycle this daemon on and on.

Can anyone help me figure out how to code the memory check? It should be easy enough. I found this thread but I'm having some trouble implementing it without running into errors... maybe another approach will be better? How do you get total amount of RAM the computer has?

Thanks guys! I hope this helps,

2012-04-05 22:04
by atwellpub
OOM is about address space, not physical RAM - CodesInChaos 2012-04-05 22:06
Reminds me of "select is broken - Austin Salonen 2012-04-05 22:11
So are you actually using a profiler or just doing arm-chair memory analysis, like using the Task Manager - Bryan Crosby 2012-04-05 22:11
@ColdelnChaos Is OOM private working set memory? When the private working set memory climbs past 1gb pretty soon my sqlite cn.Open(); function throws an out of memory error. Navigating to new pages and loading images on pages drives up the private working set memory and disposing the webbrowser control never releases the memory. Disposing my SqlCeConnections (using Using) does not decrease the PWSM either.. - atwellpub 2012-04-05 22:16
I don't believe Microsoft Visual C# 2010 Expresss offers me a memory profiler because it doesn't accept plugins. Yes, Task Manage seems to show numbers that match the problem for me to have a little understanding of what's going on - atwellpub 2012-04-05 22:17
My advice would be to first learn about how memory actually works before you try to write a complicated program that makes decisions on the basis of memory management statistics. The fact that you are confusing RAM, address space and private working set indicates to me that you do not understand the differences at a deep enough level to write this code correctly. Memory in modern operating systems is extremely complicated and you need to understand it thoroughly and deeply if this is the task you've set yourself - Eric Lippert 2012-04-05 22:21
To answer your question: No, out-of-memory does not necessarily happen when private working set gets high. It is possible to have a very high private working set but still have lots of virtual memory, and it is possible to have a low private working set and no virtual memory. Private memory is memory that is not shared with other processes, and working set is memory that is kept local for performance reasons. Private working set is the amount of memory that is both private and in the working set - Eric Lippert 2012-04-05 22:23
That's why I'm here. You are great teachers and I love you. I'm humbled and schooled. Let's assume that I'm somewhere in the right realm of understanding (but not quite there yet!), using yonder lore, is there a way to detect an impending memory crash by comparing available metrics? Can this be created smartly in a method that I can seed throughout my automation to detect and circumvent crashes by running a restart - atwellpub 2012-04-05 22:25


1

This doesn't make a lot of sense. OOM occurs because the CLR memory manager could not allocate the desired chuck of memory. This may be due to a number of reasons. Looking at available RAM (which is completely irrelevant) or even available GC memory before all allocations will not guarantee the absence of OOMs. E.g. if you have LOH fragmentation the total number of available bytes may exceed the need for the allocation, but if the CLR cannot allocate a single, contiguous chunk of memory from the free-list the request will still fail.

2012-04-05 22:18
by Brian Rasmussen


0

Asp.Net will evict stuff from the httpcache when it notices that it goes low on available memory, you could take a trip into that part of the framework through Reflector and learn from how it does the job there...

2012-04-05 22:09
by AndreasKnudsen
@AdamMaras: You have missed the point. Andreas's point was that ASP somehow figures out that memory is low, and therefore you could reverse-engineer ASP to figure out how it works. That is not how I personally would recommend that anyone proceed, mind you - Eric Lippert 2012-04-05 22:27
Couldn't you just programatically set the webbrowser control instance to prevent caching? (I'm assuming httpcache and image/page caching during a browser session is the same thing) ... reading up on this IE issue a lot of people assume the memory creep is due to caching. But like me, there are more idiots then people really in the know trying to address the webbrowser memory leak issue... and without a memory profiler, you guys are going to have a hard time believing that my issue is related to the webbrowser control for certain.. - atwellpub 2012-04-05 22:35
@EricLippert I don't know how ASP does this, but I suspect that you can only successfully do this if you're hosting the CLR - Brian Rasmussen 2012-04-05 22:52
Ads