Question

If I try to allocate memory:

int ramSize = magicLibrary.getRamSize();
assert(ramSize*2 <= pow(2,64-8));//Don't overflow the 64 bit limit (32 on some systems).

int * mainData new int[ramSize*2/sizeof(int)]; //2 times too big. 

Will I get disk swap to fill in the space? If not, how can I use swap?

Was it helpful?

Solution

As long as neither RAM nor Swap-space is completely exhausted, and address space limitations (that is, 2-3GB in 32-bit, more than your ram in a 64-bit system), the system will allow you to allocate memory. If RAM is exhausted, the system will use swap-space.

Sometimes, the OS will also allow "overcommit", which is basically the same as airlines do when they "expect some passengers to not show up", so they sell a few extra tickets for that flight, and worry about "all seats full" when they get to that point. In terms of computers, that means that the OS may well allow you to allocate more memory than there really is available, and the error of "there isn't enough" is solved later by some means (typically by freeing up some memory that is "spare" or "killing some random application"). The reason the OS allows this is that applications quite often allocate large areas of memory that isn't fully used. Memory areas that are filled with zero may also be "merged" as "copy-on-write" memory, such that if you later write to it, it makes a new copy of that memory. This also may mean that if you allocate a large amount of memory, you have to actually write something (other than zero?) to it to make it "in use". Again, this reflects applications typical behaviour of "allocate a large amount of memory, fill it with zero, and only use some of it later" - so the OS tries to "save space" by not having a huge amount of "pages with just zeros in them".

Note that whether the memory is in RAM or Swap is a dynamic criteria - memory is swapped in and out all the time, and both program code and data may be swapped out at any time, and then be brought back when it's needed. It doesn't matter if this is "heap" or some other memory, all memory is pretty much equal in this respect.

It isn't entirely clear what you actually want to achieve, but hopefully this explains at least what happens.

Oh, and assuming ramSize is number of bytes, this is almost certainly always false:

assert(ramSize*2) <= 2^(64-8)

since ramSize * 2 > 0, and (2 XOR 56) = 0.

OTHER TIPS

First of all, new knows nothing about swap. That's the job of the operating system (at least for general purpose operating systems, like the ones you are working with) and it will decide when to swap and what to swap out.

With that said, your attempt to allocate that much memory will almost certainly fail. Even if sufficient swap memory is available, the operating system reserves large chunks of virtual address space for itself which means what you can allocate is limited. For example, on 32-bit Windows 2GB of addresses are reserved for the kernel, and you coudln't allocate 4GB of virtual memory if you tried, because there aren't enough addresses available to represent that much memory.

What are you, actually, trying to do?

In short Swap space is the portion of virtual memory that is on the hard disk, used when RAM is full. And this is the job of OS to take care of not the new operator!

If you use new, the operating system receives a request for memory. It then decides where to get that memory.

It is the same as with opening a file or echoing with cout. Those things are builtin to c++.

So your programms might already use swap, but you did not know about it.

If you want to know more about this subject, you might want to look up the inner workings of unix (mother and father of every operating system currently in use).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top