Link to home
Start Free TrialLog in
Avatar of yongyih
yongyihFlag for Malaysia

asked on

How to solve OutofmemoryException problem in multithreading?

Hi,

  My program will throw an OutofMemoryException after run for 30 minutes.  Below is how my program works.

  I have a button in my form.  There is a loop in my button and in that loop, it will create instance of a class and create Thread and assign a sub in the instance to run as multi-threading.

  I set the instance to Nothing within the loop. and try to call the Collect() function in garbage collector.  But when my program run for few minutes, it still will stop because of out of memory problem.

I tried to create a while loop in the loop above and hope it will slow down the speed of the loop so that the program has enough time to clean the memory but it does not work as well.

   Could you tell me how to clear the memory or any other methods to solve this problem?  Thanks first for your reply.


 
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Show us your code please...
Avatar of yongyih

ASKER

Hi Idle_Mind,

  My codes are all in my home PC.  Basically it looks like this.  If you cannot solve by looking the code blow, then I will show all the codes when I go home tonight.

  What is MemoryBarrier? Do you think I can use it to solve my problem? Thanks.

--------------------------------------------------------------
li_count=0
a=1
while  a <> txtstop.text
  dim temp as new myClass
  dim t As New Threading

  'set temp value here
  temp.name = "abc"
  t..Thread(address of temp.someprocess)  ' the someprocess function will check something in internet.. and insert result to textfile using file stream.  writeline() and close().
  t.start()
  t.join()  ' cant remember i put join() or sleep(0) here.

  li_count=li_count+1
  if li_count = 1000
     delay()   'call a Sub.. that loop 10000 times.  just want to slow down the loop
     gc.collect()
     li_count=0
  end if


  temp=nothing
wend


messagebox.show "Process Completed."
--------------------------------------------------------------


Avatar of yongyih

ASKER

The While loop is no problem.  Because if I set it to loop 100 times, then the program will finish all the process correctly and prompt the "Process Completed." messagebox.

FYI: There is no looping in Sub named temp.someprocess.  

Please advice.
Thanks.
Avatar of yongyih

ASKER

Hi,

  I just checked my code. I also use Application.DoEvents() in the loop.

Dim new_thread As New Thread(AddressOf temp.someprocess)
new_thread.Start()
new_thread.Sleep(0)  'and I am using Sleep(0)

If I didnt use Sleep(0), then program will run out of memory faster.

'This is the Sub that run in multi-threading.
'l_listbox.  Pass in by reference.

     Public Sub someprocess()
          Dim current As Thread = Thread.CurrentThread
          System.Windows.Forms.Application.DoEvents()
          Try
               System.Windows.Forms.Application.DoEvents()
               IPHost = Dns.Resolve(l_url)
               addresses = IPHost.AddressList
               l_listbox.Items.Add(l_url & " connected")
               appendToFile(l_url, Application.StartupPath & "\url_" & l_cnt_table.ToString & ".txt")
               current.Abort()
               current = Nothing
          Catch exc As Exception
               l_listbox.Items.Add(l_url & " not connected")
               'appendToFile(l_url, Application.StartupPath & "\error_" & l_cnt_table.ToString & ".log")
               current.Abort()
               current = Nothing
          End Try
     End Sub

  Any idea what is wrong?
Why are you using?

    current.Abort()

This can cause memory leaks as things in the Thread are not cleaned up properly.  Take out the call to Abort().  When the end of the someprocess() sub is reached then the Thread will end and clean up itself.  This is only one line away anyways...
Avatar of yongyih

ASKER

 I already removed the current.abort() statement. The same problem still happen.

 I just found out that the OutOfMemory exception was thrown in the loop, not the someprocess().  After I received the OutOfMemory Exception, the program still receiving a lot of results from DNS.Resolve() .  Do you think it is because of the Internet connection cannot support too many requests (DNS.Resolve()) to be sent out at the same time and cause the error?

  If there is no other way to solve the problem, then may be I have to use the delay() function to make the program loop slower again and hope the program has enough time to free up the memory.  Is there any function in vb.net that can make the loop wait for certain interval ?

  Please advice.  
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yongyih

ASKER

 I thought Thread.Sleep(50) is to make the thread run slower, not the loop?

  From my testing, I found out that if the URL is valid and accessible, the result will be returned almost immediately.  (IPHost = Dns.Resolve(l_url)).  I am wondering if I can clean all the memory and kill all threads when the OutOfMemory exception is thrown or not?

  Othewrise, may be I have to use Timer to solve this problem.  I will try again tonight.

  Thanks for your reply.
The purpose of the call to Sleep() in my code is to prevent the tight loop from ramping CPU usage to 100%.  

The Sleep() method makes the current thread sleep, and in this case, the current thread IS the loop.