According to Windows core programming: The X86 operating system provides a maximum of 4 g of virtual memory per program, where 2G of virtual memory is provided to the system (specifically for what to investigate), and 2G of memory is left to the user. What's this 2G of memory going to do? First, you introduce the program memory of the 5 regions: Stack area, heap area, global zone (static zone), text constant area, program code area. These 5 regions can only poor dwelling in this 2G of memory, so we should cherish this 2G memory! Most programmers are accustomed to using new when applying for heaps, because it's easy to use, but a lot of programmers don't know how much memory I can get! It's up to you to see how much memory you have in addition to the remaining 4 partitions in the heap area, in short, new memory cannot exceed 2G. When your program 5 large partition occupies a total of 2G of memory, you also need new memory, this time the system will not tell you the memory is not enough, but the program directly collapsed! So many programs because the new memory forget to release, over time, the total user memory over 2G, the program will be directly collapsed. At this point you will certainly ask, how should I solve this problem? Since we can't count how much user memory is being used, how much more can we get new? Fortunately, Microsoft has provided us with a good allocation heap method, which is to allocate the heap with HeapAlloc. You must call HeapCreate to create the secondary stack before calling HeapAlloc, and remember that heapcreate corresponds to Heapfree,heapalloc and HeapDestroy one by one, just like new and delete. HeapAlloc not only has new features, but HeapAlloc will return an empty pointer when 2G memory is used to tell you that the memory is exhausted! HeapAlloc also has a very prominent bit, that is, it applies memory and frees up memory very, very fast, much faster than new.
Here to tell you the order of HeapAlloc calls, Heapalloc->new->virtualalloc->malloc,malloc is the C language application memory method, is also the lowest application memory method. At this time people will ask, why HeapAlloc application memory will be faster than new, obviously HeapAlloc called the new method ah? The above is not stressed to everyone, in the call HeapAlloc must first call heapcreate. The original trick on the heapcreate body! Tell us about the distribution of our memory before we tell you about the trick. The available memory of the system is not orderly, but here a piece, where a piece. When you call new to request a piece of memory, the system will find the memory you need in the available memory. It takes time for the system to find the available memory! That's when I started to tell you about this. When you call HeapCreate, the system will create a contiguous amount of memory for you to use. When you call HeapAlloc to request memory, the system allocates the memory you need in the contiguous available memory that you heapcreate create. Obviously, allocating memory in a contiguous amount of memory is definitely faster than allocating memory in a cluttered memory! So everyone in the C + + environment under the project, when applying for memory, we recommend that you use HeapAlloc.
The maximum number of memory programs can be new (2G memory to put the program in the 5 regions, HeapAlloc faster than new, and more than 2G will tell you)