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.


 
LVL 2
yongyihAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Show us your code please...
0
yongyihAuthor Commented:
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."
--------------------------------------------------------------


0
yongyihAuthor Commented:
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.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

yongyihAuthor Commented:
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?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0
yongyihAuthor Commented:
 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.  
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I know there is a limit to the number of concurrent requests you can send out but I don't have much experience with web based stuff...

To make a delay:   (AddMilliseconds(), or AddSeconds() or etc...)

   ' two second delay
   Dim targetTime As DateTime = DateTime.Now.AddAddSeconds(2)
   While targetTime.Subtract(DateTime.Now).TotalMilliseconds > 0
       Application.DoEvents()
       System.Threading.Thread.Sleep(50)
   Wend
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yongyihAuthor Commented:
 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.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.