Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possibly memory leak OR?

Would appreciate any kind of help here.

A brief description about scenario -

There is a COM+ running on server (written in C#). The task of this COM is to take a file name, page number of a multi-paged tiff file and the resolution to convert it to a gif file image. This COM is called from a web application using a proxy. The web site gets the converted image and displays in the requested resolution. For printing - it makes 2 request - 1st for display resolution, 2nd in full resolution (which gets printed using window.print()).

Problem -

After sometime server goes out of memory and images are not getting displayed on web site. Server needs to be restarted periodically.

Error

EventType clr20r3, P1 imageCOM.exe, P2 1.0.0.0, P3 4fd65854, P4 prod.web.imaging, P5 1.0.0.0, P6 4fd65853, P7 1a, P8 21, P9 system.outofmemoryexception, P10 NIL.

Here is the error(s) on the web server (these continuously appear every minute) ….

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---

I do not have access to production server, but error sent by sysadmin states OutOfMemory.

Thus, assuming memory leak and focusing on it - my findings so far with limited experience of handling this kind of situation -

  • Perfmon - I see that Process/Private Bytes are increasing and so is .Net CLR memory/# of bytes in Heap. Thus, I assume it's Managed memory leak. I am not sure though.
  • CPU Usage - started with 8% and went up to 80% only at beginning. It dropped back and stayed between 3% - 12%, except couple of times when it went back to 75%-85%. Not sure what is going on here.
  • I started to debug server COM to have a look at heap, gcroot etc.

    1. There are 2 objects for count is increasing in the heap with every request made. 1 object is that hold the image data. 2nd object is the event handler to remove the Object 1 (image) from cache when it expires after a certain time.
    2. Looking at the method call - both objects lead to the same method.
    3. Code Implementation wise - Requested images are getting cached (up to a certain number) - I can understand it is for better performance. Probably, this is why reference to objects are increasing in heap as mentioned in bullet 1.

I know I gave very vague description, but I need some kind of lead to detect the real issue on server.

EDIT: Image object has been disposed like

Bitmap retVal;

      using (MemoryStream buffer = new MemoryStream(doc.FileData, 0, doc.DocumentSize, false, false))
      {
        using (Bitmap original = new Bitmap(buffer))
        {
        //select the page to convert and
        //perform scaling - full resolution or the requested resolution.  
        }
      }

      using (MemoryStream buffer = new MemoryStream())
      {
        retVal.Save(buffer, ImageFormat.Gif);
        retVal.Dispose();
        return buffer.GetBuffer();
      }
like image 944
peacefulmember Avatar asked Aug 23 '12 15:08

peacefulmember


People also ask

What is a possible memory leak?

Despite having adequate RAM and not running resource-intensive software, there can be another situation where all available RAM gets used and performance degrades. This is known as a memory leak, and it happens when software fails to manage the available RAM correctly.

How do you identify a memory leak?

Running out of memory is the simplest way to identify a memory leak, and it's also the most common approach to uncovering one. That's also the most inconvenient way to find a leak. You'll probably notice your system slowing down before you run out of RAM and crash your application.

Is memory leak serious?

Very dangerous. Memory leaks in the kernel level lead to serious system stability issues. Kernel memory is very limited compared to user land memory and should be handled cautiously. Memory is allocated but never freed.

Can humans have memory leakage?

The brain is highly segregated. Multiple mechanisms ensure that different types of memories are processed independently. Nonetheless, information leaks out across these memory systems. Only recently has the diversity of these leaks been revealed.


1 Answers

Make sure you are disposing the Image objects (such as Bitmap) after you're done using them. I'm guessing you're opening the tiff image as a bitmap and rescaling it and then saving it again as a gif. If you don't dispose the Bitmap it will leak memory (usually several MB each time depending on the size of the image).

like image 106
Paccc Avatar answered Oct 04 '22 00:10

Paccc