Re: VirtualAlloc()
"Chris Kushnir" wrote:
VirtualAlloc doesn't allocate global memory; control
over physical storage, which VirtualAlloc provides is
indirect and close to none.
Calling VirtualAlloc(MEM_COMMIT) does give you control
over physcial memory.
If you have a system with 512MB physcial memory and 256MB
swap file, and commit 128MB via VirtualAlloc you have
removed 128MB of the total 768MB 'memory' available to all
processes. At any given point in time the OS decides
where the pages for your 128MB reside (memory of page
file, unless allocated using AWE), but you have taken
control of some physcial memory storage.
Yes, you're right here. Without any doubt, VirtualAlloc will
affect availability of system resources. What I'm trying to
say is that caller of VirtualAlloc has little control over
what, when and where will be used by the system. So, unless
you reserve really big chanks of memory with following
commit/decommit jugglery, VirtualAlloc doesn't offer much
more than regular `new' can offer. (I put memory protection
attributes aside for a while.)
The only reason i replied at all was that your relpy
seemed to imply that the OP had no reason to use
VirtualAlloc and that there was no way to control physical
memory. Your reply came accross as authoritative. I
wanted the OP to see a contrary, or at least moderated,
view, hopefully to get them to research the topic more on
their own.
I apologize to you and OP if my answer was too harsh. It
wasn't my intent. What stroke me is that VirtualAlloc as it
used in original post:
VirtualAlloc(NULL, uSize, MEM_COMMIT, PAGE_READWRITE);
doesn't provide any benefits over simple plain `new'. When
it's used like that (MEM_COMMIT, PAGE_READWRITE), then
flexibility and control of VirtualAlloc are not utilized. In
that case, there is no reason to use VirtualAlloc instead of
`new', no matter what is the size of allocated chunk.
Moreover, VirtualAlloc can be actually counterproductive in
this situation, because each call ends up as real
allocation/deallocation of memory. Usage of `new', or
`malloc', or Heap* functions may diminish the burden of
allocations by using preallocated memory, as you correctly
pointed on other post.
Alex