Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible Memory Leak?

Status
Not open for further replies.

dbrooks74

Programmer
Nov 13, 2001
105
US
I have created a very basic program that moves a shape around the screen but when I look in the task manager, the size gets larger and larger even though I am mostly using local procedure variables.

Does anything look funny???


Private xG As Short
Private yG As Short

Private Sub DrawItems()
Dim graphicsG As System.Drawing.Graphics = Me.CreateGraphics()
Dim rectangleG As New System.Drawing.Rectangle(xG, yG, 20, 20)
graphicsG.Clear(Color.LightGray)

graphicsG.DrawEllipse(System.Drawing.Pens.Black, rectangleG)
graphicsG.DrawRectangle(System.Drawing.Pens.Red, rectangleG)

graphicsG.Dispose()
rectangleG = Nothing
End Sub

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

Select Case e.KeyCode
Case Keys.Down
yG = yG + 10
Case Keys.Up
yG = yG - 10
Case Keys.Right
xG = xG + 10
Case Keys.Left
xG = xG - 10
End Select

Call DrawItems()
End Sub
 
Someone's coding PacMan
;)

The only thing I could suggest is to declare

Dim rectangleG As New System.Drawing.Rectangle(xG, yG, 20, 20)

as a global or module level variable. Then just modify its x/y coordinates in the keydown event, and still call DrawItems.

The difference is that you aren't re-creating a rectangle every time you run the code.

Just because you set an object to nothing doesn't mean its gone...it just sits in the heap until garbage collection occurs. That might be why it just keeps building and building. If you just work with the one rectangle that might solve the issue though.

hth

D'Arcy
 
System.Drawing.Rectangle is a structure i.e. Value Type and is therefore allocated on the stack when created in a procedure. The Graphics object is probably the culprit even though you Disposed it. Try putting a GC.Collect after the Dispose or execute on a special Key just to see what happens.

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Your memory usage will grow until the GC decides it's time to free some memory.

The GC collects memory in three cycles, and doesn't collect the memory in some circumstances at all. All three cycles are progressively more intensive, starting from small local variables, moving onto larger variables & objects. The objects which never get freed & relocated are those above a certain size (~86kb in this version of the framework, AFAIK), as the cost to compact them in memory is too expensive.

In short, unless you're writing for a memory-constrained system (PDA or smart phone), or if you're concerned about latency during a GC cycle, you shouldn't have to worry about when memory is freed. You just need to make sure that you dispose of your objects that implement IDisposable when you're done with them.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
After using my form for 2 minutes, the memory stopped going up, so perhaps it only goes to a certain point. My concern was that it was going to increase by 100 Kbits every move which could be a lot considering how long my "pac-man" game would run (good guess jfrost10).

Thanks for all of your input.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top