Channels ▼

Al Williams

Dr. Dobb's Bloggers

Memory Pages

September 09, 2011

Last time I talked about how, under Linux, a malloc call (or things based on malloc calls) might appear to succeed when they really should have failed. This leads to nasty runtime exceptions when you try to actually use the memory you requested.

If that seemed complicated, it turns out to actually be worse. The malloc and related calls have to get their memory from somewhere, and on modern systems that can lead to an entirely different set of problems.

Traditionally, a program had an sbrk value that was the "high water mark" for its code and static data. Everything from the sbrk location to the top of memory was fair game (remember, this is address space or logical addresses; no physical pages are used until you really try to do something). The system would typically set the stack to the top of memory and let it grow down. The heap (where malloc and friends get memory) would start at sbrk and grow up. You didn't want these two regions to overlap, of course.

In these classic systems, allocating memory would cause the runtime library to increase the sbrk value by some fixed amount, and malloc then returns some part of that memory to the calling program. Future malloc calls will use more of the block until there is no more. At that point, the malloc call would further increase sbrk.

Today, things are often more complicated. The GNU library (common on desktop systems, but not universally used on embedded systems) has several tunable parameters (see http://is.gd/Rux3nM). The aim of most of these parameters is to avoid a common problem.

Suppose your malloc implementation grows the heap 1MB at a time. You merrily allocate 1MB and the library adds another 1MB to the heap (for a total of 2MB). In an ideal world, your program might release all of the memory it is using in that second megabyte and the library could return it to the operating system for use by other programs (or at least, to keep it from swapping out; see last week's discussion about swapping dirty data pages).

How likely is that, though? If you have even a small allocation in that upper MB it will stop the library from returning all that memory to the operating system. The library parameters set how much memory malloc will try to grow (or shrink) the heap when necessary. It also defines M_MMAP_THRESHOLD. Any allocation above this size will not be put in the heap at all. At least, not in the classic sense. If you ask for a block of memory larger than this threshold, the system will allocate its own block big enough to fit the request using the mmap system call. When you free this memory block it will always go back to the operating system. This is mildly expensive in terms of speed, so you don't want that threshold to be, say, one byte.

You can set these parameters using the library call mallopt. Changing them can make allocations faster or friendlier to the operating system, depending on your allocation patterns.

Notice that the heap allocations are still broken up (invisibly) into pages (normally 4K in length). The operating system won't find physical pages until you actually use a page. Assume a 4K page size and consider the following call:

char *p=(char *)malloc(4096*10);

This nominally allocated 10 pages from the heap (and that may or may not cause the heap to grow). Of course, if the threshold is low enough, it could result in an mmap allocation, too. You don't know and usually don't care.

However, the kernel won't actually back those pages up until you hit them as the code from last week demonstrated. So consider:

p[0]='A';
p[9000]='Z';

This code will cause two real pages to materialize, but the other eight pages are still theoretical (until you touch them). That's why a comment in last week's code:

#include <stdio.h>
#include <stdlib.h>
 
#define TOUCH 1       // 0 to not touch memory, 1 to touch
#define SIZE 1024     // bytes to allocate at once (assume <1 page)
 
int main(int argc, char *argv)
  {
  unsigned i;
  char *p;
  do {
      p=(char *)malloc(SIZE);
      if (p) printf("%u\r", ++i);
      else
        {
          printf("Malloc failed after %u calls\n",i);
          break;
         }
#if TOUCH
      *p=0xAA;
#endif
      } while (p);
   return 0;
  }

mentions the test size is assumed to be less than one page in size.

Still not complicated enough? There is another wrinkle. The kernel normally has an OOM (Out of Memory) killer. When the kernel decides it is low on memory, the OOM killer swings into action and uses some heuristics to decide what process it should kill. This is usually bad when your X server or your web server gets the axe.

However, Android — based, of course, on Linux — uses a sophisticated OOM mechanism that is more suitable for small devices. It categorizes processes into several distinct groups and when memory starts getting low, the lowest priority processes get the axe. This is somewhat more predictable than the standard kernel's algorithm.

I'll talk more about the OOM killers next time. But the point this week is this: Know your library. It is easy to treat abstractions like malloc as "magic" — especially when you use them via another abstraction (like Java, for example). However, understanding what's happening is often the difference between a system that works and performs well and one that doesn't.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video