Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 04 Apr 2003 17:08:56 -0800
From:      Terry Lambert <>
To:        Sreekanth <>
Subject:   Re: Mmap and malloc
Message-ID:  <>
References:  <000001c2fa20$2ee5ba20$ae28a8c0@SREELAPTOP>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
Note: For really basic questions, you should probably post questions
like these to .

This is the third time in 24 hours you have asked this question.
The first two times, you asked it one hour apart.  The last time
you asked it on -arch, which is definitely the wrong list to ask
on; it is a architectural discussion list.

I will answer your question here on -hackers, but realize that
asking the same thing repeatedly in short intervals is likely
to get you ignored in the future.

Sreekanth wrote:
>     I have a situation i am not sure i understand.I am running a 4.6.2
> FreeBSD system with a Dual Xeon board with 2GB of Physical memory.

Not relevent.  Both mmap() and malloc() operate on vitural address
spaces, and are user space, and so are limited by your UVA size.

Worst case, mmap() or malloc() will give you more memory than you
actually have physical memory for, and then start swapping.  That's
what "virtual memory" is supposed to do.

> I have a test program which continuoulsy calls mmap(with larger size
> each time) till it receives MAP_FAILED return value. I then call
> malloc(with bigger memory size each time) continuosly till it
> fails.Here are my results.
> Max mem mapped = 714 MB
> Max mem alloc = 308 MB
> My MAXDMSIZ is 1200*1024*1024 and KVA_PAGE is 512 (2GB)
> If i swap the order in wich mmap and malloc are called, then i get the
> following result.
> Max mem alloc = 1200MB
> Max mem mapped = 711MB
> My questions are as follows
> 1) Why is the difference between two approaches. ?

Malloced memory comes from growing the BSS, and mapping anonymous
pages into them.  Mmap'ed memory comes from selecting an address
and mapping pages into it.

There is a specific limit on the number of pages you can mmap(),
which is administratively enforced.  It is both a kernel tuning
option, and an administrative "soft" limit.  Which you are hitting
depends on which is smaller.  The "soft" limit is set via your
login class in /etc/login.conf.

The reason the two approaches give different answers is that you
are probably not specifying a mapping address for the mmap()'ed
memory that you are using.  As a result, malloc() stops giving
you anonymous pages as soon as it hits its head on the first
mapping that you did with mmap().  Specify a location for the
mappings, and the problem will "go away".

Specifically, add a "debug" option to your test program, and then
print out as "0x%08x" each of the start and end addresses of the
memory regions returned by mmap() and malloc() in both cases.

The overlap collision should become very obvious to you very

> 2) What exactly is the relationship between mmap malloc and KVA_PAGES.

4G - KVA_PAGES = available user process address space.  Or to put it
another way:

	UVA + KVA = 4G

That's because 2^32 = 4G, and you are running on a 32 bit architecture.

Both mmap() and malloc() allocate portions of the available UVA.

> 3) I found that the Maximum mmapped memory = 4GB - KVA_PAGES*PAGE_SIZE
> - MAXDSIZ - 134
> What is the 134 value..? It is very consistent for me for various
> values of KVA_PAGES and MAXDSIZ, so i am ruling it being just a
> coincidence.

Overhead for the kernel.  If you want to understand where every byte
is going, you are going to have to understand everything in locore.s,
machdep.c, and the boot loader.  Frankly, it's not worth the amount
of effort required to do this (I say this having done it once), because
the information changes from release to release.

-- Terry

Want to link to this message? Use this URL: <>