# Why I used the MappedImage object would lead to memory leak using C# programming?

**URL:** <https://forum.commonvisionblox.com/t/why-i-used-the-mappedimage-object-would-lead-to-memory-leak-using-c-programming/617>\
**Category:** Programming Questions\
**Tags:** csharp\
**Created:** [December 14, 2018, 1:20am UTC](https://forum.commonvisionblox.com/t/why-i-used-the-mappedimage-object-would-lead-to-memory-leak-using-c-programming/617 "2018-12-14T01:20:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JieZhao](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@JieZhao](https://forum.commonvisionblox.com/u/JieZhao)\
**Post date:** [December 14, 2018, 1:20am UTC](https://forum.commonvisionblox.com/t/why-i-used-the-mappedimage-object-would-lead-to-memory-leak-using-c-programming/617/1 "2018-12-14T01:20:46Z")

</div>

Dear all,  
I have a question on MappedImage object in C# language.  
My aim is crop the rect of source image.  
the function of MappedImage is effective, but the pc memory keeps increasing.  
Would you tell me why the pc memory is increasing?  
Thanks every all.

My demo is below:

```auto
    using (Stemmer.Cvb.Image img = Stemmer.Cvb.Image.FromFile("xxx.bmp"))
    {
        Point2D pt = new Point2D(0, 0); 
        Size2D imgSize = img.Size;
        Rect re = new Rect(pt, imgSize);
        using (MappedImage mapImg = img.Map(re) )
        {
            pt = Point2D.Empty;
            re = Rect.Empty;
            img.Dispose();
            mapImg.Dispose();
        }
    }
    System.GC.Collect();
    GC.WaitForPendingFinalizers();

```

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [December 14, 2018, 8:58am UTC](https://forum.commonvisionblox.com/t/why-i-used-the-mappedimage-object-would-lead-to-memory-leak-using-c-programming/617/2 "2018-12-14T08:58:36Z")

</div>

Hi @JieZhao

I couldn’t reproduce your result with the _Stemmer.Cvb.dll_ version 1.30. I used Visual Studio’s internal reporting and Microsoft’s [Process Explorer](https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer). I used a slightly different test (I explain why below):

```cs
static void Main(string[] args)
{
  Console.WriteLine("Hit key to start...");
  Console.ReadKey();
  Console.WriteLine("running...");

  var imagePath = Path.Combine(SystemInfo.InstallPath, "Tutorial", "FruitBowl.jpg");
  foreach (var iteration in Enumerable.Range(0, 1000))
  {
    using (var fruitBowl = Image.FromFile(imagePath))
    {
      using (var map = fruitBowl.Map(fruitBowl.Bounds))
      {
        Debug.Assert(map.Size == fruitBowl.Size);
      }
    }
  }

  Console.WriteLine("done...");
  Console.ReadKey();
}

```

Here is the result from the internal reporting:  
 ![image](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/eec84f5e6df3495673672c6a5041d92c7f6fb59b.png)

And _Process Explorer_’s start and end (at the `.ReadKey()` calls):

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/9746de51d862f11a58c4753bd1bb185aaec5d8c2.png) ![image](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/b176f4797f27787255732b2aeb49801e6f906735.png)

I changed your test a bit as you have double-dispose in it. The `using`-block also calls dispose. The `GC.Collect` also has no real impact as you disposed the images which frees the native image buffer memory.

The interesting section is _Virtual Memory_ and there _Private Bytes_/_Peak Private Bytes_. The increase from the first snap shot by ~1MB is due to loaded modules (DLLs). The peak is ~4MB larger which is roughly the image memory. The memory usage by the `MappedImage` is insignificant as its only a view on the original’s image’s. The effects are the same for _.bmp_ files – I used fruit bowl as it has a larger and thus more visible memory footprint.
