{"id":5114,"date":"2017-01-05T20:55:15","date_gmt":"2017-01-05T20:55:15","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/strategies-for-the-allocation-of-memory\/"},"modified":"2023-09-18T15:50:06","modified_gmt":"2023-09-18T15:50:06","slug":"strategies-for-the-allocation-of-memory","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/strategies-for-the-allocation-of-memory\/","title":{"rendered":"Strategies for the Allocation of Memory"},"content":{"rendered":"<p>There are a lot of different strategies for allocating memory. Programming languages like Python or Java request their memory from the heap at runtime. Of course, C or C++ also has a heap but prefers the stack. But these are by far not so only strategies for allocating memory. You can preallocate memory at the program&#8217;s start time as a fixed block or a pool of memory blocks. This preallocated memory can afterward be used at the runtime of your program. But the critical question is: What are the pros and cons of the various strategies to allocate memory?<\/p>\n<p><!--more--><\/p>\n<p>At first, I want to present four typical memory strategies.<\/p>\n<h2>Strategies for the allocation of memory<\/h2>\n<h3>Dynamic allocation<\/h3>\n<p>Dynamic allocation, also called variable allocation, is known to each programmer. Operations like <span style=\"font-family: Courier New,Courier,monospace;\">new <\/span>in C++ or <span style=\"font-family: Courier New,Courier,monospace;\">malloc <\/span>in C request the memory when needed. On the contrary, calls like <span style=\"font-family: Courier New,Courier,monospace;\">delete <\/span>in C++ or <span style=\"font-family: Courier New,Courier,monospace;\">free <\/span>in C release the memory when not needed anymore.<\/p>\n<div style=\"background: #ffffff none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"> <span style=\"color: #2b91af;\">int<\/span>* myHeapInt= <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">int<\/span>(5);\n <span style=\"color: #0000ff;\">delete<\/span> myHeapInt;\n\n<\/pre>\n<\/div>\n<p>The subtle difference in the implementation is whether the memory will be automatically released. Languages like Java or Python know general garbage collection, C++ or C is contrary not. (Only for clarification: That is not totally true because we have the <a href=\"http:\/\/www.hboehm.info\">Boehm<\/a>&#8211;<a href=\"http:\/\/www.cs.cornell.edu\/annual_report\/00-01\/bios.htm#demers\">Demers<\/a>&#8211;<a href=\"http:\/\/www-sul.stanford.edu\/weiser\/\">Weiser<\/a> conservative garbage collector that<span id=\"transmark\"> <\/span>can be used as a garbage collecting replacement for C <tt>malloc<\/tt> or C++ <tt>new<\/tt>. Here are the details:&nbsp;<a href=\"https:\/\/www.hboehm.info\/gc\/\">https:\/\/www.hboehm.info\/gc\/<\/a>).<\/p>\n<p>Dynamic memory management has a lot of pros. You can automatically adjust the memory management to your needs. The cons are that there is always the danger of memory fragmentation. In addition, a memory allocation can fail or take too much time. Therefore, many reasons speak against dynamic memory allocation in highly safety-critical software that requires deterministic timing behavior.<\/p>\n<p>Smart Pointers in C++ manage their dynamic memory by objects on the stack.<\/p>\n<h3>Stack allocation<\/h3>\n<p>Stack allocation is also called memory discard. The critical idea of stack allocation is that the objects are created in a temporary scope and are immediately freed if the objects go out of scope. Therefore, the C++ runtime takes care of the lifetime of the objects. The scope is typically areas like functions, objects, or loops. But you can also create artificial scopes with curly braces. &nbsp;<\/p>\n<div style=\"background: #ffffff none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\">{\n  <span style=\"color: #2b91af;\">int<\/span> myStackInt(5);\n}\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>A beautiful example of stack allocation is the smart pointers <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> and <span style=\"font-family: courier new,courier;\">std::shared_ptr.<\/span> Both are created on the stack to care for objects created on the heap.<\/p>\n<p>But what are the benefits of stack allocation? At first, memory management is easy because the C++ runtime manages it automatically. In addition, the timing behavior of memory allocation on the stack is deterministic. But there is a big disadvantage. On the one hand, the stack is smaller than the heap; on the other hand, the objects should&nbsp;often outlive their scope, and C++ supports no dynamic memory allocation on the stack.<\/p>\n<h3>Static allocation<\/h3>\n<p>Most of the time, static allocation is called fixed allocation. The key idea is that at runtime, required memory will be requested at the start time and released at the shutdown time of the program. The general assumption is that you can precalculate the memory requirements at runtime.<\/p>\n<div style=\"background: #ffffff none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #2b91af;\">char<\/span>* memory= <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">char<\/span>[<span style=\"color: #0000ff;\">sizeof<\/span>(Account)];\nAccount* a= <span style=\"color: #0000ff;\">new<\/span>(memory) Account;\n\n<span style=\"color: #2b91af;\">char<\/span>* memory2= <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">char<\/span>[5*<span style=\"color: #0000ff;\">sizeof<\/span>(Account)];\nAccount* b= <span style=\"color: #0000ff;\">new<\/span>(memory2) Account[5];\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Objects a and b in this example are constructed in the preallocated memory of <span style=\"font-family: Courier New,Courier,monospace;\">memory <\/span>and <span style=\"font-family: Courier New,Courier,monospace;\">memory2<\/span>.<\/p>\n<p>What are the pros and cons of static memory allocation? In hard real-time driven applications, in which the timing behavior of dynamic memory is no option, static allocation is often used. In addition, the fragmentation of memory is minimal. But of course, there are pros and cons. Often it is not possible to&nbsp;precalculate the application&#8217;s memory requirements upfront. Sometimes your application requires extraordinary memory at runtime. That may be an argument against static memory allocation. Of course, the start time of your program is longer.<\/p>\n<h3>Memory pool<\/h3>\n<p>Memory pool, also called pooled allocation, combines the predictability of static memory allocation with the flexibility of dynamic memory allocation. Similar to the static allocation, you request the memory at the start time of&nbsp;your program. In opposite to the static allocation, you request a pool of objects. At runtime, the application uses this pool of objects and returns them to the pool. If you have more the one typical size of object, it will make sense to create more than one memory pool.<\/p>\n<p>What are the pros and cons? The pros are quite similar to the pros of static memory allocation. On the one hand, there is no memory fragmentation; on the other hand, you have predictable timing behavior of the memory allocation and deallocation. But there is one significant advantage of memory pool allocation to static allocation. You have more flexibility. This is for two reasons. The pools can have different sizes, and you can return memory to the pool. The cons of memory pools are that this technique is quite challenging to implement.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Sometimes, I envy Python or Java. They use dynamic memory allocation combined with garbage collection, which is fine. All? All four presented techniques are used in C or C++ and offer a lot. In the <a href=\"https:\/\/www.modernescpp.com\/index.php\/pros-and-cons-of-the-various-memory-management-strategies\">next post<\/a>, I will closely examine the difference between the four techniques. I&#8217;m particularly interested in predictability, scalability, internal and external fragmentation, and memory exhaustion.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a lot of different strategies for allocating memory. Programming languages like Python or Java request their memory from the heap at runtime. Of course, C or C++ also has a heap but prefers the stack. But these are by far not so only strategies for allocating memory. You can preallocate memory at the [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":8270,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[498,493],"class_list":["post-5114","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-memory","tag-new-delete"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5114"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5114\/revisions"}],"predecessor-version":[{"id":8271,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5114\/revisions\/8271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/8270"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}