Re: VirtualAlloc or LocalAlloc? Plz Help
- From: Gakuren Ko <GakurenKo@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 18 Sep 2007 22:22:00 -0700
As my understanding, which memory allocation API should be used depends on
the size you need to allocate and how you use the memory you have allocated.
LocalAlloc() and malloc() allocate memory from process local heap which
resides within 32MB process slot, while VirtualAlloc() allocate memory from
virtual space which could be process's free virtual space (in 32MB prcess
slot) or shared memory (which is outside 32MB process slot).
Although LocalAlloc() and malloc() normally allocate memory space from local
heap, when the allocation size is larger than 96KB, it internally calls
VirtualAlloc() to allocate memory from virtual space.
You should take care to use local heap because it may bring fragmentation
impact. For example, if you frequently alloc/free memory for different size
objects with LocalAlloc() or malloc(), it will easily result in out-of-memory
status, because every allocated memory would not be freed imediatly until the
process is finished
From this point of view, local heap is suitable for small fixed-sizeobjects. For detailed reason, please refer to the following help doc
http://msdn2.microsoft.com/en-us/library/aa450550.aspx
On the other hand, you should also take care when you use VirtualAlloc()
becuase it works on 64KB boundary, otherwise it may leave "holes" in the
memory you have allocated but never used or bring out-of-memory status. For
this, there are some explanation and sample code in the following doc.(look
at "Regions vs. Pages" section in this artical )
http://www.microsoft.com/mspress/books/sampchap/6535.aspx
Hope the above info is helpful for you.
Regards
Gakuren Ko
"developer28" wrote:
On Sep 18, 3:00 am, "Steve Maillet \(MVP\)".
<nos...@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
Can anyone tell me which out of the following two API's is better to
be used for memory allocation in Windows CE 5.0 and why?
VirtualAlloc() or LocalAlloc().
Most likely, neither. use malloc() or in c++ operator new(). Don't much
around with those low level APIs unless you absolutely have to.
Steve Maillethttp://www.EmbeddedFusion.com
Hi Steve,
Thanks for the prompt reply.
Actually the exact picture is that we are working on a high end
navigation device with the product having about 5 applications of
sizes 9 MB , 5 MB , 5 MB , 5 MB ,6 MB approx. Apart from this, we
shall have some Dll's of a total size around 10MB.
Now inside these applications which integrate together to form the
product, we need to allocate memory.
I know that there is this 32 MB limit on each process space.
In such a condition which one is the one which should be used for
memory allocation?
LocalAlloc() , VirtualAlloc() or malloc() ?
Do all of these allocate memory from the same 32 MB process slot?
Regards,
Aman.
- Follow-Ups:
- Re: VirtualAlloc or LocalAlloc? Plz Help
- From: developer28
- Re: VirtualAlloc or LocalAlloc? Plz Help
- References:
- VirtualAlloc or LocalAlloc? Plz Help
- From: developer28
- Re: VirtualAlloc or LocalAlloc? Plz Help
- From: Steve Maillet \(MVP\)
- Re: VirtualAlloc or LocalAlloc? Plz Help
- From: developer28
- VirtualAlloc or LocalAlloc? Plz Help
- Prev by Date: Re: Error invalid signature??
- Next by Date: Re: USB mass storage
- Previous by thread: Re: VirtualAlloc or LocalAlloc? Plz Help
- Next by thread: Re: VirtualAlloc or LocalAlloc? Plz Help
- Index(es):
Relevant Pages
|