I have a problem with free () in the structure in my C program. When I look at / proc // statm before and after free, it does not seem to decrease. Am I using free () incorrectly in this case, or am I reading / proc // statm incorrectly?
Here is a test case that gives the problem:
struct mystruct { unsigned int arr[10000]; }; void mem() { char buf[30]; snprintf(buf, 30, "/proc/%u/statm", (unsigned)getpid()); FILE* pf = fopen(buf, "r"); if (pf) { unsigned size; // total program size unsigned resident;// resident set size unsigned share;// shared pages unsigned text;// text (code) unsigned lib;// library unsigned data;// data/stack unsigned dt;// dirty pages (unused in Linux 2.6) fscanf(pf, "%u %u %u %u %u %u", &size, &resident, &share, &text, &lib, &data); printf("Memory usage: Data = %d\n", data*sysconf(_SC_PAGESIZE)); } fclose(pf); } int main(int argc, char **argv) { mem(); struct mystruct *foo = (struct mystruct *)malloc(sizeof(struct mystruct)); mem(); free(foo); mem(); }
Output:
Memory usage: Data = 278528 Memory usage: Data = 282624 Memory usage: Data = 282624
When I expect it to be:
Memory usage: Data = 278528 Memory usage: Data = 282624 Memory usage: Data = 278528
I did a similar test with malloc'ing (char *), then released it and it works fine. Is there anything special about structures?
c memory-management struct malloc free
oprimus
source share