answersLogoWhite

0


Best Answer

You don't say whether you mean physical memory or virtual memory. Regardless, there is no portable method given that memory management is system-specific. However, the system should provide some means of giving you the information you need. The following example works on Windows and Linux:

#ifdef WIN32 #include <windows.h>

unsigned long long getTotalSystemMemory()

{

MEMORYSTATUSEX status;

status.dwLength = sizeof(status);

GlobalMemoryStatusEx(&status);

return status.ullTotalPhys;

}

#else // UNIX...

#include <unistd.h>

unsigned long long getTotalSystemMemory()

{

long pages = sysconf(_SC_PHYS_PAGES);

long page_size = sysconf(_SC_PAGE_SIZE);

return pages * page_size;

}

#endif

User Avatar

Wiki User

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

6y ago

There is no portable means of doing so because different operating systems manage memory in different ways.

In Windows, use the following code:

#include "windows.h"

#include "psapi.h"

PROCESS_MEMORY_COUNTERS_EX pmc;

GetProcessMemoryInfo (GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));

// pmc.WorkingSetSize tells us how much physical RAM is currently consumed by this process.

// pmc.PrivateUsage tells us how much virtual memory is currently consumed by this process.

In Linux, use the following code:

#include "stdlib.h"

#include "stdio.h"

#include "string.h"

int parseLine(char*); // forward declaration (used by getWorkingSize() and getPrivateUsage())

// tells us how much physical RAM is currently consumed by this process (in KB)

int getWorkingSetSize(){

FILE* file = fopen("/proc/self/status", "r");

int result = -1;

char line[128];

while (fgets(line, 128, file) != NULL){

if (strncmp(line, "VmSize:", 7) == 0){

result = parseLine(line);

break;

}

}

fclose(file);

return result;

}

// tells us how much virtual memory is currently consumed by this process (in KB)

int getPrivateUsage(){

FILE* file = fopen("/proc/self/status", "r");

int result = -1;

char line[128];

while (fgets(line, 128, file) != NULL){

if (strncmp(line, "VmRSS:", 6) == 0){

result = parseLine(line);

break;

}

}

fclose(file);

return result;

}

int parseLine(char* line){

int i = strlen(line);

const char* p = line;

while (*p <'0' *p > '9') p++;

line[i-3] = '\0';

i = atoi(p);

return i;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you get how much memory is available using c program?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How can you find out how much memory is available?

Erm, Start-&gt;My Computers.


What do you understand by static memory allocation and dynamic memory allocation?

MEMORY ALLOCATION MODELS&hellip;&hellip;STATIC MEMORY ALLOCATION DYNAMIC MEMORY ALLOCATIONMemory is allocated before the execution of the program begins.(During Compilation)Memory is allocated during the execution of the program.No memory allocation or deallocation actions are performed during Execution.Memory Bindings are established and destroyed during the Execution.Variables remain permanently allocated.Allocated only when program unit is active.Implemented using stacks and heaps.Implemented using data segments.Pointer is needed to accessing variables.No need of Dynamically allocated pointers.Faster execution than Dynamic.Slower execution than static.More memory Space required.Less Memory space required.


What is the native heap size?

The heap refers to the free store, which basically means the total unused memory available to you. The physical amount of memory will vary from system to system and the amount of memory available will depend upon how much is currently in use. However memory fragmentation means that while there may be sufficient physical memory available to meet an allocation request it does not follow that there is a large enough block of contiguous memory available, resulting in an out of memory error. Modern systems use virtual memory addresses rather than physical ones. This allows the memory manager to physically move objects around in memory without affecting the virtual addresses allocated to those objects and thus makes it possible to consolidate memory fragments. Objects that are not in use can also be paged out to a disk file, thus making it seem like there's far more memory available than physically exists.


What is profiling in IT industry?

Profiling refers to the act of running a program (called a "profiler") which analyzes a program you've written. The profiler will collect information about your program, such as: what functions are called, how often each is called, how much time is spent in each function, how much memory is used, etc.. This information can be used to help improve the performance of your program.


Static and dynamic memory allocation in c plus plus?

dynamic memory allocation is that type of memory which create to allocate the memory on running time or at compile time by the function of malloc , calloc , realloc and free. dynamic memory allocation is give the best utilization of memory which gives the sufficient use of memory.

Related questions

If a program is using too much of system resources and bogging down?

The reason that a program is using too much memory is the way it is programmed or written. A solution to this is to quit or shut down the program and restart it. If there is still not enough memory then increasing the amount of memory on the computer will help. Another reason that a program is using too much memory is a virus is running on the computer. Like a human virus the computer virus is very bad news. There are still some computers that make it easy to catch a computer virus. Removing the virus will make the computer run much better.


When using Adobe Premier Pro 2 the playback keeps on stopping without giving a reason - nothing crashes - what is the problem?

Whenever a computer program stops working all of a sudden there are some of us that classify that as a crash. One reason for a software crash is the amount of RAM available for the program on the computer the program is running on. The Adobe range of programs have a habit of using as much memory as they can. Solution currently available to you include closing every other program but the operating system and the Adobe program you are actually using. One way of avoiding the problem happening again is to get a faster computer, more RAM, a similar program that uses less memory


How much memory is found in DOS transient program area?

1. How much memory is found in the DOS transient program area


What is the difference between physical memory and available memory?

Physical memory is how much total memory your computer actually has. Available memory is what memory you have that is not being used.


What are Outings?

A page out is when a page from memory is written to disk. If you have a program/application that needs alot of computer memory, the CPU can break up the program and run in memory the only part it currently needs. Once that part is finished it can be paged out and the next part paged in. Using the disk drive allows the computer to run larger jobs than those that would only fit into memory. Using the disk drive to swap out pages will cause the program to run slower. It's a trade off. Today's computer has much more memory that what was available in the past and the need for paging and swaping isn't as important as it once was. It is still used today.


What are page outs?

A page out is when a page from memory is written to disk. If you have a program/application that needs alot of computer memory, the CPU can break up the program and run in memory the only part it currently needs. Once that part is finished it can be paged out and the next part paged in. Using the disk drive allows the computer to run larger jobs than those that would only fit into memory. Using the disk drive to swap out pages will cause the program to run slower. It's a trade off. Today's computer has much more memory that what was available in the past and the need for paging and swaping isn't as important as it once was. It is still used today.


What the meaning of 3072 DDR3 RAm?

3072 is how much memory the ram has and what ram does is it like short term memory so when you are using any program it is using ram. ddr3 is the type of ram, it just means it is a little better then ddr2.


How much memory does a 2gb memory card hold?

Not much on anything, but that depends on what you are using.


Why does it say available memory is 614 mb when there is 1024 mb in the computer?

Any program running on your computer requires an amount of memory to function. The amount can vary depending on the software running and it's intended purpose. You operating system is a software program or more accurately, a collection of programs. All of the processes and tasks it performs require memory. This is why 410Mb of memory is un available; it's already in use by processes running on the computer. When the memory you have is all used up, then windows uses something called "virtual memory" which is basically one or more temporary files on your hardisk (also known as swap files). When there is no memory available, windows will use virtual memory instead (where possible). Writing and reading data with a hard disk is much slower than using memory. This is one major reason why increasing memory size (reducing the need to use virtual memory) improves the overall performance of a computer.


What is the purpose of storing a program in memory?

Anything stored in memory can be accessed much faster than items stored on the harddrive. This is basically because of the physical composition of memory chips which allows for much faster data-transfer. The tradeoff is the amount of space that memory provides, and the number of programs that end up sharing the memory. So, when a program is stored in memory, its execution is much faster. However, if a program exists ONLY in the memory, and does not store any data on the primary disk, then when you exit a program, all your data will also be destroyed due to active memory management that takes place. Data will not persist when program exists. Hence, you will need to write the data to a file on the hardisk which can be accessed later and loaded into the memory next time when the program runs.


How much memory is available to the 80486 microprocessor?

2 mb


How can you find out how much memory is available?

Erm, Start-&gt;My Computers.