none
System.OutOfMemoryException' was thrown. at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark) at System.Threading.Thread.Start()

    Question

  • Hi,,,,

    I have multi windows services and each servce have multi-threads, Every thing work fine, but after two days or three this exception raise:

    System.OutOfMemoryException was thrown.
    Stac trace:   at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark)
       at System.Threading.Thread.Start()
    Target site:Void StartInternal(System.Security.Principal.IPrincipal, System.Threading.StackCrawlMark ByRef)

    Sample to create thresds:

    while (Loop each 3 muits)
    {
    new Thread(new ThreadTester().Loop).Start();
    }

    Note:

    ThreadTester().Loop : Class to read records from DB and do some things on it then do updates.

    Thanks alot.
    Wednesday, December 16, 2009 3:19 PM

Answers

  • If it's stopping working after running for a while, and as the exception informs, you're running out of system resources. You must be allocating IDisposable objects but forgetting to dispose them. The memory consumption is high in this case.

    The Right Thing is to use the using statement around the statements that use that objects, like: files, database connections, handles...the using automatically envolves your code with a try/finally and calls dispose on IDisposable objects (inside the first clause, like fs below).

    using (var fs = new FileStream())
    {
    }

    You can also force a garbage collection from time to time (a couple hours) with GC.Collect();
    • Marked as answer by Roahn Luo Wednesday, December 23, 2009 6:39 AM
    Wednesday, December 16, 2009 3:58 PM
  • Hello Flaylol,

    Maybe you could download a .NET Memory Profiler to detect if there are any memory issues in our code. Hope it helps!

    best regards
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Roahn Luo Wednesday, December 23, 2009 6:38 AM
    Monday, December 21, 2009 11:28 AM

All replies

  • One obvious conclusion that jumps out to me is the threads are not being cleaned up when finished (assuming they finish)?

    Assuming they don't finish, what would you expect your PC running the service to do when it continually spawns threads for a few days and doesn't clean up.

    Blog (WPF/C#)
    Crystal (WPF Google Contact UI/C# Library)
    Wednesday, December 16, 2009 3:47 PM
  • Yes, That is may happen, but what can you do about id?
    What is the solution?
    Wednesday, December 16, 2009 3:55 PM
  • If it's stopping working after running for a while, and as the exception informs, you're running out of system resources. You must be allocating IDisposable objects but forgetting to dispose them. The memory consumption is high in this case.

    The Right Thing is to use the using statement around the statements that use that objects, like: files, database connections, handles...the using automatically envolves your code with a try/finally and calls dispose on IDisposable objects (inside the first clause, like fs below).

    using (var fs = new FileStream())
    {
    }

    You can also force a garbage collection from time to time (a couple hours) with GC.Collect();
    • Marked as answer by Roahn Luo Wednesday, December 23, 2009 6:39 AM
    Wednesday, December 16, 2009 3:58 PM
  • using statement dose not work with your own classes, specialy if you have many classes!
    I think it i not right solution for this!

    If there any method to make free to any thread after period time (but we don't hve reference to threads).
    Wednesday, December 16, 2009 4:55 PM
  • What Andre is saying is not that your classes themselves need to be wrapped around the 'using' statement. But the logic in the classes perhaps could use some cleanup. Such as if one of your custom classes contains DB connections or File I/O operations, it would be wise to ensure they are finalized and cleaned up when they go out of scope.
    Blog (WPF/C#)
    Crystal (WPF Google Contact UI/C# Library)
    Wednesday, December 16, 2009 5:06 PM
  • Yes, all classes wrapped around using statement!
    Wednesday, December 16, 2009 5:10 PM
  • No, that is not what I said. Read what I said and then comment here again.
    Blog (WPF/C#)
    Crystal (WPF Google Contact UI/C# Library)
    Wednesday, December 16, 2009 5:11 PM
  • The key to the problem is in the Loop() method. Can you show it?
    Wednesday, December 16, 2009 5:20 PM
  •         public void Loop()
            {
                try
                {
                    DataTable _MDTReturn = new DataTable();
                    DBMethods _MDBMethods = new DBMethods();

                    _MDTReturn = _MDBMethods.GetDBRecords(Records_Count);
                    if (_MDTReturn.Rows.Count > 0)
                    {
                        CDBMaintenanceWorker _MDBMaintenanceWorker = new CDBMaintenanceWorker(MEventLog);data

                        int _MIntCounter = 0;
                        do
                        {
                            try
                            {
                                _MDBMaintenanceWorker.UMUpdateAFICSIdTCN(_MDTReturn.Rows[_MIntCounter++]);
                                GC.Collect();
                            }
                            catch (Exception e) { MEventLog.Log(e, String.Format("Error exception accord while proccessing record Id: {0}", _MDTReturn.Rows[_MIntCounter - 1]["QUEUEID"].ToString())); }
                        }
                        while (_MIntCounter < _MDTReturn.Rows.Count);
                        _MDBMaintenanceWorker = null;
                    }
                    _MDTReturn = null;
                    _MDBMethods = null;
                }
                catch (Exception e) {  }
                GC.Collect();
            }

    Wednesday, December 16, 2009 5:47 PM
  • How's the database connection is handled? I infer it is DBMethods that is handling this. Is it openning a new connection every time?
    Setting _MDTReturn to null just remove a reference to the object but that doesn't discard any resource yet. (For that you need do Dispose() objects, perhaps inside DBMethods' methods)

    Good luck
    Wednesday, December 16, 2009 5:56 PM
  • Hello Flaylol,

    Maybe you could download a .NET Memory Profiler to detect if there are any memory issues in our code. Hope it helps!

    best regards
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Roahn Luo Wednesday, December 23, 2009 6:38 AM
    Monday, December 21, 2009 11:28 AM