HUGE memory usage while loading images

HUGE memory usage while loading images

Post by Marauder » Wed, 26 Mar 2003 11:07:08


I'm witnessing large consumption of RAM while loading and
unloading very large images (~1.7MB worth of JPEG data)
It's shocking to see your program consume like 200MB of
memory when all it does is view images!

Any suggestions what I should do? Manually called Garbage
collection? Or dispose the object or something like that?

Here's my code that does the loading:-

Private Sub lstFiles_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
lstFiles.SelectedIndexChanged
        If lstFiles.SelectedIndex = -1 Then Exit Sub

        If Not oThread Is Nothing Then
            If oThread.ThreadState =
Threading.ThreadState.Running Then
                'Stop the previous thread first
                oThread.Suspend()
                oThread.Abort()
                oThread = Nothing
            End If
        End If

        ''Create a new thread and ask it to load the
picture
        oThread = New Threading.Thread(AddressOf
LoadImage)
        oThread.Start()
End Sub

    Private Sub LoadImage()
        Dim oBitmap As Bitmap
        Dim oBack As Bitmap
        Try
            oBitmap = New Bitmap(CStr(lblCurrPath.Text &
lstFiles.SelectedItem))
            'First get dimensions constrained to picture
box, GetDimensions is a function that constraints resizing
'to the same aspect ratio.

            Dim oPt As Point
            oPt = GetDimensions(oBitmap.Width,
oBitmap.Height)
            'Create a new image for the picture box

            oBack = New Bitmap(oBitmap, picDisplay.Width,
picDisplay.Height)
            Dim g As Graphics = Graphics.FromImage(oBack)
            g.DrawImage(oBitmap, 0, 0, oPt.X, oPt.Y)
            g.Dispose()
            picDisplay.Image = oBack
            oBitmap = Nothing
            oBack = Nothing
        Catch ex As Threading.ThreadAbortException
            Trace.WriteLine("Loading Interuppted")
        Finally
            oBitmap = Nothing
            oBack = Nothing
        End Try
    End Sub

 
 
 

HUGE memory usage while loading images

Post by Erik Brow » Fri, 28 Mar 2003 10:58:49


Make sure you dispose of your Bitmap objects when you are finished.  Failing
to do so will cause the sort of problem you are describing, since as long as
your program is "busy" the garbage collector will not run and you will burn
through the memory.

That is, you need to call:

    oBitmap.Dispose()
    oBack.Dispose()

rather than just setting these variables to Nothing.

Erik

============
Erik Brown
Author of "Windows Forms Programming with C#"
http://www.amazon.com/exec/obidos/ASIN/1930110286


Quote:> I'm witnessing large consumption of RAM while loading and
> unloading very large images (~1.7MB worth of JPEG data)
> It's shocking to see your program consume like 200MB of
> memory when all it does is view images!

> Any suggestions what I should do? Manually called Garbage
> collection? Or dispose the object or something like that?

> Here's my code that does the loading:-

> Private Sub lstFiles_SelectedIndexChanged(ByVal sender As
> Object, ByVal e As System.EventArgs) Handles
> lstFiles.SelectedIndexChanged
>         If lstFiles.SelectedIndex = -1 Then Exit Sub

>         If Not oThread Is Nothing Then
>             If oThread.ThreadState =
> Threading.ThreadState.Running Then
>                 'Stop the previous thread first
>                 oThread.Suspend()
>                 oThread.Abort()
>                 oThread = Nothing
>             End If
>         End If

>         ''Create a new thread and ask it to load the
> picture
>         oThread = New Threading.Thread(AddressOf
> LoadImage)
>         oThread.Start()
> End Sub

>     Private Sub LoadImage()
>         Dim oBitmap As Bitmap
>         Dim oBack As Bitmap
>         Try
>             oBitmap = New Bitmap(CStr(lblCurrPath.Text &
> lstFiles.SelectedItem))
>             'First get dimensions constrained to picture
> box, GetDimensions is a function that constraints resizing
> 'to the same aspect ratio.

>             Dim oPt As Point
>             oPt = GetDimensions(oBitmap.Width,
> oBitmap.Height)
>             'Create a new image for the picture box

>             oBack = New Bitmap(oBitmap, picDisplay.Width,
> picDisplay.Height)
>             Dim g As Graphics = Graphics.FromImage(oBack)
>             g.DrawImage(oBitmap, 0, 0, oPt.X, oPt.Y)
>             g.Dispose()
>             picDisplay.Image = oBack
>             oBitmap = Nothing
>             oBack = Nothing
>         Catch ex As Threading.ThreadAbortException
>             Trace.WriteLine("Loading Interuppted")
>         Finally
>             oBitmap = Nothing
>             oBack = Nothing
>         End Try
>     End Sub


 
 
 

1. Draw image HUGE memory load ??????

Hi all,

I need some help here with this simple program. It takes a folder and shows
the images on the screen. The images are about 1.5 MB. The problem is that
the program uses a HUGE load of memory. for 3 pictures it takes about 65 MB
which is very weird. I tried to stream the files and the showing them but
without any luck. What is it wrong with this code ? Is there other ways to
get this done without using so much memory ?

Best Regards

Mihai Marinescu


String[] test = new string[len];


Graphics g = e.Graphics;

int x = 0;

int y = 0;

int width = 120;

int height = 120;

int rX =10;

int rY =10;

int rW =120;

int rH =120;

Rectangle destRect1;

GraphicsUnit units = GraphicsUnit.Pixel;

for (int i=0; i<len; i++)

{

Image _image = Image.FromFile(test[i]);

_image = _image.GetThumbnailImage(120,120,null, new IntPtr());

destRect1 = new Rectangle(rX,rY,rW,rH);

e.Graphics.DrawImage(_image, destRect1, x, y, width, height, units);

rX+=125;

2. Roommate wanted for CCCG conference

3. Memory leak when loading Images in thread

4. RAS Windows XP

5. Memory Error when loading an Image

6. tracking stylus movement on ppc screen

7. MSXML, C# and huge memory leak

8. Having Problems with ISAXXMLReader interface

9. aspnet_wp process taking up *huge* amounts of memory

10. Huge Form, Huge Headache (small classic question)

11. .NET executable process high memory usage?

12. Memory Usage

13. XMLfiles, memory usage and object hierarchies