Solution for high memory or CPU usage of linux c ++ applications _ 20161213, linux_20161213

Source: Internet
Author: User

Solution for high memory or CPU usage of linux c ++ applications _ 20161213, linux_20161213

For the vast majority of real-time programs, the balance between the loss of machines and the processing speed caused by the Loop Problems in real-time processing related programs, interaction with other programs and the impact on other functions will inevitably become the biggest obstacle in programming and the biggest breakthrough point.

In the face of all these problems, our unified solution is almost a multi-threaded operation, and the performance of the machine is brought to the maximum level that we can control, it also increases our processing speed to the highest level we can control.

However, for many people, the instability caused by multithreading is undoubtedly a nightmare.

For example:

At first, when we were writing a single-threaded program, we created a pipeline. There were several steps in the pipeline. We arranged a worker to finish one step of a product step by step, then we will continue to work on the next product. Slowly, with the increase in processing speed and machine performance, this solution is getting out, and we begin to use multithreading, we assign multiple or even dozens of workers to work at the same time, but with the increase in speed, the real problem comes one after another.

We began to split the steps on the assembly line, and started to allocate people according to the work intensity of each step on the assembly line. However, as the program continues to accumulate code and functions, there are two problems that become more and more obvious in our development process, which will greatly cause the energy and difficulty of later maintenance, in the worst case, the entire program can even be destroyed-the memory and CPU problems.

Memory problems and solutions:

In the pipeline, we use classes to encapsulate our logic functions. As the processing requirements increase, we constantly improve our underlying memory blocks and memory pools, but with the complexity of code, there will inevitably be memory blocks that cannot be released or memory blocks that exceed usage. This will cause the program to occupy more and more memory, re-reading leads to program crash and even data confusion caused by pointer disorder.

To solve this problem, we can closely monitor the memory creation and destruction phases of each block. For example, we can add materials to the memory when applying, and then check the materials we add when the memory is destroyed.

High CPU and solutions:

As the number of task links increases, we layer our programs and link them in various ways. However, despite the Qualified Data structure to coordinate various links, there is always a wrong connection, the next step will be the case where too many times of loop execution may even lead to an endless loop, and even more serious deadlocks may occur.

In the face of this situation, if we think about the design process, we can carefully analyze each link and propose the most inclusive structure. However, when we expand later, we can only constantly optimize and clarify the logic.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.