0

I have a recursive algorithm that creates two new threads each time through. My tests crash with the following error when the array's length is 100k. I beleive this is exceeding the thread pool and/or running out of memory.

The question now, is how can I redesign the algorithm to not crash?

Test Authoring and Execution Framework: TE.ProcessHost.Managed[v5.3-1509] has stopped working

public class MyParamObj
{
    public int[] MyArray;
    public int MyIntOne;
    public int MyIntTwo;

    public static Create( int[] myArray, int myIntOne, int myIntTwo )
    {
        return new MyParamObj
               {
                   MyArray = myArray,
                   MyIntOne = myIntOne,
                   MyIntTwo = myIntTwo
               };
    }
}

public class MyClass
{
    public void Entry( int[] myArray)
    {
        int intOne = 0;
        int intTwo = myArray.Length;
        MyDelegate( MyParamObj.Create( myArray, intOne, intTwo) );
    }

    private void MyDelegate( object paramaObject )
    {
        var parameters = paramaObject as MyParamObjCreate;
        if (parameters == null) throw new ArgumentNullException(nameof(parameters));

        // just sample values
        int intOne = 0;
        int intTwo = 0;
        int intThree = 0;
        int intFour = 0

        var threadOneParams = MyParamObj.Create( parameters.MyArray, intOne, intTwo );
        var threadTwoParams = MyParamObj.Create( parameters.MyArray, intThree, intFour );

        var threads = new Thread[2];
        threads[0] = new Thread( MyDelegate );
        threads[1] = new Thread( MyDelegate );

        threads[0].Start(threadOneParams);
        threads[1].Start(threadTwoParams);

        threads[0].Join();
        threads[1].Join();

        //reorder elements within the array
    }
}

Update 3

I think I am exceeding the thread pool's max limit. When I have an array that's 100k long and I'm recursive down to a size of 1, I'll have 100k+ threads I think. Maximum number of threads in a .NET app? So, I guess my next question, is how do I recursively do this operation while not exceeding the thread limit?

Community
  • 1
  • 1
jacobvoller.com
  • 476
  • 7
  • 28
  • Could you please fix your code so that I can copy and paste it into VS or LINQPad to run it? Right now it's full of errors? You really need to post a [mcve] otherwise this question is unanswerable. – Enigmativity Apr 12 '16 at 02:22
  • @Enigmativity I fixed the method signature that was 'v', and added default values for the ints. I believe it should compile now but it's more of a theory question at this point then and implementation one. – jacobvoller.com Apr 12 '16 at 02:45
  • The code still doesn't compile, but at least it didn't take much to get it to compile. When you write a question on here you should do as much work as you can to help us answer it. In any case, when I ran it I got and exception "Exception of type 'System.OutOfMemoryException' was thrown.". You've created an infinitely recursive method that just runs out of RAM due to all of the threads. This code has nothing to do with an array of `int`s being thread-safe or not. Can you please fix it so that it does? – Enigmativity Apr 12 '16 at 02:56
  • 1
    You're not exceeding a thread limit. You're running out of RAM. You're also not using the thread pool - you're spinning up new threads. Each thread you create uses in excess of 1MB of RAM. So it doesn't take much to blow RAM. So if you have a list 100k long then you're trying to allocate 100GB+ of RAM. You might need to rethink your concept. – Enigmativity Apr 12 '16 at 02:58

1 Answers1

2

It's hard to tell since you left out huge chunks of code, but it looks like with the line threads[0] = new Thread( MyDelegate ); you have a function that is creating new threads of itself. Your program will explode as it recursively creates threads until it runs out of memory.

smead
  • 1,768
  • 15
  • 23
  • Haha I just had the same realization about 5 minutes ago. I believe that is correct, my question at this point is what can I do to fix this? Preferably, I would keep it recursive. If you're able to tell me how to avoid it, I'll mark your answer. – jacobvoller.com Apr 12 '16 at 02:47
  • 1
    It's hard to say because I'm not really sure what you're trying to do, exactly. I guess as a start you can just get the total number of open threads in your app with `System.Diagnostics.Process.Threads.Count` and decide on a number to stop at. – smead Apr 12 '16 at 02:53