Memory Leak from a loop inside thread.
Hi everybody,
I am having memory leak problem from inside the thread. I have create this thread which does a monitoring on certain activity. This thread may run for 3/4 days. I have put a While loop inside the thread which is making the thread to do the same monitoring task again and again. This thread also interacts with the database too.
Environment :
language : C#
Databse : SQLite (its a small database and is open source).
I have tried different methods to reduce the memory leak problem, such as:
-Disposing all the class when not in used.
-called the GC forcefully.
-closing unmanaged resources, etc.
Please try to help me solve this problem.
Thank you,
shakti

Jan MontanoRank #1,295 · 1.3K reputationPosted Jun 5, 2007, 10:59 PM EDT
shakti shrestha3 reputationPosted Jun 6, 2007, 5:36 PM EDT
private void StartRealTrading() { string CurrencyPair = String.Empty; SimulationData Simulate = new SimulationData(); DataRow[] dRows; string PossessedCurrencyPair = string.Empty; bool IsPossessed = false; DataTable DtPossessed = new DataTable(); int cnt = 0; try { while (true) { if (cnt > 100) { GC.Collect(); cnt = 0; } cnt++; if (!_Start) break; try { // This function check the information from Database if (!GetDBPossessedCurrency(ref PossessedCurrencyPair)) this.AddOrderInformation = true; Thread.Sleep(500); // This Function dequeues object from queue and stores it into datatable if the AddOrderInformation ==true. DequeueOrderInformation(); //check if start date has crossed or not. if (DateTime.Now mUsrSetting.EndDate) break; //check if any currency pair exists for monitoring or not. dRows = dtOrderInfo.Select("DoMonitoring = 1 "); // 1= do monitoring ,, 0 = don't do monitoring if (dRows == null || dRows.Length == 0) { continue; } } catch (ThreadAbortException) { break; } catch (Exception ex) { this.mELog.WriteError("StartRealTrading1", ex, false); } //foreach (DataRow dRow in dtOrderInfo.Rows) for(int i=0;i<dtOrderInfo.Rows.Count;i++) { if (((int)dtOrderInfo.Rows[i]["DoMonitoring"]) != 1) continue; CurrencyPair = dtOrderInfo.Rows[i]["currencyPair"].ToString().Trim(); if (PossessedCurrencyPair != "" && PossessedCurrencyPair != CurrencyPair) { /* 2007/05/13 - shakti * I have commented this code because if the Monitoring is disabled for the code * then it will not be monitored for the whole session. So,inorder to allow the monitoring for the * whole session the code is commented */ //dRow["DoMonitoring"] = 0; //This code added by ooda san.. continue; } try { //acquire the current rate sTickPrice CurrentTickPrice = Utility.GetTickPrice(CurrencyPair); if (!CurrentTickPrice.DataFound) continue; //check if the currency is posssessed or not (both in IB and DB) //Currency will be possessed when the currency value is not equal to 0. //Currency is even when the Balance is in minus too (eg. -10000) IsPossessed = this.IsCurrencyPossessed(CurrencyPair, ref DtPossessed); //?? = New ??=Settlement if (!IsPossessed && dtOrderInfo.Rows[i]["OrderType"].ToString().Trim() == "??") {//currency not possessed //If the Order information (Currency pair is already possed) if (dtOrderInfo.Rows[i]["BuySellDone"].ToString() == "1") { continue; } //Give th Buy Sell order. PerformBuySellReal(dtOrderInfo.Rows[i], CurrencyPair, CurrentTickPrice); } else {//currency possessed //Give th Buy Sell order. PerformSettlementReal(dtOrderInfo.Rows[i], DtPossessed.Rows[0], CurrentTickPrice); } } catch (ThreadAbortException) { break; } catch (Exception ex) { this.mELog.WriteError("StartRealTrading2", ex, false); } if (!_Start) break; Thread.Sleep(200); } if (!_Start) break; Thread.Sleep(500); } } catch (ThreadAbortException) { // Do nothing } catch (Exception ex) { this.mELog.WriteError("StartRealTrading3", ex, false); } finally { DtPossessed.Dispose(); this._queOrderInfo.Clear(); this.dtOrderInfo.Dispose(); this.dtPossessedDetail.Dispose(); this.mCon.Close(); if(this.StopAutoRun !=null) StopAutoRun(); GC.Collect(); } }