How to track malloc and free?

Possible duplicate:
Simple C implementation for malloc / free memory tracking?

I need to know how much memory I have used so far in a C program, and here is the pseudo code

#include <stdio.h> int usedMemory =0; void *MyMalloc(int size){ usedMemory = usedMemory +size ; return malloc(size); } void MyFree(void *pointer){ /*****************what should i write here????*************/ } int main(int argc, char *argv[]) { char *temp1= (char *)MyMalloc(100); char *temp2= (char *)MyMalloc(100); /*......other operations.........*/ MyFree(temp1); MyFree(temp2); return 0; } 

Can someone tell me what to write in the MyFree method (which reduces the amount of memory freed from usedMemory.

+4
source share
5 answers

You can select a few extra bytes more than specified, and save the size in additional bytes so that you can find out the size later, in MyFree , with a little calculation:

 unsigned long int usedMemory = 0; void *MyMalloc(int size) { char *buffer = (char *) malloc(size + sizeof(int)); //allocate sizeof(int) extra bytes if ( buffer == NULL) return NULL; // no memory! usedMemory += size ; int *sizeBox = (int*)buffer; *sizeBox = size; //store the size in first sizeof(int) bytes! return buffer + sizeof(int); //return buffer after sizeof(int) bytes! } void MyFree(void *pointer) { if (pointer == NULL) return; //no free char *buffer = (char*)pointer - sizeof(int); //get the start of the buffer int *sizeBox = (int*)buffer; usedMemory -= *sizeBox; free(buffer); } 
+10
source

In C ++, you can save the global std::map<void*, std::size_t> to track the size of each selected block; your own distribution function will register the size during distribution, and the release function will delete the record. (Update: either do as the related question is asked, and allocate a little more memory and save the size there.)

A more fundamental problem is that it will probably be very limitedly used in a typical C ++ program: allocations there are performed mainly in two ways: 1) through explicit new expressions that call ::operator new() , which in ( usually) calls malloc() and 2) through std::allocator<T>::allocate() , which is implemented in many platforms in terms of ::operator new() .

The problem is that you do not have control over the features of your platform. You can replace the global operator with a new one to use your own MyMalloc() , but by default std::allocator can use malloc() directly and therefore cannot be affected by this.

A cleaner approach for debugging is to use an external tool like valgrind to track heap usage. For ongoing internal use, tracking placement sizes will also result in significant performance degradation.

+2
source

You can select the memory and save its size in the selected block (the verification error is omitted for brevity):

 unsigned int totalAlloc = 0; void *MyAlloc(unsigned int size) { void *p; totalAlloc += size; p = malloc(size + sizeof(int)); *(int *) p = size; return (void *)(((int *) p) + 1) } void MyFree(void *ptr) { ptr = (void *)(((int *) ptr) -1 ); totalAlloc -= * (int *) ptr; free(ptr); } 

This code actually reserves more memory than requested to preserve the block size in the (usually) first four bytes. You can then get this information later when you free up memory.

+1
source

You need to manage the list of all malloc () that you made with the + size pointer. Then you can search for the size in this list and reduce it in free ().

Check out the example in this example how they do it: http://developers.sun.com/solaris/articles/lib_interposers_code.html#malloc_interposer.c

You may have other options for tracking memory, for example:

  • Valgrind with an array to track memory usage over time. You can even generate png output graphics
  • Nested libraries. You can find several libraries that you can use LD_PRELOAD=thelib.so ./yourprogram and they will LD_PRELOAD=thelib.so ./yourprogram some statistics like jemalloc

(Please note, please accept some answers to your question!)

0
source

you can try something like this ... I highly recommend using this for debugging purposes only!

 #define MAXMEMBLOCKS 10000 typedef struct { int size; void* ptr; } memblock; typedef struct { int totalSize; int current; memblock memblocks[MAXMEMBLOCKS]; } currentMemory; currentMemory mem; void *MyMalloc(int size) { if (mem.current < MAXMEMBLOCKS) { mem.current += size; mem.memblocks[mem.current].size = size; mem.memblocks[mem.current].ptr = malloc(size); return mem.memblocks[mem.current++].ptr; } else { // you needed more memblocks than estimated return NULL; } }; int MyFree(void *pointer) { int i; for (i = 0; i < mem.current; i++) { if (mem.memblocks[i].ptr == pointer) { mem.totalSize -= mem.memblocks[i].size; free(mem.memblocks[i].ptr); mem.current--; return 0; } } // you tried to free a block wich hasn't been allocated through MyMalloc() return -1; } 
0
source

Source: https://habr.com/ru/post/1382921/


All Articles