views:

270

answers:

5

I'm writing a performance critical application where its essential to store as much data as possible in the physical memory before dumping to disc.

I can use ::GlobalMemoryStatusEx(...) and ::GetProcessMemoryInfo(...) to find out what percentage of physical memory is reserved\free and how much memory my current process handles. Using this data I can make sure to dump when ~90% of the physical memory is in use or ~90 of the maximum of 2GB per application limit is hit.

However, I would like a method for simply recieving how many bytes are actually left before the system will start using the virtual memory, especially as the application will be compiled for both 32bit and 64bit, whereas the 2 GB limit doesnt exist.

+2  A: 

Even if you're able to stop your application from having memory paged out to disk, you'll still run into the problem that the VMM might be paging out other programs to disk and that might potentially affect your performance as well. Not to mention that another application might start up and consume memory that you're currently occupying and thus resulting in some of your applications memory being paged out. How are you planning to deal with that?

There is a way to use non-pageable memory via the non-paged pool but (a) this pool is comparatively small and (b) it's used by device drivers and might only be usable from inside the kernel. It's also not really recommended to use large chunks of it unless you want to make sure your system isn't that stable.

You might want to revisit the design of your application and try to work around the possibility of having memory paged to disk before you either try to write your own VMM or turn a Windows machine into essentially a DOS box with more memory.

Timo Geusch
+4  A: 

How about this function:

int
bytesLeftUntilVMUsed() {
    return 0;
}

it should give the correct result in nearly all cases I think ;)

Andreas Brinck
:-) Cheered me up no end.
Skizz
+6  A: 

Imagine running Windows 7 in 256Mb of RAM (MS suggest 1GB minimum). That's effectively what you're asking the user to do by wanting to reseve 90% of available RAM.

The real question is: Why do you need so much RAM? What is the 'performance critical' criteria exactly?

Usually, this kind of question implies there's something horribly wrong with your design.

Update:

Using top of the range RAM (DDR3) would give you a theoretical transfer speed of 12GB/s which equates to reading one 32 bit value every clock cycle with some bandwidth to spare. I'm fairly sure that it is not possible to do anything useful with the data coming into the CPU at that speed - instruction processing stalls would interrupt this flow. The extra, unsued bandwidth can be used to page data to/from a hard disk. Using RAID this transfer rate can be quite high (about 1/16th of the RAM bandwidth). So it would be feasible to transfer data to/from the disk and process it without having any degradation of performance - 16 cycles between reads is all it would take (OK, my maths might be a bit wrong here).

But if you throw Windows into the mix, it all goes to pot. Your memory can go away at any moment, your application can be paused arbitrarily and so on. Locking memory to RAM would have adverse affects on the whole system, thus defeating the purpose of locing the memory.

If you explain what you're trying to acheive and the performance critria, there are many people here that will help develop a suitable solution, because if you have to ask about system limits, you really are doing something wrong.

Skizz
This application wont be used at the same time as other applications.
Viktor Sehr
So why use windows?
Skizz
Because the application needs two mouse buttons and Mac only has one.
Viktor Sehr
Ah-ha, so it's a GUI application with user interaction. Well then, you definitely don't want to be reserving that kind of memory - the system will become really unresponsive if you do. There are quite a lot of things going on whilst your app is running that will really slow the system down if there's limited RAM, i.e. excessive disk swapping. Also, I said 'windows' not 'pc' since I was thinking 'linux' or 'vxworks'. This wouldn't be video editing would it, or maybe high resolution image editing? Since it's a GUI you can fool the user into thinking it's more responsive than it actually is.
Skizz
Actually that was irony, but I'd have a hard time persuade my boss to switch operating system support. Its a storage management application working with 50-100gb datasets so I basically need to know what I was asking for in the beginning.
Viktor Sehr
Since the datasets are so large, I wouldn't worry about keeping as much as possible in RAM, you're going to have to page the data anyway. Locking RAM like you suggest is only going to affect the system in a negative way.
Skizz
+1  A: 

The standard solution is to not worry about "virtual" and worry about "dynamic".

The "virtual" part of virtual memory has to be looked at as a hardware function that you can only defeat by writing your own OS.

The dynamic allocation of objects, however, is simply your application program's design.

Statically allocate simple arrays of the objects you'll need. Use those arrays of objects. Increase and decrease the size of those statically allocated arrays until you have performance problems.

S.Lott
A: 

Ouch. Non-paged pool (the amount of RAM which cannot be swapped or allocated to processes) is typically 256 MB. That's 12.5% of RAM on a 2GB machine. If another 90% of physical RAM would be allocated to a process, that leaves either -2,5% for all other applications, services, the kernel and drivers. Even if you'd allocate only 85% for your app, that would still leave only 2,5% = 51 MB.

MSalters