How to limit the resources used by the system?

11

I have a loop of repetition that does several iterations and has in its scope calculations that require a lot of processing.

The problem is that when running the code snippet, processor usage will gradually rise, until the application crashes and then the operating system later.

Well, my goal is to limit somehow the consumption of these features that lead to system crashes. I know this will increase response time, but my priority at the moment is not to crash the system.

  

The project is in Windows Forms , but this snippet is in a    Class Library .

See a snippet of code:

private static string[] TodosPossiveis(char[,] letras)
{
            int linhas = letras.GetUpperBound(1) + 1;
            int colunas = letras.GetUpperBound(0) + 1;
            int max = (int)Math.Pow(linhas, colunas);
            string[] todos = new string[max];


            int[] posY = new int[colunas];

            int atual = 0;
            while (atual < max)
            {

                string nova = "";
                for (int i = 0; i < colunas; i++)
                {
                    nova += letras[i, posY[i]];
                }
                for (int i = colunas - 1; i > -1; i--)
                {
                    posY[i]++;
                    if (posY[i] == linhas)
                    {
                        posY[i] = 0;
                    }
                    else
                    {
                        break;
                    }
                }
                todos[atual++] = nova;
            }

            return todos;
 }

This method receives a character set and returns all possible combinations.

I tried to use only primitive variables to improve performance, but even so, receiving a large character set, the system crashes.

    
asked by anonymous 24.11.2015 / 18:24

4 answers

5

The first task you must do is to think about whether you have a better way to do this. I doubt you need such a cumbersome algorithm. Or it's not that heavy. Without a full context, it can not help. And neither is the focus of the question.

Evaluate if you are not having memory problems. Without knowing exactly where the problem is, you will find the wrong solution.

I'll repeat, the real solution is to outline and find an algorithm, and maybe a better data structure.

If you can not improve it and are really messing up, then the operating system should be responsible for giving more or less priority to the process.

You can do this by calling the program with start or have your own code decrease priority with the Process.PriorityClass property. A BelowNormal should serve, or may exaggerate and put in Idle .

The Process.ProcessorAffinity property can help by preventing all colors are used by the process.

There is the possibility of creating some code that minimizes this, but I do not find a good solution in most cases. It is gambiarra greater still.

Be careful not to play tricks that seem to solve and only get worse.

    
24.11.2015 / 18:43
1

Using this tutorial you will be able to separate this processing. It will consume the resource of the machine, but will not lock the machine, still could calculate percentage, count the processing, etc. updating on the screen.

See this Link

Having a problem with your code is just saying, we can follow up on another question if it is the case.

I hope I have helped

    
24.11.2015 / 19:06
1

I worked on a Human Resource Budget system that processed the payroll budget of more than 500,000 employees, with several calculation steps that really consumed the features of the operating system.

The solution they found was to leave a machine dedicated to the calculation, and conveniently called it the "Calculation Machine". In this way, the system would be separated from the calculation processing, and would allow the user to request the calculation execution to pick up the results after they were ready.

Maybe this will work for you too. So instead of trying to process everything at once and already showing the results to the user, you just notify the user that the processing has started and is in progress, and after processing is complete you notify the user and display the results to him. This can be either in file, report or on a screen of your system.

    
25.11.2015 / 16:16
1

I know it's an old post, but it still has some points worth listing ...

1) The heavy memory load and slowness are not surprising, since you are trying to allocate all possibilities in memory BEFORE you return. If you pass an input array of 10x10, the variable max will be 1E10, or 1 with 10 zeros in front. I think that if you try to allocate a return array of this size, it will have at least that amount of byte consumption, which gives 9.3 GIGAS memory to allocate all of that ... off the size of each word itself ...

One way to get around this is to use the yield return operator. Check out these links:

With this, you avoid allocating everything in memory. Another point is to change the return to IEnumerable<string> .

2) In the% wrapper%, instead of using the + operator = use a StringBuilder object - see examples here . At each iteration in this line, .NET is required to reallocate memory to construct the current string.

    
24.07.2018 / 02:36