OpenNetCF.Net Memory Leak ****Urgent****

S

Simon

I am developing a VB.Net 2003 application for PocketPC using a wireless
network connection to a SQL Server Database. I am using the
OpenNetCF.Net libraries to monitor the state of the network so I can
start saving the data locally when the network strength is getting low.

I am having a problem where the Memory used by the Application is
slowly increasing until I get an OutOfMemoryException or some other
component fails to load probably due to the low memory. I am using
version 1.4.50804.0 of the OpenNetCF assemblies.

The following code is from a test program I have run to monitor the
problem:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

Dim Strength As OpenNETCF.Net.StrengthType =
OpenNETCF.Net.StrengthType.NotAWirelessAdapter
Dim db As Int32

'Check the Wireless Signal Strength
Dim al As OpenNETCF.Net.AdapterCollection =
OpenNETCF.Net.Networking.GetAdapters
For iLoop As Int32 = 0 To al.Count - 1
Dim tmpAdapter As OpenNETCF.Net.Adapter = al.Item(iLoop)
If tmpAdapter.IsWireless Then
Strength = tmpAdapter.SignalStrength.Strength
db = tmpAdapter.SignalStrength.Decibels
End If
tmpAdapter.Dispose()
Next
Strength = OpenNETCF.Net.StrengthType.NoSignal

'Dim x As New String("x"c, 10000)

counter += 1
If (counter \ 10) * 10 = counter Then
Dim Txt As String =
counter.ToString("#,###,##0").PadLeft(10) &
GC.GetTotalMemory(False).ToString("#,###,##0").PadLeft(10) &
GetAvailablePhysicalMemory()
ListBox1.SelectedIndex = ListBox1.Items.Add(Txt)
ListBox1.Refresh()
Label1.Text = Txt
Label1.Refresh()

End If

End Sub

Private Structure MEMORY_STATUS
Public dwLength As UInt32
Public dwMemoryLoad As UInt32
Public dwTotalPhys As UInt32
Public dwAvailPhys As Integer
Public dwTotalPageFile As UInt32
Public dwAvailPageFile As UInt32
Public dwTotalVirtual As UInt32
Public dwAvailVirtual As UInt32
End Structure

<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Sub GlobalMemoryStatus(ByRef ms As MEMORY_STATUS)
End Sub

Public Function GetAvailablePhysicalMemory() As String
Dim ms As New MEMORY_STATUS
Try
GlobalMemoryStatus(ms)
Dim avail As Double = ms.dwAvailPhys / (1024 * 1024)
Dim sAvail As String = avail.ToString("0.00").PadLeft(10)
Return sAvail
Catch ex As Exception
MessageBox.Show(ex.Message)
Return Nothing
End Try
End Function

The timer intreval is set to 10. If I comment out the OpenNETCF.Net
code, uncomment the "x" padded string and comment out the ListBox code,
after an initial drop upto the first Garbage Collection, the Available
Physical memory remains constant.

The following shows the Output of the run in the format (I've cut some
out):
Loops
GC.GetTotalMemory Value
Available Physical Memory (MB)

0 212,540 53.61
10 294,460 53.61
20 392,764 53.61
30 474,684 53.61
40 556,604 53.61
50 654,908 53.52
60 736,828 53.39
70 828,988 53.26
80 927,292 53.12
90 230,960 53.00
100 312,880 53.00
110 394,800 53.00
120 476,720 53.00
130 575,024 53.00
140 656,944 53.00
150 738,864 52.94
160 837,168 52.79
170 919,088 52.65
180 214,576 52.53
190 296,496 52.53
200 378,416 52.53
210 476,720 52.53
220 558,640 52.53
230 640,560 52.53
240 738,864 52.47
250 820,784 52.35
260 902,704 52.22
270 1,001,008 52.07
280 280,112 52.06
290 378,416 52.06
300 460,336 52.06
310 542,256 52.06
320 640,560 52.06
330 722,480 52.03
340 804,400 51.91
350 902,704 51.77
360 984,624 51.63
2,000 951,856 43.32
2,010 230,960 43.20
2,020 329,264 43.20
2,030 411,184 43.20
2,040 493,104 43.20
2,050 591,408 43.20
2,060 673,328 43.20
2,070 755,248 43.15
2,080 853,552 43.01
2,090 935,472 42.88
2,100 1,017,392 42.75
2,110 312,880 42.73
2,120 394,800 42.73
2,130 493,104 42.73
2,140 575,024 42.73
2,150 656,944 42.73
3,910 871,996 33.69
3,920 970,300 33.55
3,930 249,404 33.42
3,940 347,708 33.42
3,950 429,628 33.42
3,960 511,548 33.42
3,970 609,852 33.42
3,980 691,772 33.42
3,990 773,692 33.38
4,000 871,996 33.23
4,010 953,916 33.11
4,020 1,035,836 32.98
4,030 331,324 32.95
4,040 413,244 32.95
4,050 495,164 32.95
4,060 593,468 32.95
4,070 675,388 32.95
4,080 757,308 32.93
4,090 855,612 32.79

and keeps dropping until Available Physical Memory reaches 29.35MB at
which point the OpenNETCF.Net.Networking.GetAdapters collection
contains 0 items. Presumably it is handling the OutOfMemoryException
and returning an empty collection. There seems a pattern where the
Available Physical Memory stays constant until the GC.GetTotalMemory
gets to about 700,000 and then the Available Physical Memory drops
until the Garbage Collection is performed. It's as if the GC is
clearing the objects OK but some memory is being left by each object
after collection.

Has anyone else had any issues? Am I doing anything wrong?

Regards

Simon Reynolds
 
P

Paul G. Tobey [eMVP]

There is a leak in AccessPointCollection, near line 329 in RefreshList().
You need to add the following:

FileEx.CloseHandle( ndisAccess );


before leaving that method.

I'm unable to find anything else actually leaking. All of the managed
arrays, I would think, should be getting freed when the garbage collector
decides that it's time. There's very little, if any, unmanaged memory
allocated when that series of steps is performed, so there isn't a lot of
room for things to get lost.

Paul T.
 

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