How to troubleshoot an out of memory exception?

Howdy, I have (what I think) is a fairly basic program processing HTTP requests. I’ve suddenly started getting an OutOfMemoryException being thrown when processing my HTTP requests.

Can someone give me some pointers on figuring this out? I’ve never dealt with anything like this before.

I notice that in my debug window, I keep seeing a line like this:

each time the “bytes used” increases, but never goes down. I am basing my program off of the example provided by Internet of Things book under the " Network-Controlled Screamer" section (page 47, http://www.ghielectronics.com/downloads/FEZ/FEZ_Internet_of_Things_Book.pdf).

I’m a little clueless on how to troubleshoot something like this.

The example in The Internet of Things book, looks to have an error that was causing my problem. By putting in the “context.Close();” call, I’m no longer getting the memory errors. But, in the future, are there any general tips for solving memory issues? Also, is this actually an error? Does the context have to be closed each time?


private static void processRequestResponse(HttpListener listener)
        {
            while (true)
            {
                HttpListenerContext context = null;
                try
                {
                    context = listener.GetContext();

                    if (context.Request.HttpMethod == "POST" || context.Request.HttpMethod == "GET")
                    {
                        ParseRequest(context);
                        context.Response.Close();
                    }
                    else
                    {
                        context.Response.StatusCode = (int)HttpStatusCode.NotImplemented;
                        context.Response.Close();
                    }
                    context.Close();       //<=== ADDING THIS LINE FIXED THINGS
                    
                }
                catch(Exception e)
                {
                    Debug.Print(e.ToString());
                    if (context != null)
                    {
                        context.Close();
                    }
                }
            }
        }

Sounds like you’re using up all your memory before the GC has a chance to clean it up. You might consider refactoring to use static buffers instead of newing up new memory.

Bear in mind that “example code” is exactly that - it’s there only to illustrate basic concepts related to the class you’re using. It will very likely have bugs or it may not exactly satisfy your requirements, but solving that is “an exercise for the student”…

When you call the .Close() method, you’re telling the HTTPListenerContext and HTTPListenerResponse objects that you’re done with the request that they were handling and allows them the opportunity to free up resources that [italic]they[/italic] allocated to that activity.
If you don’t call .Close(), the resources [italic]they[/italic] allocated may never get freed (GC doesn’t run all the time) and you’ll eventually hit an out-of-memory state.

IOW, when dealing with network IO, you should always ".Close()"any [italic]completed[/italic] connections and conversations.

Jimmy,

Thanks for the reply. I’m not a native C# dev., so figuring out all the intricacies has been “fun” :slight_smile: I figured things out by noticing that in the “catch” block, they were closing the context.

I figured it would be automatically closed/GC’ed since each time through the while() loop it was declared null. Am I missing some tricky aspect of .NET?

Setting a variable = null does not necessarily immediately dispose of that object in C#/.NET. The GC may do that for you WHEN it runs. However, the GC only runs when it needs to free up memory not as soon as you release the variable. You should always explicity close and dispose of objects in C#. Study “Dispose” and the “using” command in the C# docs to get a better understanding.