How to Get Available Memory C++/G++

How to get available memory C++/g++?

Having read through these answers I'm astonished that so many take the stance that OP's computer memory belongs to others. It's his computer and his memory to do with as he sees fit, even if it breaks other systems taking a claim it. It's an interesting question. On a more primitive system I had memavail() which would tell me this. Why shouldn't the OP take as much memory as he wants without upsetting other systems?

Here's a solution that allocates less than half the memory available, just to be kind. Output was:

Required FFFFFFFF

Required 7FFFFFFF

Required 3FFFFFFF

Memory size allocated = 1FFFFFFF

#include <stdio.h>
#include <stdlib.h>

#define MINREQ 0xFFF // arbitrary minimum

int main(void)
{
unsigned int required = (unsigned int)-1; // adapt to native uint
char *mem = NULL;
while (mem == NULL) {
printf ("Required %X\n", required);
mem = malloc (required);
if ((required >>= 1) < MINREQ) {
if (mem) free (mem);
printf ("Cannot allocate enough memory\n");
return (1);
}
}

free (mem);
mem = malloc (required);
if (mem == NULL) {
printf ("Cannot enough allocate memory\n");
return (1);
}
printf ("Memory size allocated = %X\n", required);
free (mem);
return 0;
}

How to get available RAM on Windows?

You can use the same call to GlobalMemoryStatusEx() and examine the .ullAvailPhys field of the MEMORYSTATUSEX structure to get the amount of available physical memory. The difference between this and the .ullTotalPhys value will be how much physical memory is in use.

C++ checking available ram?

Windows: GlobalMemoryStatusEx. MSDN page has a detailed C sample code.

Linux: check the "/proc/meminfo" file (discussion)

OSX: see this SO thread Determine physical mem size programmatically on OSX

The question is not clear, however. There is physical memory, there is virtual memory, there is an OS ability to swap some unused pages to disk/other storage.

If you need to write some kind of a system monitor, then my answer would do.

If you need to be sure that none of your malloc()/new[] calls fail, then just catch appropriate exceptions or handle NULL results. The other option is to build your own allocator which gets a large memory block at the beginning and allocates smaller blocks there.

EDIT: answer to comment

The calls to WinAPI's MapViewOfFile and CreateFileMapping provide error codes to exclude fatal situations. Since files are mapped to the virtual address space shared with your process' data, you may check if there are sufficient number of pages available. I.e., if you're on a 32-bit system, you won't be able to map the whole 8Gb file to the memory at once (but you can map its smaller parts), but on a 64-bit system the mapping possibilities are sufficient for any current needs.

How to alloc largest available memory on different computer?

You can use the GNU extension get_avphys_pages() from glibc

The get_phys_pages function returns the number of available pages of physical the system has. To get the amount of memory this number has to be multiplied by the page size.

Sample code:

#include <unistd.h>
#include <sys/sysinfo.h>
#include <stdio.h>

int main() {
long int pagesize = getpagesize();
long int avail_pages = get_avphys_pages();
long int avail_bytes = avail_pages * pagesize;
printf( "Page size:%ld Pages:%ld Bytes:%ld\n",
pagesize, avail_pages, avail_bytes );
return 0;
}

Result Godbolt

Program returned: 0
Page size:4096 Pages:39321 Bytes:161058816

This is the amount of PHYSICAL memory in your box so:

  1. The true available memory can be much higher if the process pages in/out

  2. The physical memory is a maximum as there would be other processes using memory too.

So treat that result as an estimate upper bound for available DDR.

If you plan to allocate large chunks of memory use mmap() directly as malloc() would be too high level for this usage.

Get the remaining available memory in standard C++11?

This is highly dependent on the OS/platform. The approach that you suggest need not even work in real life. In some platforms the OS will grant you all your memory requests, but not really give you the memory until you use it, at which point you get a SEGFAULT...

The standard does not have anything related to this.

How to determine amount of free RAM in C++ application?

About paging: paging is performed automatically if enabled in Windows. No need to change your code to use it.

About free memory: Win32/MFC: How to find free memory (RAM) available?

You could also try to claim the memory. If successful, the operation can be performed. If you are going to use it anyway... This is not the nicest solution.



Related Topics



Leave a reply



Submit