|
-
Feb 18th, 2004, 10:15 PM
#1
Thread Starter
Hyperactive Member
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:
Public Function Paint_text(ByVal txt As String) As Bitmap
Dim bm_source As New Bitmap(Image.FromFile("C:\piv.bmp"))
Dim bm_dest As New Bitmap(CInt(bm_source.Width), CInt(bm_source.Height)
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width, bm_dest.Height)
Dim mybrush As New Drawing2D.LinearGradientBrush(ClientRectangle, FontDialog1.Color, FontDialog1.Color, Drawing2D.LinearGradientMode.Horizontal)
Dim myFont As New Font("Verdana", 12, FontStyle.Regula)
gr_dest.DrawString(txt, myFont, mybrush, New RectangleF(x1 , y1 , x2 - x1, y2 - y1)
Paint_text = bm_dest
gr_dest.Dispose()
mybrush.Dispose()
mypen.Dispose()
myFont.Dispose()
bm_source.Dispose()
bm_source = Nothing
bm_dest = Nothing
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.
-
Feb 18th, 2004, 11:16 PM
#2
Lively Member
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....
-
Feb 18th, 2004, 11:30 PM
#3
Frenzied Member
try using the "unlockbits" method of your new object
-
Feb 19th, 2004, 02:20 AM
#4
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...
-
Feb 19th, 2004, 04:36 AM
#5
Thread Starter
Hyperactive Member
Can you give me an exmaple, how to use it?
-
Feb 19th, 2004, 04:48 AM
#6
Hi.
Just put it in the end of you function.
VB Code:
Public Function Paint_text(ByVal txt As String) As Bitmap
Dim bm_source As New Bitmap(Image.FromFile("C:\piv.bmp"))
Dim bm_dest As New Bitmap(CInt(bm_source.Width), CInt(bm_source.Height)
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width, bm_dest.Height)
Dim mybrush As New Drawing2D.LinearGradientBrush(ClientRectangle, FontDialog1.Color, FontDialog1.Color, Drawing2D.LinearGradientMode.Horizontal)
Dim myFont As New Font("Verdana", 12, FontStyle.Regula)
gr_dest.DrawString(txt, myFont, mybrush, New RectangleF(x1 , y1 , x2 - x1, y2 - y1)
gr_dest.Dispose()
mybrush.Dispose()
mypen.Dispose()
myFont.Dispose()
bm_source.Dispose()
bm_source = Nothing
GC.Collect
Paint_text = bm_dest
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|