Garbage Collector and Threads

  • Thread starter Thread starter Pealy
  • Start date Start date
P

Pealy

Hi, I'm re-posting this under a different subject to see if I can get
attract anyone to help, sorry if this annoys anyone.

Below is some code I've produced to reproduce a problem with
memory in my app. All it's doing is repeatedly calling a web service
from a form timer. I've done my homework and I know that using the new
thread(Addressof...) is not recommended but I can't see any
alternative. I've included a gc.collect to try and free memory but the
app slowly but surely sucks every last byte out of my iPaq until
everything dies. I have installed SP2 and this has helped slow the
leak
but it's still happening.

This behaviour seems to be linked to the fact that i'm calling a web
service as there doesn't seem to be a problem if I just populate the
string directly in the code.

Any advice on how to avoid using 'new' on each call to ThreadTest, or
how to reclaim the memory would be appreciated..

The application contains one form (with timer1 and a checkbox) and a
Module which contains two functions:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
If chkProcess.CheckState = CheckState.Checked Then
Label1.Text = ThreadTest()
Else
Label1.Text = "Disabled"
End If
System.GC.Collect()
End Sub

Imports System.threading

Module MemoryLeaks
Public aService As New HiFiService.Service1
Private sThread As Thread
Private blnBusy As Boolean = False

Public Function ThreadTest() As String
If blnBusy = False Then
sThread = New Thread(AddressOf DoSomething)
sThread.Start()
ThreadTest = "ok"
Else
ThreadTest = "busy"
End If
End Function

Private Sub DoSomething()
On Error GoTo anerror
Dim strTemp As String
blnBusy = True
strTemp = aService.Handshake("hello")
blnBusy = False
Exit Sub

anerror:
blnBusy = False
Exit Sub
End Sub
End Module
 
You can make a asyncronous calls to web services, so you
can avoid explicit creation of the threads. If you still
want to do that, you should consider using a thread pool...
 
Back
Top