5 Replies - 8032 Views - Last Post: 22 September 2007 - 03:42 AM Rate Topic: -----

#1 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

OutOfMemoryException with many threads

Posted 21 September 2007 - 05:20 AM

I am writing an IP Scanner which uses a semaphore to manage the amount of threads running at the same time and a method to create the threads. When I have 255 IPs to scan it makes 255 threads and sends them to the semaphore for processing. The problem arises when I have around 1,000 threads to send at one time I will get the OutOfMemory exception message. Does anybody have any suggestions on what I can do to stop this? Thank you all in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: OutOfMemoryException with many threads

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: OutOfMemoryException with many threads

Posted 21 September 2007 - 05:28 AM

If you're doing this in an Array you can the maximum size to, say 250, then send them 250 at a time.
Was This Post Helpful? 0
  • +
  • -

#3 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Re: OutOfMemoryException with many threads

Posted 21 September 2007 - 06:17 AM

I didn't think about using an array for this... I'll give that a try. I was just sending all of the addresses to the semaphore queue and letting the semaphore sort it out. I know the amount of threads is the problem and was considering making a separate function to just create the threads. I'll let you know if this works.

How long did you code before you got the MCAD cert??
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: OutOfMemoryException with many threads

Posted 21 September 2007 - 07:31 AM

Well I got my first MCSD about 5-7 years after I became a full-time professional programmer. I got my MCSD & MCAD in C# and VB.Net about 2 years ago.
Was This Post Helpful? 0
  • +
  • -

#5 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Re: OutOfMemoryException with many threads

Posted 21 September 2007 - 12:28 PM

Thats awesome!! I have just started programming about a year ago and I'm slowly studying for MCAD with C# I came from a network background i got my A+,N+, CCNA and MCP then fell in love with programming. I have to get home before I try your suggestion. I let you know if it works tonight.
Was This Post Helpful? 0
  • +
  • -

#6 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Re: OutOfMemoryException with many threads

Posted 22 September 2007 - 03:42 AM

Well I tried putting the ip addresses in an array, StringBuilder of course, and using ToString() where a string was needed. I was still having the same problem because too many threads were being sent down at nearly the same time. I needed to figure out how to use the Join() method on a nameless thread well this isn't possible so what I ended up doing is making an if/else statement. Using a century variable to count the number of threads I have made so far I said if the century value is 255 or more create a thread named waitForMe which does the same exact thing as the nameless thread except I call the Join() method after starting that thread.

When I scanned a classB network (65,535 hosts) my cpu went upto 100% for a few minutes which was not a huge supprise. My next learning experience will be trying to get my IPScanner to play nice with the processor and not eat all of it's resources.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1