report a cf bug

S

sheng shan

there's a memory leak exist in such a simple app below:
the app only have a timer and a form
timer's interval is 100ms

below is the code in vb.net with cf sp1:
Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Private xval As Byte
Private yval As Byte

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
MyBase.Dispose(disposing)
End Sub

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Timer1 = New System.Windows.Forms.Timer
Me.TextBox2 = New System.Windows.Forms.TextBox
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(344, 96)
Me.TextBox1.Size = New System.Drawing.Size(136, 21)
Me.TextBox1.Text = "TextBox1"
'
'Timer1
'
Me.Timer1.Enabled = True
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(192, 40)
Me.TextBox2.Size = New System.Drawing.Size(176, 21)
Me.TextBox2.Text = "TextBox2"
'
'Form1
'
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Text = "Form1"

End Sub

Public Shared Sub Main()
Application.Run(New Form1)
End Sub

#End Region

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim CalcVal As Single
Dim TempVal As Single
Dim TempStr As Single
If xval < 255 Then
xval = xval + 1
yval = yval + 1
Else
xval = 0
yval = 0
End If
CalcVal = Convert.ToSingle(Convert.ToSingle(xval) +
Convert.ToSingle(yval) * 256)
TempVal = CalcVal / 65536 * 100
'TempStr = TempVal.ToString("0.0") & "kN"
TempStr = Int(TempVal * 10 + 0.5) / 10
TextBox1.Text = TempStr & "kN"
TextBox2.Text = Microsoft.VisualBasic.TimeOfDay
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
xval = 0
yval = 0
End Sub
End Class

run the app,then check the memory of ce system,you will find a memory
leak.
 
G

Geoff Schwab [MSFT]

Hi Sheng,

This is not actually a memory leak. Remember that under the .NET Compact
Framework, garbage collection is handled automatically and memory is not
freed immediately when it goes out of scope. In fact, when you close your
application using Start->Settings->System->Memory->Runnning Programs, the
memory is all cleaned up properly. It looks like the calls to
'TextBox2.Text = Microsoft.VisualBasic.TimeOfDay' and 'TextBox1.Text =
TempStr & "kN"' are allocating memory every frame. This memory will
eventually be cleaned up by the garbage collection system but not every
frame. Whenever possible, it is best to cache memory in one spot rather
than every frame.

For more information on garbage collection, see the following FAQ item:
1.20. How is memory managed in the .NET Compact Framework?
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx#1.20

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sheng shan

Hi Geoff,
Actually I do have the memory leak in my app,there are a lot of
simular processes(like the sample code I offered) in a timer of my
app,it seems the GC of the system is not able to handle more than 10
such a processes in a timer.so eventually the available memory of the
system is consumed up.and my app is dead.any suggestion to solve my
problem,please?or how to improve the code?
 
G

Geoff Schwab [MSFT]

Hi Sheng,

It is best if you can create a single instance of memory and re-use it
rather than re-allocating every time the timer goes off. In your case, it
is a little more difficult though. I do not like to suggest this for most
cases but you may want to try and force garbage collection every so often.
If you add a counter then you can force a garbage collection every X number
of times the timer is called. This seems more like masking the symptoms
than curing the problem in most cases but it may work for you. I ran a
quick test with your code and this stopped the constant loss of memory in my
sample.

' Place this as a global outside of your counter
Dim NumTimerCalls As Int32 = 0

' Place this as the last code executed in the counter
NumTicks += 1
If NumTimerCalls Mod 200 = 0 Then
System.GC.Collect()
End If

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sheng shan

Hi Geoff,
Thanks for your help,I tried your method before,I even tried
GC.collect() every time the timer is called,but still it doesn't
work.another method I tried is to call a DLL which I wrapped the system
function "forcepageout",that method can slowdown the memory leak,but
only makes my app survive for 2 more hours.any suggestion,please?
 
D

Daniel Moth

Hi

Interesting article referenced by that link... It does however talk about
generations etc and I was under the impression that .NETcf CLR handles all
that in a *completelly* different manner...(?) For example the 3 collection
types, as described by one of your colleagues in this ng and also in Yao's
book, do not exist on the desktop...

By the way, I have experienced similar symptoms to the original poster I
think... will try to create a repro case if I get some time...

Cheers
Daniel
 
S

sheng shan

I start to doubt if the garbage collection of compact framework is
strong enough to handle all the memory process.
maybe it would be corrected in sp2?
 
S

sheng shan

Never mind,the problem is solved now.Yesterday I installed cf sp2(beta
version) which I downloaded from http://www.betaplace.com,after that I
tried my app again,and the memory leak is gone.It seems this kind of
memory leak does exist in cf and cf sp1 and it will be fixed in sp2.
it's really a good news,right?
 
G

Geoff Schwab [MSFT]

Hi Sheng,

Thanks for the update. I was looking into the problem and the fact that I
already have SP2 Beta installed may explain why I was unable to reproduce
the problem - the next step was going to be to uninstall SP2. I would still
like to know the root of the problem but I am glad it is working for you
now.

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top