How to limit the resources used by the system?

Asked

Viewed 750 times

12

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

The problem is that when executing the code snippet, the use of the processor increases gradually, until locking the application and later the operating system.

Well, my goal is to somehow limit the consumption of those resources that lead to a system crash. I know this will increase response time, but my priority at the moment is not to shut down the system.

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

See an excerpt from the 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 takes a character set and returns all possible combinations.

I tried to use only primitive variables to improve performance, but even so, when receiving a large set of characters the system ends up crashing.

  • 3

    Have you considered processing into a separate thread? With this he would use the processor, but still give space to other processes of the system and of all OS.

  • The operation locks the operating system or its application?

  • Try using a Thread.Sleep(10); so the system pauses and does not consume as much processing.

  • @Ricardo how could I do that? Could I formulate an answer so that I accept?

  • @jbueno everything starts with the application not responding, but in a short time the whole operating system gets stuck.

  • @Jedaiasrodrigues you use running this as? Winfor, Asp.net?

  • @Ricardo Windows Form, I’ll add in question.

  • 2

    Try to follow this guide, I needed to do a heavy processing here and with it I not only released the processes but did faster by using several only same time. If you have problems with the guide that you put the problem with your code that I help the most. https://msdn.microsoft.com/pt-br/library/ywkkz4s1.aspx

Show 3 more comments

4 answers

5


The first task you should do is to think about whether you have a better way to do this, I doubt you need such a heavy algorithm, or it’s not that heavy. Without a full context it is not helpful and neither is the focus of the question.

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

I’ll repeat, the real solution is to profile and find an algorithm, and maybe better data structure.

If you can’t improve it and you’re really getting in the way, then the operating system should be responsible for giving more or less priority to the process.

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

The estate Process.ProcessorAffinity 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 even bigger gambiarra.

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

  • Thank you so much for your reply @bigown, I found very interesting this possibility of being able to control the priority of the process, could you talk a little more about it? A trhead would be a good option?

  • 1

    I don’t know if you have much more to say. I don’t think you need to create thread to solve this. There are even some possible solutions, but for what? In fact it can get in the way, because if today you do not use all processors, and therefore have slack in processing, creating thread It will help to occupy everything, which is the opposite of what you want. It seems that not everyone understood this. You don’t want to take better advantage of the processor than to "waste" its use :) I doubt you need a complex solution, but it is possible to make a scheme of throttling of a thread, but again, what’s the point? Go simple.

1

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

Look at this Link

Having some problem in your code just say, we can follow up on another question if that’s the case.

I hope I’ve helped

  • Thank you @Ricardo this link solves my life, but to accept your answer, I need you to talk a little more about the subject Multithreading. So, not only me, but others who need it, you can go step by step knowing exactly what you’re doing.

1

I know it’s an old post, but it still has some points worth being listed...

1) The heavy memory load and slowness do not surprise, since you are trying to allocate all the possibilities in memory BEFORE returning. If you pass a 10x10 input array, the variable max will be 1E10, or 1 with 10 zeroes in the front. 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 a few 9.3 GIGS memory to allocate it all... out of the size of each word itself...

One way around this is to use the operator yield return. See these links:

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

2) In the excerpt nova += letras[i, posY[i]];, instead of using the += operator use a Stringbuilder object - see examples here. At each iteration on this line, the . NET is required to reallocate memory to build the current string.

1

I worked on a Human Resources Budget system that processed the payroll budget of over 500,000 employees, with several calculation steps that really consumed a lot of operating system resources.

The solution they found was to leave a dedicated machine for the calculation, and they conveniently called it the "Calculating Machine". Thus, the system was separated from the processing of the calculation, and allowed the user to request the execution of the calculation to pick up the results after they were already ready.

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

  • 1

    I worked with a sheet that ran in several companies with various complexities, had company that had needs that only this system solved. The existing computer at the time was the 386. It processed about 10 employees per second. I believe that today I process at least 1000 per second (probably more), which I would do in less than 10 minutes. I remember the time they won with wide slack a sheet that famous for being the fastest on the market :)

  • Thank you very much for the reply @Ulysses-Alves. Unfortunately I can not use a dedicated machine, because my application has to run directly on the client’s machine even, but I really liked the idea of notification, I will study a little more about multithreading and notifications.

  • Not at all. Another possibility is to create a service to run on the same machine, perhaps searching a database table waiting for new data to be processed. This way your application would continue with the responsibility only to meet the requests of users, while the service would do the heavy processing of data. I believe that this way you can also separate the two things: processing and business rules.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.