{"id":456,"date":"2009-09-28T10:58:30","date_gmt":"2009-09-28T09:58:30","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=456"},"modified":"2026-03-26T16:31:05","modified_gmt":"2026-03-26T20:31:05","slug":"mmu-and-heap-contiguity","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2009\/09\/28\/mmu-and-heap-contiguity\/","title":{"rendered":"MMU and heap contiguity"},"content":{"rendered":"<p>A while ago I did a Webinar looking at <a href=\"http:\/\/www.mentor.com\/products\/embedded_software\/multimedia\/c-plus-web-seminar\" target=\"_blank\" rel=\"noopener noreferrer\">C++ for embedded applications<\/a>. It was well attended and well received and there were lots of questions and comments, which is always very satisfying. I observed that a number of people were specifically interested in dynamic memory allocation in C and C++ and the challenges that are presented to embedded and real time programmers. So I developed a further Webinar specifically looking at <a href=\"http:\/\/www.mentor.com\/products\/embedded_software\/multimedia\/dynamic-memory-allocation-webseminar\" target=\"_blank\" rel=\"noopener noreferrer\">dynamic memory allocation and fragmentation<\/a>. Both of these were recorded and available as archives to view on demand.<\/p>\n<p>I was interested in investigating how to avoid dynamic memory [heap] fragmentation &#8230;<\/p>\n<p><!--more-->The issue comes about because the arbitrary allocation and deallocation of chunks of memory can result in something of a mess. The free space is not contiguous &#8211; i.e. it is fragmented &#8211; and an allocation can fail, even though enough free space is actually available.<\/p>\n<figure id=\"attachment_458\" aria-describedby=\"caption-attachment-458\" style=\"width: 236px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-458\" src=\"https:\/\/blogs.mentor.com\/colinwalls\/files\/2009\/09\/heap.jpg\" alt=\"Fragmented heap\" width=\"236\" height=\"501\" \/><figcaption id=\"caption-attachment-458\" class=\"wp-caption-text\">Fragmented heap<\/figcaption><\/figure>\n<p>In this example, there is 6K of free memory, but a request for a 4K block would fail.<br \/>\nAn obvious solution would be to de-fragment the memory, but this is not a possibility for 2 reasons:<\/p>\n<ol>\n<li>The application code uses pointers to the allocated memory; changing the address of allocated chunks would break that code. This is possible in languages like Java and Visual Basic, where there are no direct pointers.<\/li>\n<li>Such &#8220;garbage collection&#8221; would be intrinsically non-deterministic, which would not be acceptable in a real time system.<\/li>\n<\/ol>\n<p>The only realistic solution, that I could propose, is to use the block [partition] memory allocation provided by most real time operating systems. A series of partition pools are created, with partition sizes in a geometric series [e.g. 32, 64, 128, 256 bytes]. Allocations are then made from the pool that has partitions [just] large enough. This solves the problem, as a partition pool cannot get fragmented and its usage is most likely to be highly deterministic.<\/p>\n<p>But I had another idea: would creative use of a memory management unit [MMU] be another way to apparently de-fragment the heap? The concept is simple: the MMU manages the free space and maps it so that it always <em>appears<\/em> to be contiguous. Of course, when memory is allocated, the MMU would also need to maintain the apparent contiguity of the allocated chunk.<\/p>\n<p>To be frank, my experience in the use of MMUs is limited. So my question is: would this actually work? If you have any insight, please comment or <a href=\"mailto:colin_walls@mentor.com\">email<\/a> me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A while ago I did a Webinar looking at C++ for embedded applications. It was well attended and well received&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,334,300,335,336,337],"industry":[],"product":[],"coauthors":[],"class_list":["post-456","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-dynamic-memory","tag-embedded-software","tag-heap","tag-memory-management-unit","tag-mmu"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=456"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/456\/revisions"}],"predecessor-version":[{"id":9792,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/456\/revisions\/9792"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=456"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=456"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=456"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}