LEADTOOLS Support
General
General Questions
Memory leak when using RasterImageViewer in ItemsControl
This topic and its replies were posted before the current version of LEADTOOLS was released and may no longer be applicable.
#1
Posted
:
Wednesday, July 22, 2009 4:47:41 AM(UTC)
Groups: Registered
Posts: 4
I have attached a small WPF app that reproduces this problem. It binds a List to an ItemsControl and displays each image via a DataTemplate that contains a RasterImageViewer.
After several clicks of the Reset button, it will throw an OutOfMemoryException. Also, watching the private bytes in task manager shows the application consuming approximately 250MB per button click.
I have downloaded the trial version of lead tools Imaging Pro v16.5
#2
Posted
:
Wednesday, July 22, 2009 4:52:32 AM(UTC)
Groups: Registered
Posts: 4
I left out the Images.Clear() in Button_Click(), but it will still throw the error when it is present. You have to click the reset button fairly rapidly to get to occurr.
#3
Posted
:
Wednesday, July 22, 2009 6:53:42 AM(UTC)
Groups: Guests
Posts: 3,022
Was thanked: 2 time(s) in 2 post(s)
I changed the Button_Click code to this, and no more out of memory exceptions were raised:
for (int i = 0; i < 10; i++)
Images.Add(codecs.Load("..\\..\\barcode1.tif"));
for (int i = Images.Count - 1; i >= 0; --i)
{
Images[i].Dispose();
Images[i] = null;
}
Images.Clear();
#4
Posted
:
Wednesday, July 22, 2009 8:43:03 AM(UTC)
Groups: Registered
Posts: 4
The click should always end with images being displayed. It is meant to simulate switching between some entity that displays different images for every click.
The code you have provided will not display any images after execution.
#5
Posted
:
Thursday, July 23, 2009 1:06:03 AM(UTC)
Groups: Guests
Posts: 3,022
Was thanked: 2 time(s) in 2 post(s)
The purpose of my added code was to free all images so that you don't get a leak.
If you want to keep one or more images to display them, do not call Images[i].Dispose() or Images[i] = null for these images, but you should add that code SOMEWHERE if you don't want to get a memory leak.
In other words, do not keep a RasterImage object floating by clearing it from the collection without disposing it first.
#6
Posted
:
Thursday, July 23, 2009 4:25:56 AM(UTC)
Groups: Registered
Posts: 4
Ahh. I see your intention now. I changed the code to dispose the existing images first before displaying new ones. That did prevent an out of memory exception from occurring, but the process memory still fluctuated between 250MB and 1GB.
I then changed the code to do 20 images:
for (int i = 0; i < Images.Count; i++)
{
Images[i].Dispose();
Images[i] = null;
}
Images.Clear();
for (int i = 0; i < 20; i++)
Images.Add(codecs.Load("..\\..\\barcode1.tif"));
This did cause the out of memory exception to occurr. The image is only a 20kb file. I'm just not understanding why the RasterImages cause such a high memory footprint. The application we will be developing could easily display more than 20images at a time ( as thumbnails).
#7
Posted
:
Thursday, July 23, 2009 6:39:20 AM(UTC)
Groups: Guests
Posts: 3,022
Was thanked: 2 time(s) in 2 post(s)
20kb is the size on disk. The actual image size on memory could different when it's loaded if the disk file is compressed.
Also, .NET doesn't always free the memory when you dispose of an object. If you want to force memory freeing, you can use the following code:
GC.Collect(); //force garbage collection
GC.WaitForPendingFinalizers();
LEADTOOLS Support
General
General Questions
Memory leak when using RasterImageViewer in ItemsControl
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.