We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to solve OutofmemoryException problem in multithreading?

yongyih
yongyih asked
on
Medium Priority
674 Views
Last Modified: 2008-02-26
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.


 
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Show us your code please...

Author

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."
--------------------------------------------------------------


Author

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.

Author

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?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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...

Author

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.  
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.