Click here to Skip to main content
15,947,389 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I can't figure why memory is keeps growing by the second and why my cpu usage is so high (90%)...

I building a multithreaded database that's implementing the Producer Consumer design pattern. I'm pulling in real-time stock market data which I'm parsing then updating a table in my database. Here's an example of what I'm doing:

C#
List<threads> threads = new List<threads>();
object sync = new object();
EventWaitHandle ewh = AutoResetEvent(false);
Queue<string> q =  new Queue<string>();
int numberOfThreads = Environment.ProcessorCount;

// constructor of class used for parsing data
public ParseCls()
{
  MyDatabase databaseObj = new MyDatabase();
  for(int i = 0; i < numberOfThreads; i++)
  {
     threads.Add(new Thread(prase));
     threads[i].Start();
  }
}
public GetData(string stockData)
{
  lock(sync)
  {
     q.Enqueue(stockData);
  }
  ewh.Set();
}

private void parse()
{
  string s = "";
  lock(sync)
 {
   string s = q.Dequeue();
 }

 if(s.Length != "")
 {
   // parse string and create a new one then pass results to
   // my database
   string newS = s.Substring(0,1);
   dataBaseObj.GetData(newS);
 }
 else
 {
   ewh.WaitOne();
   s = "";
 }
 
// here's my database class
class MyDatabase
{
  int numberOfThreads = Enviorment.ProcessorCount;
  List<threads> threads = new List<threads>();
  EventWaitHandle ewh = AutoResetEvent(false);
  object sync = new object();
  Queue<string> q = new Queue<string>();

  public MyDatabase()
  {
     for(int i = 0; i < numberOfThreads; i++)
     {
        threads.Add(new Thread(update));
        threads[i].Start();
     }
  }

  public void GetData(string data)
  {
    lock(sync)
    {
      q.Enqueue(data);
    }
    ewh.Set();
  }

  private void update()
  {
    SQLConnection conn = etc...
    // other ado.net objects...

    while(true)
    {
      lock(sync)
      {
        string s = q.Dequeue();
      }

      // update database here
      // code...
      if(etc...)
      // blah, blah, blah
      else
      {
       ewh.WaitOne();
      }
    }
  }
}
Posted
Updated 7-Nov-11 6:55am
v2

1 solution

It's taking processor time because your update method doesn't have any kind of delay built in before it cycles and tries to handle the next value. It just keeps cycling.

It keeps eating memory because you're creating threads that never get disposed/deallocated because they're running endlessly.
 
Share this answer
 
Comments
Sander Rossel 7-Nov-11 13:57pm    
At least Thread.Abort isn't called! :)
My 5 for the answer.
d.allen101 7-Nov-11 14:13pm    
hey John Simmons/outlaw, i didn't include in my example the implementation of IDispose but it's in my actual code and the threads are properly disposed of. As far as the 'delay' before the update what should i do in this situation? so my for loop is iterating faster than what my database and/or update method can handle? is that correct?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900