.NETGURU
NOT ANSWERED: Is there a Memory Leak in the Bitmap object ?
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-drawing' list.


Michael Lang
I seem to have a memory problem with the bitmap object. It is illustrated
in the code below. The problem seems to be some sort of memory leak that
eventually causes the aspnet_wp.exe to kill itself with the error:-

aspnet_wp.exe (PID: 2716) was recycled because memory consumption exceeded
the 76 MB (60 percent of available RAM).

Which loses all state data kept on this machine (nasty), this occurs rather
quickly on my test machine as it only has 128 megs of RAM.

This problem is a little difficult to track as:-

1. It does not occur all the time. To repeat the problem I have to rebuild
and start the application at least 3 or 4 times. I find after aspnet_wp has
rebooted it definitely works fine, to get it to fail again you need to go
into Visual Studio and recompile the project.
2. Calls to GC.GetTotalMemory(false); does not return any startling numbers
I have only been able to observe the problem in progress by monitoring the
memory useage and VM Size of the aspnet_wp.exe process in the windows task
manager.
3. In the example below when stepping through in the debugger when the
bitmap is constructed from the stream there seems to be a disproportionate
increase in aspnet_wp.exe's memory usage ie if the bitmap is 100K the memory
usage of aspnet_wp.exe in the task manager jumps by 1 or 2 megabytes.

Can anyone come up with an explanation for any of this behaviour I'm
observing ?
Could someone please tell me if they can repeat this and observe the memory
size of aspnet_wp.exe steadily increasing with the following code, if you
could not repeat the problem that would also be useful feedback:-

webform1.aspx:-

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5";
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
 
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 247px; POSITION:
absolute; TOP: 88px" runat="server" Height="35px" Width="165px"
Text="Button"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 253px; POSITION:
absolute; TOP: 51px" runat="server" Width="264px" Height="19px">Click this
button lots</asp:Label>
</form>
</body>
</HTML>

Webform1.aspx.cs:-

using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.IO;

namespace WebApplication1
{
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button Button1;
        const string filePath = Physical Path to an image file here, use an image
> 100k
        protected System.Web.UI.WebControls.Label Label1;
        byte[] _myImage = null;
        private void Page_Load(object sender, System.EventArgs e)
        {
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            try
            {
                _myImage = (byte[])Cache["bitmapData"];
                if (_myImage == null)
                {
                    FileStream oFileStream;
                    oFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read,
FileShare.Read);
                    _myImage = new Byte[oFileStream.Length];
                    oFileStream.Read(_myImage, 0, (int)oFileStream.Length);
                    oFileStream.Close();

                    Cache.Add("bitmapData", _myImage, null,
System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20),
System.Web.Caching.CacheItemPriority.Normal, null);
                }
            }
            catch
            {
            }

            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button1_Click(object sender, System.EventArgs e)
        {
            System.Drawing.Image testBitmap = null;
            System.Drawing.Image testBitmap2 = null;
            System.Drawing.Image testBitmap3 = null;
            System.Drawing.Image testBitmap4 = null;
            System.Drawing.Image testBitmap5 = null;
            System.Drawing.Image testBitmap6 = null;
            System.Drawing.Image testBitmap7 = null;
            System.Drawing.Image testBitmap8 = null;
            try
            {
                MemoryStream aStream = new MemoryStream(_myImage);
                testBitmap = System.Drawing.Image.FromStream(aStream);
                testBitmap2 = System.Drawing.Image.FromStream(aStream);
                testBitmap3 = System.Drawing.Image.FromStream(aStream);
                testBitmap4 = System.Drawing.Image.FromStream(aStream);
                testBitmap5 = System.Drawing.Image.FromStream(aStream);
                testBitmap6 = System.Drawing.Image.FromStream(aStream);
                testBitmap7 = System.Drawing.Image.FromStream(aStream);
                testBitmap8 = System.Drawing.Image.FromStream(aStream);
            }
            finally
            {
                if (testBitmap != null) testBitmap.Dispose();
                if (testBitmap2 != null) testBitmap2.Dispose();
                if (testBitmap3 != null) testBitmap3.Dispose();
                if (testBitmap4 != null) testBitmap4.Dispose();
                if (testBitmap5 != null) testBitmap5.Dispose();
                if (testBitmap6 != null) testBitmap6.Dispose();
                if (testBitmap7 != null) testBitmap7.Dispose();
                if (testBitmap8 != null) testBitmap8.Dispose();
            }

            GC.Collect();
        }
    }
}

Reply to this message...
 
    
Minh Truong
Michael,

All that I can think of is that an image's size on disk could be vastly
different than its size in memory. A 100K JPEG can easily expand to
multi-megabytes as an Image. Ten separate Image's can fill up that 76 MB
with multiple clicks. Is there a way you can view memory usage after each
click? Mayby PerfMon can help?

----- Original Message -----
From: "Michael Lang" <Click here to reveal e-mail address>
To: "ngfx-drawing" <Click here to reveal e-mail address>
Sent: Tuesday, January 15, 2002 8:04 AM
Subject: [ngfx-drawing] NOT ANSWERED: Is there a Memory Leak in the Bitmap
object ?

[Original message clipped]

Reply to this message...
 
    
Michael Lang
That would make some sense if the Bitmap's inards were expanding a JPEG
however I have seen a 300K BMP file cause the aspnet_wp.exe to expand by
over 700K when the Bitmap object is constructed.

[Original message clipped]

Reply to this message...
 
    
Minh Truong
8-bit BMPs can be run-length-encoded, which provides pretty good compression
for clean images. But that may not be your situation.

----- Original Message -----
From: "Michael Lang" <Click here to reveal e-mail address>
To: "ngfx-drawing" <Click here to reveal e-mail address>
Sent: Friday, March 15, 2002 10:57 PM
Subject: [ngfx-drawing] Re: NOT ANSWERED: Is there a Memory Leak in the
Bitmap object ?

[Original message clipped]

Reply to this message...
 
 
System.Byte
System.Drawing.Image
System.EventArgs
System.EventHandler
System.GC
System.IO.FileAccess
System.IO.FileMode
System.IO.FileShare
System.IO.FileStream
System.IO.MemoryStream
System.TimeSpan
System.Web.Caching.Cache
System.Web.Caching.CacheItemPriority
System.Web.UI.Page
System.Web.UI.WebControls.Button
System.Web.UI.WebControls.Label




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification