Reputation: 1436
A program I am writing for work needs to keep track of the memory used by itself. But it can't monitor its memory usage as a whole, I need it to monitor each object in the program and how much memory that object is using, that way it can then tell that object to cut back on memory usage if it using above a certain capacity. The part that monitors memory usage holds a pointer to all created objects and keeps track of their memory usage by calling a method on that object that returns the size of the object.
The problem I'm having is that I cannot accurately calculate the size of the memory used. It doesn't matter if my calculation are off by a little bit but I'm getting big difference. The size that my program calculates varies (depending on which actions the program performs) between 1/2 to 2/3 of the actual memory usage of the program. For example a program that used 3.35gb of ram was calculated to only be using 2.16gb.
The current way I calculate the size of an object is by adding sizeof(*this) to the length of any vectors or arrays in the object multiplied by the sizeof the elements in the vector/array.
Is there something wrong with the way I'm calculating the memory used? Or is there just something else I'm not taking into account? If anyone knows of a program that you can analyse memory usage by different aspects of a program that would also be very helpful, that way I can track down where all this extra memory is coming from (preferably one that can run on Linux without GUI as I'm using Ubuntu server, but also have a windows machine I can use).
Upvotes: 3
Views: 3926
Reputation: 106530
Is there something wrong with the way I'm calculating the memory used?
Yes. First of all, the size of the memory consumed by your program may not be entirely used at any one point. For instance, after a vector resizes, the old memory block may be returned to the system. Or it may be held on to by the heap for the next time someone requests a memory block of the same size.
Also, do keep in mind that any libraries you are using (E.g. OS APIs) allocate memory, and those things aren't free.
Also keep in mind that there is additional overhead imposed by the heap manager for each memory allocation; usually on the order of a pointer or two, per allocation.
If you want to track what is using memory in your application, use a real memory profiler for that. If you want to dynamically scale how much memory your program allocates in the first place, use a profiler to figure out the average size taken up by one of the objects you are dealing with, and then limit by number of objects rather than memory size.
Upvotes: 2