Robert_Zenz
Well-known member
Hello.
Since my Project has reached some size I've started using the Garbage Collection and trying to find best ways to keep the useage of memory and cpu small. However, I was trying out how ByVal and ByRef are behaving with the garbage collection and found some pretty strange behavior (code below).
I've created a test application with two checkboxes and a timer. The timer refreshes a label with System.GC.GetTotlaMemory(False). If the checkboxes get checked, a function is called which receives a String (filled with 2^25 spaces) (ByRef or ByVal) and circles while the checkbox stayes checked (to see the actual memory useage).
If I check the ByVal Checkbox2 the memory usage grows up on 67MB and stays there until I uncheck it, then it falls back on some hundred kilobytes.
If I check the ByRef Checkbox1 the memory useage also grows on 67MB, but stays there even after unchecking! To me this seems like that the ByRef Object does not get destroyed by the garbage collection, but ByRef shoudln't create any object at all. I would expect such an behavior from ByVal, but not from ByRef since there is no actual copy of the object. Can somebody explain this to me?
If you want to try to reproduce it, just copy this Code onto a form with a Timer, a Label and two checkboxes:
Best Regards,
Bobby
Since my Project has reached some size I've started using the Garbage Collection and trying to find best ways to keep the useage of memory and cpu small. However, I was trying out how ByVal and ByRef are behaving with the garbage collection and found some pretty strange behavior (code below).
I've created a test application with two checkboxes and a timer. The timer refreshes a label with System.GC.GetTotlaMemory(False). If the checkboxes get checked, a function is called which receives a String (filled with 2^25 spaces) (ByRef or ByVal) and circles while the checkbox stayes checked (to see the actual memory useage).
If I check the ByVal Checkbox2 the memory usage grows up on 67MB and stays there until I uncheck it, then it falls back on some hundred kilobytes.
If I check the ByRef Checkbox1 the memory useage also grows on 67MB, but stays there even after unchecking! To me this seems like that the ByRef Object does not get destroyed by the garbage collection, but ByRef shoudln't create any object at all. I would expect such an behavior from ByVal, but not from ByRef since there is no actual copy of the object. Can somebody explain this to me?
If you want to try to reproduce it, just copy this Code onto a form with a Timer, a Label and two checkboxes:
VB.NET:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Label1.Text = System.GC.GetTotalMemory(False).ToString("N0")
End Sub
Private Sub testRef(ByRef sender As Object)
While CheckBox1.Checked
Application.DoEvents()
End While
End Sub
Public Sub testVal(ByVal sender As Object)
While CheckBox2.Checked
Application.DoEvents()
End While
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Dim str As String = Space(2 ^ 25)
Me.testRef(str)
str = Nothing
System.GC.Collect()
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
Dim str As String = Space(2 ^ 25)
Me.testVal(str)
str = Nothing
System.GC.Collect()
End Sub
Best Regards,
Bobby