Memory leak in c ++

I am running my C ++ application on an Intel Xscale device. The problem is that when I run the offtarget (Ubuntu) application with Valgrind , it does not detect memory leaks.

But when I run it on the target system, it starts with 50K free memory and decreases to 2K per night. How to catch a leak that is not shown by Valgrind?

+6
c ++ memory-management memory-leaks
source share
9 answers

This may not be an actual memory leak, but perhaps a situation with increased memory usage. For example, it can highlight a continuously increasing line:

string s; for (i=0; i<n; i++) s += "a"; 

50k is not so much, maybe you should shift your source manually and see what might cause the problem.

+3
source share

A common culprit for these small embedded devecs is memory fragmentation. You may have free memory in the application between two objects. A common solution for this is to use a dedicated allocator (the new operator in C ++) for the most common classes. The memory pools used exclusively for objects of size N do not fragment - the space between two objects will always be a multiple of N.

+10
source share

It may not be a leak, but just a bunch of runtimes that don't free memory from the operating system. It can also be fragmentation.

Possible ways to overcome this:

  • Divide into two applications. The main application will have simple logic with little or no use of dynamic memory. He will launch a working application to actually work in such pieces so that the working application does not run out of memory and periodically restarts this application. Thus, the memory periodically returns to the operating system.

  • Write your own memory allocator. For example, you can select the allocated heap and allocate only memory, and then completely free the allocated heap. This requires the operating system to support multiple heaps.

Also note that it is possible that your program runs differently on Ubuntu and on the target system, and therefore different execution paths are executed, and code that causes a memory leak runs on the target system, but not on Ubuntu.

+3
source share

It sounds like fragmentation. Fragmentation is caused by the fact that you select objects on the stack, for example:

object1
object2
object3
object4

And then deleting some objects

object1

object3
object4

You now have a hole in your unused memory. If you select another object that is too large for the hole, the hole will be wasted. After all, with enough memory junk, you might end up with so many holes that they lose memory.

A way to solve this problem is to select the correct memory requirements. If you have certain objects that, as you know, you create many of them, try and make sure that they are the same size.

You can use the pool to make the allocation more efficient for a particular class ... or at least let you track it better so that you can understand what is happening and come up with a good solution.

One way to do this is to create one static:

 struct Slot { Slot() : free(true) {} bool free; BYTE data[20]; // you'll need to tune the value 20 to what your program needs }; Slot pool[500]; // you'll need to pick a good pool size too. 

Create a pool before starting the program and pre-allocate it so that it is as maximum as the maximum requirements for your program. You might want HeapAlloc (or the equivalent on your OS so that you can control when it appears from somewhere in your application).

Then redefine the new and delete operators for the suspicious class so that they return slots from this vector. So, your objects will be saved in this vector.

You can override new ones and delete for classes of the same size that will be placed in this vector.

Create pools of different sizes for different objects.

Just go for the worst offenders first.

I already did something similar before, and this solved my problem on the embedded device. I also used a lot of STLs, so I created a custom distributor (google for stl custom allocator - there are many links). This was useful for records stored in the mini-database used by my program.

+1
source share

If your memory usage is reduced, I donโ€™t think it can be defined as a memory leak. Where do you get memory usage messages? The system could just use most of your program memory in virtual memory.

All I can add is that Valgrind is known to be quite effective at detecting memory leaks!

0
source share

Also, are you sure when you profiled your code, the code coverage was enough to cover all the code paths that could be executed on the target platform?

Valgrind probably isn't lying. As already noted, it may actually be a bunch of runtimes that don't free up memory, but I would have thought otherwise.

0
source share

Do you use any sophisticated method to track the scope of an object ?? if so than valgrind is not smart enough though you can try setting the xscale parameter related to valgrind

0
source share

Most applications show memory usage as follows:

  • they use very little at startup
  • as they create data structures that they use more and more
  • when they begin to delete old data structures or reuse existing ones, they reach a steady state when memory usage remains approximately constant.

If your application is constantly growing in size, you can have aleak. If it increases in gray over the course of a period, and then reaches an equilibrium state, you probably do not.

0
source share

You can use the array tool from Valgrind, which will show you where most of the memory is allocated and how it evolves over time.

0
source share

All Articles