Re: VirtualAlloc or LocalAlloc? Plz Help



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-size
objects. 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.


.



Relevant Pages

  • Re: VirtualAlloc()
    ... large/global memory usage instead of "new". ... allocate physical storage. ... uses `VirtualAlloc'. ...
    (microsoft.public.vc.language)
  • Re: Games made with BBC BASIC for Windows
    ... Memory handling, string handling and data structures are part of BBC ... allocate 256 Mbytes of contiguous virtual address space (actual memory ... used is of course typically very much less) but for some reason Wine ... options of VirtualAlloc, presumably because of some limitation in ...
    (comp.sys.acorn.misc)
  • Re: Sudden decrease in available virtual address space
    ... a loss of available memory in the kernel. ... little application that does the same GlobalMemoryStatus and see if it exhibits the same ... >VirtualAlloc, GlobalAlloc, HeapAlloc, LocalAlloc, GlobalReAlloc, ... All the memory that we allocate ...
    (microsoft.public.vc.mfc)
  • Re: Wince address space allocation
    ... malloc allocates memory from your process' heap which is created by ... the kernel within the RAM memory region specified in the MEMORY ... You do not need to do anything else to allocate cacheable memory for ... returned by virtualalloc, it remains the same as returned by normal ...
    (microsoft.public.windowsce.platbuilder)
  • Re: VirtualAlloc or LocalAlloc? Plz Help
    ... the size you need to allocate and how you use the memory you have allocated. ... virtual space which could be process's free virtual space (in 32MB prcess ... Although LocalAllocand malloc() normally allocate memory space from local ...
    (microsoft.public.windowsce.platbuilder)

Quantcast