Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
Go Back  Xtreme Visual Basic Talk > > > Memory Leaks using picture boxes & BMP's


Reply
 
Thread Tools Display Modes
  #1  
Old 05-30-2014, 12:10 AM
graham2550 graham2550 is offline
Newcomer
 
Join Date: Aug 2013
Posts: 11
Default Memory Leaks using picture boxes & BMP's


This program graphs incoming data via serial a port sent from a PIC MCU.
The PIC does all the collection of data, being frequency, voltage and 10 channels of current from sensors.
The incoming data is a text string 46 characters long.
It consists of: 2 digits for frequency can be 1 - 99 i.e 50
next 4 digits are voltage * 10 ( the decimal place is calculated in the program by divide by 10). i.e 2350
then there are 10 lots of 4 digits, all between 0 and 8000 (these are actual watts calculated in the PIC. i.e 2345
so basically a string like 5023502345234523452345234523452345234523452345
with a CR at the end. this is sent every second. It is only plotted every 2 seconds, saved to disk each 2 seconds, a new file is created at midnight every day.

My problem is when running under Visual studio, it gobbles up memory at about 25Mb a second, but VS seems to somehow save itself from a crash by restoring memory.
But, when run as an exe, it runs out of memory within 1 1/2 minutes.
There is obviously a BIG memory leak some where, I tried disposing of the BMP's etc but to no avail.
The complete solution is attached.
Please help
Attached Files
File Type: zip Power Monitor Mk10.zip (374.0 KB, 2 views)

Last edited by graham2550; 05-30-2014 at 12:23 AM.
Reply With Quote
  #2  
Old 05-30-2014, 03:07 AM
PlausiblyDamp's Avatar
PlausiblyDampMemory Leaks using picture boxes & BMP's PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Only had a quick skim through but I noticed a few places where you are doing
Code:
myBMP(0) = New Bitmap(PBW, PBH)
and
Code:
myBMP(inc) = New Bitmap(PBW, PBH)
but you never seem to be disposing of those Bitmap objects, you could try disposing of them first and then reassigning the new bitmap to the array.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 05-30-2014, 10:47 AM
passel's Avatar
passelMemory Leaks using picture boxes & BMP's passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

You have to be careful because of the way you're copying your references.
You can't dispose of myBMP(0) before assigning a new bitmap to it, because you've already copied that reference to myPictureBoxes(0).Image, and you plan on using that bitmap to draw on the new bitmap.
So, you would dispose of the reference to the old bitmap using myPictureBoxes(0).Image before replacing it with the new bitmap.
Code:
'
    Private Sub PlotVolts()
'...
        myBMP(0) = New Bitmap(PBW, PBH)
        Using gr As Graphics = Graphics.FromImage(myBMP(0))
            gr.DrawImage(myPictureBoxes(0).Image, 1, 0)                             'display old plot, move right
            gr.DrawLine(myPens(0), 1, OldYpos(0), 0, NewYpos)                  'add the new piece of line
        End Using
'        myBMP(0).MakeTransparent(Color.Transparent) 'this doesn't do anything
        myPictureBoxes(0).Image.Dispose()       'Dispose of the previous reference here
        myPictureBoxes(0).Image = myBMP(0)   'Assign the new reference
'...
    End Sub
The following line says "make all the transparent colors ,i.e. argb(0,0,0,0), in my bitmap transparent ,i.e. argb(0,0,0,0)".
myBMP(0).MakeTransparent(Color.Transparent)
so, shouldn't be doing anything for you.
If you did something like
myBMP(0).MakeTransparent(Color.Fuchsia)
then that would make sense, if you wanted all the Fuchsia, argb(FF,FF,0,FF), to be changed to Tranasparent, argb(0,0,0,0).

When you create a new bitmap, New Bitmap(x,y), all the pixels of the bitmap are 0, argb(0,0,0,0), so is already transparent. The only non-transparent pixels are the ones you draw on it.

If I was doing this, I would avoid creating New bitmaps all the time (I generally try to avoid using New, when reasonable).
Since you're storing the points in an array, I would probably just use Clear to clear the existing bitmap and redraw from the array. But since the array holds a full days worth, and the display is only the latest, less than an hours worth, I can see that just shifting the bitmap is easier, calculation-wise.
But, even in that case, I would probably just initially create a bitmap and assign it to the picturebox, and create the "backbuffer" ones in the array as well.
Then, rather than create new bitmaps and copy the references around and disposing, etc., I would just follow the process...
Clear the backbuffer.
Draw the shifted bitmap from picturebox to backbuffer.
Add the new line segment to the backbuffer.
Clear the Picturebox image.
Draw the new bitmap to the Picturebox image (rather than assigning it to the picturebox), thus you're reusing existing objects.
That way you're not generating objects that have to be disposed off. The less garbage you create, the less likely you're going to create a memory leak.

Code:
  Private Sub PlotVolts()
    Freq = Val(LSet(IncomingText, 2))                               'get the freq part for the labels
    Voltage = Val(Mid(IncomingText, 3, 4))                          'get the voltage part of the string
    Voltage = Voltage / 10
    NewYpos = (PBH - ((Voltage * 20) - 4200)) 'scale to pic box, 800 vertical size , 4200 is 210v * 20 (20 pix/volt),
    '    myBMP(0) = New Bitmap(PBW, PBH)
    Using gr As Graphics = Graphics.FromImage(myBMP(0))
      gr.Clear(Color.Transparent)                                        'clear the destination
      gr.DrawImage(myPictureBoxes(0).Image, 1, 0)                        'display old plot, move right
      gr.DrawLine(myPens(0), 1, OldYpos(0), 0, NewYpos)                  'add the new piece of line
    End Using
    Using gr As Graphics = Graphics.FromImage(myPictureBoxes(0).Image)
      gr.Clear(Color.Transparent)                                        'clear the image in the picturebox
      gr.DrawImage(myBMP(0), 0, 0)                                       'update the picturebox image
    End Using
    MonitorArray(MonitorArrayIndex, 0) = OldYpos(0)
    OldYpos(0) = NewYpos
  End Sub
It could be simpler, if the Backgrounds were not transparent, but I assume you might want the backgrounds transparent so you can merge multiple images, or change the backcolor of the picturebox (and thus the backcolor of the graph).
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #4  
Old 05-30-2014, 07:07 PM
graham2550 graham2550 is offline
Newcomer
 
Join Date: Aug 2013
Posts: 11
Default

Hi again Passel,
The reason for so much shifting around and making transparent is, as you have noticed, because I merge them all together at the end with an overlay BlankBMP with all the horizontal/vertical lines on it.
You are right, the idea of merging what is effectively 12 bmp's is so I can turn off any or all as I want, yes, and change colors. The check boxes and small color boxes serve this purpose.
I didn't realize the "transparent" worked that way, it only makes transparent the color you nominate, is that correct?
I will try your code and suggestions and let you know what the results are.
Many thanks for your, as always, splendid help.
Reply With Quote
  #5  
Old 05-30-2014, 07:09 PM
graham2550 graham2550 is offline
Newcomer
 
Join Date: Aug 2013
Posts: 11
Default

Hi again Passel,
The reason for so much shifting around and making transparent is, as you have noticed, because I merge them all together at the end with an overlay BlankBMP with all the horizontal/vertical lines on it.
You are right, the idea of merging what is effectively 12 bmp's is so I can turn off any or all as I want, yes, and change colors. The check boxes and small color boxes serve this purpose.
I didn't realize the "transparent" worked that way, it only makes transparent the color you nominate.
I will try your code and suggestions and let you know what the results are.
Many thanks for your, as always, splendid help.
Reply With Quote
  #6  
Old 05-30-2014, 08:16 PM
graham2550 graham2550 is offline
Newcomer
 
Join Date: Aug 2013
Posts: 11
Default

Hi Passel,
the first option you suggested, disposing the myPictureBoxes(x), works. I created a release executable and memory seems to be holding up ok.
The second option didn't work. I am VERY surprised, as it seems very logical whats going on. But no output to screen.
I tried playing around with this code of yours, but no luck I'm afraid.
As you say, its best not to create things already there to use.
Reply With Quote
  #7  
Old 05-31-2014, 01:59 AM
graham2550 graham2550 is offline
Newcomer
 
Join Date: Aug 2013
Posts: 11
Default

This problem is now solved
Thanks Passel
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
 
Memory Leaks using picture boxes & BMP's
Memory Leaks using picture boxes & BMP's
 
-->