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

Is it possible to get the remaining available memory on the system (x86, x64, PowerPC / Windows, Linux or MacOS) in standard C ++ 11 without failures?

The naive way is to try to allocate very large arrays, starting from too large, catch exceptions every time it fails, and reduce the size until an exception is thrown. But maybe there is a more efficient / smart method ...

EDIT 1: Actually, I don't need the exact amount of memory. I would like to know approximately (error line 100 MB) how much my code could use when I start it.

EDIT 2: What do you think of this code? Is it safe to run it at the beginning of my program or can it spoil the memory?

#include <iostream> #include <array> #include <list> #include <initializer_list> #include <stdexcept> int main(int argc, char* argv[]) { static const long long int megabyte = 1024*1024; std::array<char, megabyte> content({{'a'}}); std::list<decltype(content)> list1; std::list<decltype(content)> list2; const long long int n1 = list1.max_size(); const long long int n2 = list2.max_size(); long long int i1 = 0; long long int i2 = 0; long long int result = 0; for (i1 = 0; i1 < n1; ++i1) { try { list1.push_back(content); } catch (const std::exception&) { break; } } for (i2 = 0; i2 < n2; ++i2) { try { list2.push_back(content); } catch (const std::exception&) { break; } } list1.clear(); list2.clear(); result = (i1+i2)*sizeof(content); std::cout<<"Memory available for program execution = "<<result/megabyte<<" MB"<<std::endl; return 0; } 
+6
source share
4 answers

It is highly OS / platform dependent. The approach you propose does not even have to work in real life. On some platforms, the OS will give you all your memory requests, but it won’t give you memory until you use it, after which you will get SEGFAULT ...

The standard does not have anything to do with this.

+8
source

It seems to me that the answer is no, you cannot do it in standard C ++.

What you can do instead is discussed in How to get available C ++ / g ++ memory? and the contents contained therein. All these are platform specific things. This is not standard, but at least it helps solve the problem you are dealing with.

+2
source

As others have noted, the problem is difficult to pinpoint, much less solve. Is virtual memory on the hard drive considered "accessible"? How about whether the system will implement an invitation to delete files in order to get more hard disk space, meanwhile pausing its program? (This is exactly what happens on OS X.)

The system probably implements a memory hierarchy that gets slower when you use more. You can try to find a performance rock between RAM and disk by allocating and initializing memory chunks when using the C alarm interrupt tool or clock or localtime / mktime , or C ++ 11 clock. The wall clock time should appear faster as the machine slows down under the voltage of receiving memory from less efficient resources. (But this makes the assumption that it is not emphasized by anything else, such as another process.) You would like to tell the user that the program is trying and save the results to an editable configuration file.

+1
source

I would advise using the configured maximum amount of memory instead. Since some platforms overload memory, it's not easy to say how much memory you really have access to. It is also not polite to assume that you have exclusive access to 100% of the available memory, other programs will be launched on many systems.

+1
source

All Articles