Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VirtualAlloc v.s. malloc

960 views
Skip to first unread message

George

unread,
Nov 3, 2007, 3:21:00 AM11/3/07
to
Hello everyone,


Any practical experiences of VirtualAlloc v.s. malloc? From MSDN, I can not
find any comparisons between the two methods.


thanks in advance,
George

Alex Blekhman

unread,
Nov 3, 2007, 6:15:28 AM11/3/07
to
"George" wrote:
> Any practical experiences of VirtualAlloc v.s. malloc?
> From MSDN, I can not
> find any comparisons between the two methods.

`VirtualAlloc' is Win32 API function. `malloc' is CRT
function (which may be implemented on top of
`VirtualAlloc'). They both eventually will return a memory
chunk that you can use. However, they belong to different
worlds.

`VirtualAlloc' is low level system function, which allows
you to fine tune memory allocations. Most of the
applications don't have such requirement. They can happily
use portable CRT function `malloc', which encapsulates
underlying OS intricacies.

Alex

George

unread,
Nov 3, 2007, 7:40:01 AM11/3/07
to
Thanks Alex,


So I can understand that VirtualAlloc is more efficient than malloc, right?
Since malloc is implemented on top of VirtualAlloc.


regards,
George

Alexander Grigoriev

unread,
Nov 3, 2007, 9:17:13 AM11/3/07
to
VirtualAlloc granularity is 64K. This means each call consumes at least 64K
of address space. Not much effective for small allocations, you see.

Closest analog of malloc is HeapAlloc


"George" <Geo...@discussions.microsoft.com> wrote in message
news:236759B3-A474-41FD...@microsoft.com...

Scott McPhillips [MVP]

unread,
Nov 3, 2007, 9:26:09 AM11/3/07
to
"George" <Geo...@discussions.microsoft.com> wrote in message
news:236759B3-A474-41FD...@microsoft.com...
> Thanks Alex,
>
>
> So I can understand that VirtualAlloc is more efficient than malloc,
> right?
> Since malloc is implemented on top of VirtualAlloc.


That would not be a good conclusion. VirtualAlloc deals in pages (4K or
larger) only. Ask for 12 bytes and you waste 4K bytes.

malloc is a sub-allocator that can efficiently deal with a program's
requirements for rapidly allocating and freeing blocks of any desired size.
It gets some memory from the OS initially but then it typically does not
need to call the OS for routine alloc/free after that. Stick with the
language features (malloc/new) unless you must have something OS-specific.

--
Scott McPhillips [VC++ MVP]

George

unread,
Nov 3, 2007, 9:41:00 AM11/3/07
to
Thanks Alexander,


I have read some documents in MSDN about VirtualAlloc API and HeapAlloc API,
but I am still confused about what is the differences between VirtualAlloc
and HeapAlloc? Seems they allocate memory from different places?


regards,
George

Alex Blekhman

unread,
Nov 3, 2007, 11:08:11 AM11/3/07
to
"George" wrote:
> I have read some documents in MSDN about VirtualAlloc API
> and HeapAlloc API,
> but I am still confused about what is the differences
> between VirtualAlloc
> and HeapAlloc? Seems they allocate memory from different
> places?

I'd suggest you to follow Scott's advice: Don't use low
level OS calls unless you're absolutely required to.

`VirtualAlloc' allocates memory directly from OS. That's why
it has OS specific limitations like allocating in 4K chunks,
address alignment etc.

`HeapAlloc' calls `VirtualAlloc' internally. It returns you
aleady pre-allocated memory from process' heap. It is
somewhat similar to CRT's `malloc'.

If you want to understand the differences, then you should
read at least following articles:

"Managing Virtual Memory in Win32"
http://msdn2.microsoft.com/en-us/library/ms810627.aspx

"Managing Heap Memory in Win32"
http://msdn2.microsoft.com/en-us/library/ms810603.aspx

They are quite old, but mostly relevant even today. For
comprehensive material on Win32 memory management I'd
suggest to refer to good book:

"rogramming Applications for Microsoft Windows" by Jeffrey
Richter
http://www.microsoft.com/MSPress/books/2345.aspx


HTH
Alex

George

unread,
Nov 4, 2007, 5:00:02 AM11/4/07
to
Thanks Alex,


They are great learning resources and I have read them for 2 hours. I have
found almost all of my questions are answered except this one,

when should be use VirtualAlloc and when should we use HeapAlloc (the
specific advantages and disadvantages of the two functions), could you share
some experiences please?


regards,
George

George

unread,
Nov 4, 2007, 5:01:00 AM11/4/07
to
Thanks Scott,


I am clear with VirtualAlloc and malloc now. Do you have any experiences of
using VirtualAlloc and HeapAlloc? Could you share some experiences when
should we use VirtualAlloc and when should we use HeapAlloc please?


regards,
George

Nathan Mates

unread,
Nov 4, 2007, 9:27:22 AM11/4/07
to
In article <43D41846-2B89-4F12...@microsoft.com>,

=?Utf-8?B?R2Vvcmdl?= <Geo...@discussions.microsoft.com> wrote:
>when should be use VirtualAlloc and when should we use HeapAlloc
>(the specific advantages and disadvantages of the two functions),
>could you share some experiences please?

My experience: please don't try and write your own memory manager.
Others with a lot more time and experience have done it a *lot*
better. The Microsoft allocators (malloc/new) are only adequate in my
book, but they're almost certainly going to be faster and a *LOT* more
bugfree than your first dozen implementations. Trust me, you do NOT
want to be debugging your memory manager while also trying to debug
your code. My bottom line: if you have to ask about low-level memory
managers, you're not ready to write code that implements them.

The replacement memory manager that I've found to be quite
portable, tunable, and beats the default Microsoft one is dlmalloc --
see http://g.oswego.edu/dl/html/malloc.html for descriptions of how it
works, source code (it's public domain), and more.

Nathan Mates
--
<*> Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein

Alexander Grigoriev

unread,
Nov 4, 2007, 10:02:07 AM11/4/07
to
VirtualAlloc sould be used only:

1. I you need allocation aligned on page boundary (usually for non-buffered
I/O).
2. You don't want to commit full amount of memory (sparse allocation).
3. You want to allocate very large amount at once. There is limit on a
single allocation in HeapAlloc.

"George" <Geo...@discussions.microsoft.com> wrote in message

news:43D41846-2B89-4F12...@microsoft.com...

George

unread,
Nov 5, 2007, 12:07:03 AM11/5/07
to
Thanks Nathan,


Practical reply and good open source resouces!


regards,
George

George

unread,
Nov 5, 2007, 12:10:02 AM11/5/07
to
Thanks Alexander,


Very good advice, and after reading some documents, I think HeapAlloc is
based on VirtualAlloc (different layers of API), and for small amount of
memory, like tens of bytes, HeapAlloc should be better.


regards,
George

George

unread,
Nov 5, 2007, 12:38:01 AM11/5/07
to
Hi Nathan,


I have tried to download the malloc.c from the open source project, but
always fail to download. I am interested to learn, could you send me the
source code please?

My email address is George4...@yahoo.com. Thanks.


regards,
George

Alexander Nickolov

unread,
Nov 5, 2007, 12:44:39 PM11/5/07
to
I'd add:

4. If you need to allocate memory to write executable code in,
like ATL's windowing thunks.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Alexander Grigoriev" <al...@earthlink.net> wrote in message
news:eeBSxOvH...@TK2MSFTNGP05.phx.gbl...

George

unread,
Nov 6, 2007, 1:40:01 AM11/6/07
to
I do not have that requirement, but thanks anyway Alexander!


regards,
George

0 new messages