Click here to Skip to main content
15,671,232 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi all,

I'm developing a information kiosk which at certain page it will show list of required data and image(retrieve from mysql and convert the bytes for display) around 20 images which original size is dynamic resolution. Some image width is 1200px, some 800px, some 2000px.

Now, I've been use all three image,border and rectangle as a test to see which one is better ram consume. Because my ram sometimes jump high if I show for data A and going back to selection page and repeating process to show data A back again. It will increase until max ram and program become hang and crash. Even I just do the process once, the memory is very slow to reduce back.

Specification:
1) CPU: i5
2) RAM: 4GB
3) OS: Win8 64Bit
4) Display: 1080x1920 (Vertical Orientation)
5) Tools: VS2010 WPF VB.NET

I don't know how to solve this problem since customer always asking to overcome memory is full and make the program hang at certain time(which mean when continuously displaying image file)

Question: Is there any best way to overcome memory leaks on image

Below is what I do to convert bytes and display image:-
BORDER
VB
view_imgbyte= arr_info.Item(0).arrImage
If Not (IsNothing(view_imgbyte)) Then
    Using stream = New MemoryStream(view_imgbyte)
        Dim bitmap = New BitmapImage()
        bitmap.BeginInit()
        bitmap.StreamSource = stream
        bitmap.CacheOption = BitmapCacheOption.OnLoad
        bitmap.EndInit()
        bitmap.Freeze()
        stream.Dispose()
        img_file.ImageSource = bitmap
    End Using
Else
    img_file.ImageSource = Nothing
End If


IMAGE
VB
view_imgbyte= arr_info.Item(0).arrImage
If Not (IsNothing(view_imgbyte)) Then
    Using stream = New MemoryStream(view_imgbyte)
        Dim bi As New BitmapImage()
        bi.BeginInit()
        bi.StreamSource = stream
        bi.CacheOption = BitmapCacheOption.OnLoad
        bi.EndInit()
        bi.Freeze()
        stream.Dispose()
        Dim src As ImageSource = bi
        img_file.Source = src
        img_file.Width = 800
    End Using
Else
    img_file.ImageSource = Nothing
End If


RECTANGLE
VB
view_imgbyte= arr_info.Item(0).arrImage
If Not (IsNothing(imgbyte)) Then
    Using stream = New MemoryStream(imgbyte)
        Dim bitmap = New BitmapImage()
        bitmap.BeginInit()
        bitmap.StreamSource = stream
        bitmap.CacheOption = BitmapCacheOption.OnLoad
        bitmap.EndInit()
        bitmap.Freeze()
        imgBrush.ImageSource = bitmap
        stream.Dispose()
        img_file.Fill = imgBrush
        img_file.Stretch = Stretch.Fill
    End Using
End If


Appreciate any help. Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 31-Jan-16 14:29pm    
What you describe has nothing to do with memory leak. You just consume too much memory.
—SA
Luiey Ichigo 31-Jan-16 14:49pm    
Hi SA,

Because my system is an interactive kiosk. So the customer placed the design so much on image to be display at "PageList.xaml" the problem on display the converted image is one thing. Another thing is when query all possible list BLOB is also make the the system freeze for 2-4 seconds
Sergey Alexandrovich Kryukov 31-Jan-16 15:21pm    
I just say it has nothing like a memory leak...
—SA

1 solution

First, you don't have a memory leak problem. At least not yet.

You're probably using Task Manager to get find out how much memory your app is using. WRONG! Task Manager is telling you how much your .NET CLR app has RESERVED for it, not how much it's actually using. Learn how the .NET CLR memory manager works. Google for ".NET CLR memory management". (This is why you think the memory is being freed up slowly.)

If you've got nothing else, use the PerfMon and the .NET Memory counters to see how much your app is actually using.

Next, as for performance, you need to look at the size of your images, their bit depth and the format of the images. BMP is a 32bpp format that uses no compression techniques to reduce the byte count of the image. When you create the images in your database you can resize them to the maximum size your app will ever use and you can convert them to another format, like PNG, which greatly reduces the byte count for an image.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jan-16 15:22pm    
Sure, a 5.
—SA
Luiey Ichigo 1-Feb-16 9:00am    
Hi Dave,

Thanks for the explanation. Yes so far I'm observing using Task Manager to see how the RAM is increase and reduce over time. I'll take a look on that .NET CLR.

Regarding the image size, my client is selecting JPEG/JPG file format for image and PNG for logo. Is PNG is better than JPEG? I've seen someone in a blog said memory is using for displaying image by formula 1 pixel * 4bytes. So which type of image that works well to reduce the byte other than BMP?
Dave Kreskowiak 1-Feb-16 9:31am    
In memory and for display, you cannot reduce the size of the image. Everything is in a 4 byte per pixel representation.

In a database and on disk, you get the benefits of compression, and a shorter time to load that data into memory from a database or file.

I prefer PNG over JPG because of less compression artifacts. Other than that, you get about the same benefit in size reduction.
Luiey Ichigo 1-Feb-16 13:45pm    
Hi Dave,

Thanks for explanation. Luckily from start, my program only accept JPEG/JPG/PNG only. Lol I'm a bit relief on that.

Thank you sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900