Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical Usage of Virtual Memory

Tags:

memory

virtual

I have used the code

MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
DWORDLONG totalVirtualMem = memInfo.ullTotalPageFile;
DWORDLONG virtualMemUsed = memInfo.ullTotalPageFile - memInfo.ullAvailPageFile;
DWORDLONG totalPhysMem = memInfo.ullTotalPhys;

provided at here

Output is like: 2.3GB.

totalVirtualMem = 8.5 Gb
virtualMemUsed  = 2.3 Gb
totalPhysMem    = 4   Gb

Does this means that my program requires 2.3Gb of memory? Could you also comment on total virtual memory and RAM? Also I was not able to run this code:

PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;

as it gives error as,

error C2664: 'GetProcessMemoryInfo' : cannot convert parameter 2 from 'PROCESS_MEMORY_COUNTERS_EX *' to 'PPROCESS_MEMORY_COUNTERS'
like image 786
Shibli Avatar asked Jan 11 '12 01:01

Shibli


People also ask

When and how virtual memory is used?

Virtual memory is used when the computer has no more available random access memory (RAM). There are times when the amount of RAM needed to hold all running programs and data is greater than the amount of RAM available to the computer.

What is an example of virtual memory?

Example of virtual memoryA business owner uses their computer's virtual memory system when running multiple applications simultaneously. The user tries to load their email in their browser window while also running word processing software, shift scheduling software and a content management system.

Is virtual memory used today?

Yes, because it's the basis of all on-demand paging that occurs in a modern operating system, not just Windows. Windows will always use all of your memory, if not for applications then for caching whatever you read from your hard drive.

What are the purpose of virtual memory and physical memory?

The main difference between physical and virtual memory is that the physical memory refers to the actual RAM of the system that stores the currently executing programs, but the virtual memory is a memory management technique that allows the users to execute programs larger than the actual physical memory.


1 Answers

I stumbled upon exactly the same Problem and found out that a simple type cast solved it for me.

PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;

The solution is also described here (msdn: Question about GetProcessMemoryInfo).

like image 86
Constantin Avatar answered Oct 11 '22 20:10

Constantin