Note that there are some explanatory texts on larger screens.

plurals
  1. POHow completely unused memory can be fragmented?
    text
    copied!<p>Following C code example (taken from <a href="http://bugs.python.org/issue19246" rel="nofollow">http://bugs.python.org/issue19246</a>) executed on Windows 7 64-bit, while compiled in 32-bit mode</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;assert.h&gt; int create_huge_linked_list(void **last_item) { int i; void *prev_item = NULL; for(i = sizeof(void *); i &lt; 1000000; i++) { void *new_item = malloc(i); if(new_item == NULL) { break; } *(void **)new_item = prev_item; prev_item = new_item; } *last_item = prev_item; return i; } void free_linked_list(void *last_item) { while(last_item != NULL) { void *prev_item = *(void **)last_item; free(last_item); last_item = prev_item; } } int stress_heap() { void *last_item; int amount = create_huge_linked_list(&amp;last_item); free_linked_list(last_item); return amount; } void stress_twice(void) { int first = stress_heap(); int second = stress_heap(); printf("%i %i %f%%\n", first, second, 100.0 * second / first); } void stress_and_alloc_1_mb() { void *ptr; ptr = malloc(1000000); if(ptr != NULL) { printf("Successfully allocated 1 MB before stress\n"); free(ptr); stress_heap(); ptr = malloc(1000000); if(ptr != NULL) { printf("Successfully allocated 1 MB after stress\n"); free(ptr); } else { printf("Failed to allocate 1 MB after stress\n"); } } else { printf("Failed to allocate 1 MB before stress\n"); } } int main() { stress_and_alloc_1_mb(); stress_twice(); return 0; } </code></pre> <p>Outputs:</p> <pre><code>Successfully allocated 1 MB before stress Failed to allocate 1 MB after stress 64855 64857 100.003084% </code></pre> <p>Result may be interpreted as: after whole memory allocation and then freeing it, memory of the process is fragmented so bad, that there is no chunk with length of 1 MB. However, the stress procedure can be repeated continuously without memory errors.</p> <p>The questions are:</p> <ol> <li>How memory can be fragmented if it is completely unused?</li> <li>How memory fragmentation can be fixed for that process?</li> </ol>
 

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