Results 1 to 6 of 6

Thread: Low memmory. What to do?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309

    Low memmory. What to do?

    Hey, I have made app, which repaints and changes wallpaper after some period. But something goes wrong. Every time then function is called ~7 MB of memory are "ate". How to free it after doing job? I tried dispose, but nothing.


    VB Code:
    1. Public Function Paint_text(ByVal txt As String) As Bitmap
    2.  
    3. Dim bm_source As New Bitmap(Image.FromFile("C:\piv.bmp"))
    4. Dim bm_dest As New Bitmap(CInt(bm_source.Width), CInt(bm_source.Height)
    5. Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
    6. gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width, bm_dest.Height)
    7.  
    8. Dim mybrush As New Drawing2D.LinearGradientBrush(ClientRectangle, FontDialog1.Color, FontDialog1.Color, Drawing2D.LinearGradientMode.Horizontal)
    9.  Dim myFont As New Font("Verdana",  12, FontStyle.Regula)
    10. gr_dest.DrawString(txt, myFont, mybrush, New RectangleF(x1 , y1 , x2 - x1, y2 - y1)
    11. Paint_text = bm_dest
    12.  
    13.         gr_dest.Dispose()
    14.         mybrush.Dispose()
    15.         mypen.Dispose()
    16.         myFont.Dispose()
    17.         bm_source.Dispose()
    18.         bm_source = Nothing
    19.         bm_dest = Nothing
    20.  
    21.     End Function

    Then I set returned image to picturebox (~3MB). But it's OK. This is not why after every function call memory isn't cleared.

  2. #2
    Lively Member
    Join Date
    Jul 2003
    Location
    Kuala Lumpur (Malaysia)
    Posts
    92

    Check your function...

    Hi,

    I think your function is holding the buffer. Try to clear the buffer by using "Paint_text=Nothing"after you have transfer the data to somewhere....

  3. #3
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    try using the "unlockbits" method of your new object

  4. #4
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    This is a no-no and a bad way of doing things, but it works.

    Use GC.Collect.

    That will force the garbagecollector to clean up.
    I have had a few situations where I used som API BitBlt'ing, and for some reason I couldn't get it to release my memory even though I did release HDC's and Destroy and everything else I could think of to clean up.

    The only way I could solve this (on my own, anyway...) was using GC.Collect.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309
    Can you give me an exmaple, how to use it?

  6. #6
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Just put it in the end of you function.

    VB Code:
    1. Public Function Paint_text(ByVal txt As String) As Bitmap
    2.  
    3. Dim bm_source As New Bitmap(Image.FromFile("C:\piv.bmp"))
    4. Dim bm_dest As New Bitmap(CInt(bm_source.Width), CInt(bm_source.Height)
    5. Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
    6. gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width, bm_dest.Height)
    7.  
    8. Dim mybrush As New Drawing2D.LinearGradientBrush(ClientRectangle, FontDialog1.Color, FontDialog1.Color, Drawing2D.LinearGradientMode.Horizontal)
    9.  Dim myFont As New Font("Verdana",  12, FontStyle.Regula)
    10. gr_dest.DrawString(txt, myFont, mybrush, New RectangleF(x1 , y1 , x2 - x1, y2 - y1)
    11.  
    12.         gr_dest.Dispose()
    13.         mybrush.Dispose()
    14.         mypen.Dispose()
    15.         myFont.Dispose()
    16.         bm_source.Dispose()
    17.         bm_source = Nothing
    18.  
    19. GC.Collect
    20.  
    21. Paint_text = bm_dest
    22.     End Function

    You might have noticed that I have moved Paint_Text=bm_Dest to the bottom and deleted the bm_dest=Nothing.

    Usually you would use Return bm_Dest and that would exit the function, and your dispose commands would not be called.
    So always dispose everything you can before returning the result.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width