How to acquire real memory, else return NULL on Windows 10?

Is there a way, in C, to acquire real (non-virtual) memory on Windows 10, and be denied (e.g., by returning NULL) if real memory isn’t available?

By way of background, my Visual Studio C program uses malloc() to acquire memory – which I thought would be limited by my Linker->System Heap size setting (now 1048576, which I think is bytes, but VS doesn’t show units). Despite this setting, malloc is happy to return memory blocks that exceed my computer’s 64GB RAM.

If there isn’t a way to get guaranteed real RAM, can I be assured that a static assignment (such as: static char buff[20000000]:wink: will be real RAM? (not counting the case where Windows rolls the entire program out to disk)

Question 2: What does the Linker->System Heap size setting do if it doesn’t specify the maximum size that the program can use?

Thank you!

I think the link below is about as close to what you’re looking for as it gets.

In an OS with protected memory, you shouldn’t be able to access directly the real memory. To reserve it and lock it. It defeats the whole thing.

You may be able to do that in kernel mode (writing a driver).

MS Windows is practicing memory overcommitment. This means that when you allocate memory from userspace, even if it doesn’t have enough physical memory, even if it doesn’t have enough disk space for swapping, it will allocate the memory as it doesn’t check first.

Why do you want to ensure you have memory pages locked in RAM?

1 Like

Thanks, tk11. That function appears to precisely do the job.

Although I did specify Windows 10 in my question (and Windows is likely the only OS my program will run on), I’m hoping there might be a more portable method.

I’d like to get a better understanding of what the four Visual Studio linker parameters (reserved and committed stack sizes, reserved and committed heap sizes) mean. E.g., most malloc descriptions say that malloc gets memory from the heap; if so why can it get more? When time permits, I’ll probably pose this as a separate question.

Hi LupusMichaelis,

Other than the many Windows and background processes that are always running, my program (~20 instances of it) is the only user program running. Because the program is disk intensive, I don’t want the memory it requests to be virtual memory because that will add to the already high disk activity – which will reduce overall throughput. I.e., I’d rather have no memory (and run fewer instances) than virtual memory.

In a brief reading of how memory Windows manages memory (complicated!), this goal may be a pipe dream, particularly if I prefer a portable solution.

At the moment I’m leaning toward using static declarations in the hope that static memory may be real more often than malloc memory. This may be a false hope.

It is a false hope. All memory that your process allocates is virtual. The OS kernel then manages the memory page-by-page and maps your allocated virtual pages to actual physical memory as needed.

Roughly, the first time you actually access a virtual page (that is not already mapped to physical mem), the kernel gets a page fault from the HW and “maps” your virtual page to a physical page through manipulation of the page tables.

The kernel will also try to manage overallocation by swapping as necessary. And will unmap pages that are no longer in use.

On Linux (not sure if it’s the same on Windows) the size of the virtual data segment of the program is set by brk(). The _alloc() family uses this to actually allocate virtual memory from the kernel so that it can portion it out to the program. But it’s still virtual memory.

I spend most of my time scripting not writing C so I’m not at all sure but I think the heap size setting just sets the initial size of the heap.

Reserved I believe sets the upper limit while committed sets the initial size / lower limit. I can’t recall ever having to play with these settings though so I could easily be wrong.

Does this mean that the stack is also “unlimited” in that the OS uses virtual memory when needed?

From a coding standpoint, the stack is the easiest memory allocator/deallocator in that memory comes and goes as needed.

This issue (what do the linker settings do?) came up only because Visual Studio often warns me to avoid the stack for some allocations (that another compiler is fine with). Increasing the linker settings suppressed the warnings – but I remain concerned about a stack overflow during execution.

I’m not sure what limitations there are for the stack but no matter how a windows application allocates memory, it’s always addressed virtually.

Another angle you may want to consider is just running a large ram disk and having your application interact with this memory via the file system.

The stack is like a stack of plates: you (generally) only modify the top by adding or removing plates, and you cannot have any holes/voids in the stack. E.g. when you call a function, its local variables are allocated from the top of the stack, and when you return from that function those variables are popped from the stack.

Which means the stack needs a continuous address space to grow into. When you increase the linker settings, you increase the address space that will be available for the stack (and thus unavailable for e.g. the heap).

(And as tk11 also said, it’s all virtual, from the first byte.)

I’d rather have no memory (and run fewer instances) than virtual memory.

Be careful here. I know that MS Windows mislead by naming the page file setting “Virtual memory”, but that’s not what “Virtual memory” is.

The virtual memory is what a process sees as a mirage (2³² or 2⁶⁴ bytes depending on hardware architecture), independent to the actual available memory. The OS will source memory pages from the RAM or the page file (or swap) using various mechanisms (hopefully hardware).

If you think your app may be in competition with the pagefile, you have to check if it’s really competing for IO. You should benchmark your app’s behaviour to ensure it’s really starving IO, and be aware of the OS behaviour with caching files in memory: the OS doesn’t (by default) write/read to/from the disk, it caches everything in RAM. The OS not having memory to cache files will have a more detrimental performance hit than having your app enough memory for its operations.

So, once you benched and are sure there is competition, you may:

  • deactivate the page file
  • move the page file to a different storage device
  • think again your idea is sound, feasible

I won’t address the other points are other people addressed them satisfactory.

We don’t know what your app’s doing, so we can only say generic and vague statement, hope it helps you anyway :wink:

The files the program reads and writes are often 150GB in size, much larger than my 64GB RAM.

I understand how the stack works from a programming standpoint.

What I don’t understand is the meaning of the four Visual Studio linker parameters mentioned previously, and how, if at all, might certain values result in a stack overflow, or a malloc denial.

Right… so you’re already reading/writing smaller chunks.

I don’t know what your program does though so how you implement it is up to you. A ram disk does take care of preventing a massive amount of ram from being paged though.

Isn’t that clear?

Anyway, compiler/linker won’t have impact on how the memory is managed by the OS, as I previously said, the OS abstract away the memory hardware.

The linker configuration provides settings on how the virtual memory address space of a process is laid out. The sizes of stacks and heap are local to your process. Those settings are used to help detecting programming errors or strange behaviour, as well where the next stack (thread’s stack) will start so a stack will have enough room to grow.

If you are manipulating big files, maybe you should leave the read/write to the OS. There is primitives to do so. If you need to do some processing on the files you manipulate, you don’t want to load them in memory. Especially such large files. But the OS provides facilities to load efficiently like memory mapped files that you can use like a ribbon you mount in memory (don’t forget the semaphore if you’re multi-threading/multi-processing the file)

1 Like