Note that there are some explanatory texts on larger screens.

plurals
  1. POValgrind understanding bytes allocated increase in heap summary?
    primarykey
    data
    text
    <p>I have been looking at debugging the memory usage in a forking TCP server. I think I am doing pretty well, I just can't seem to find information on the 'bytes allocated' number in the 'heap summary'. This number seems to be ever increasing the longer my server runs:</p> <pre><code>==27526== ==27526== HEAP SUMMARY: ==27526== in use at exit: 0 bytes in 0 blocks ==27526== total heap usage: 113 allocs, 113 frees, 283,043 bytes allocated ==27526== ==27526== All heap blocks were freed -- no leaks are possible ==27526== ==27526== For counts of detected and suppressed errors, rerun with: -v ==27526== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) ==27528== ==27528== HEAP SUMMARY: ==27528== in use at exit: 0 bytes in 0 blocks ==27528== total heap usage: 120 allocs, 120 frees, 300,808 bytes allocated ==27528== ==27528== All heap blocks were freed -- no leaks are possible ==27528== ==27528== For counts of detected and suppressed errors, rerun with: -v ==27528== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) ==27537== ==27537== HEAP SUMMARY: ==27537== in use at exit: 0 bytes in 0 blocks ==27537== total heap usage: 127 allocs, 127 frees, 318,573 bytes allocated ==27537== ==27537== All heap blocks were freed -- no leaks are possible ==27537== ==27537== For counts of detected and suppressed errors, rerun with: -v ==27537== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) </code></pre> <p>Although Valgrind reports allocs and free are equal and no leaks are possible I do not trust the allocated bytes increasing. </p> <p>So : if the bytes allocated keeps increasing does this mean I have to deallocate from the heap somewhere even if Valgrind reports no leaks are possible?</p> <p>Thanks!</p> <p>EDIT: With Gordon Bailey's answer and the other tips I am still a bit weary. Wrote this little app:</p> <pre><code>/* client.c */ #include &lt;stdio.h&gt; void child_func(int childnum); int main(int argc, char *argv[]) { int nchildren = 1; int pid; int x; if (argc &gt; 1) { nchildren = atoi(argv[1]); } for (x = 0; x &lt; nchildren; x++) { if ((pid = fork()) == 0) { child_func(x + 1); exit(0); } } wait(NULL); return 0; } void child_func(int childnum) { int i; for (i = 0; i &lt; 1000; i++) { free(malloc(1)); } sleep(1); } </code></pre> <p>When I run this the Valgrind output is:</p> <pre><code>==28245== HEAP SUMMARY: ==28245== in use at exit: 0 bytes in 0 blocks ==28245== total heap usage: 1,000 allocs, 1,000 frees, 1,000 bytes allocated ==28245== ==28245== All heap blocks were freed -- no leaks are possible ==28245== ==28245== For counts of detected and suppressed errors, rerun with: -v ==28245== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) ==28246== HEAP SUMMARY: ==28246== in use at exit: 0 bytes in 0 blocks ==28246== total heap usage: 1,000 allocs, 1,000 frees, 1,000 bytes allocated ==28246== ==28246== All heap blocks were freed -- no leaks are possible </code></pre> <p>So it looks like all memory is cleared on the heap and is definitely different from the output of my app.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload