Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to completely avoid heap fragmentation?

For example, if deallocations of dynamic memory are always done in opposite direction to allocations. In that case, is it guaranteed that heap will not become fragmented?

And from theoretical point of view: Does there exist some realistic way for a nontrivial application to manage memory to completely avoid heap fragmentation? (After each atomic change in heap, is heap still unfragmented?)

like image 354
user3123061 Avatar asked Feb 21 '14 22:02

user3123061


1 Answers

Exist some realistical way for nontrivial application how to manage memory to completely avoid heap fragmentation

Yes. Allocate everything on the stack. This may seem like a counsel of perfection but I've done it in non-trivial programs.

EDIT For absolute clarity and avoidance of doubt, I said 'the stack'. By that I clearly mean automatic storage, i.e. the local variable stack, not any stack class. Some of the comments about this are basically obtuse.

like image 92
user207421 Avatar answered Nov 16 '22 00:11

user207421