0

I am planning to create cross-platform library to provide access to system configuration information (installed hardware, model of CPU, etc...).

When looking for way to access total memory (by "total" I mean amount of memory installed in system, not memory available to system), I found some problems.

In Windows, this code provide correct value:

#include <Windows.h>
....
uint64_t memory;
GetPhysicallyInstalledSystemMemory(&memory);

In OS X, this code provide correct value:

#include <sys/sysctl.h>
...
size_t memorySize;
sysctlbyname("hw.memsize",nullptr,&memorySize,nullptr,0);
int64_t memory;
sysctlbyname("hw.memsize",(void*)&memory,&memorySize,nullptr,0);

In case of Linux(Ubuntu), common way to get memory information is to parse information from command "free" or file "/proc/meminfo". Problem is that these options provide memory usable to system, which is close but not the same as total memory (similar situation with Windows function "GlobalMemoryStatusEx").

Additionally, I plan to use only C or C++ API to gather required information and don't want to result to parsing text using File API.

I am aware of command dmidecode where I could just add memory in each installed memory bank, but beside the need to pars textual data, command requires root to run.

So main question, is there option as described here ? Maybe exists function that was added later in time and is not very popular ( For Windows, I found information about "GetPhysicallyInstalledSystemMemory" function much later, because it was introduced in Windows Vista and most examples use older "GlobalMemoryStatusEx").

EDITED:

Question is not duplicated as I describe specific problem with obtaining "total" memory as total memory installed in particular system. Also, I clearly specify requirements for possible solution using only C or C++ API available, not parsing results of command or text file like "/proc/meminfo" as other similar questions do not set such requirements.

EDITED_2:

Changes to reflect that both C and C++ API are applicable

Zayl
  • 139
  • 3
  • 11
  • Questions about C/C++ shall be tagged with the C/C++ tag, not the tag of the two different languages C and C++. – too honest for this site Jan 14 '17 at 16:59
  • I could not find combined tag, so changed back to "C" and "C++" as this should also specify applicability of both languages. – Zayl Jan 14 '17 at 18:43
  • "I could not find combined tag" - so what does that tell you? Hint: you added 2 + 2 and got -4. And read what the procfs is for ... – too honest for this site Jan 14 '17 at 19:52
  • Maybe I should specify from beginning that I still deciding on implementation language (C++ or C for easier integration with other languages, in my case Java by JNI). – Zayl Jan 14 '17 at 20:03
  • I'm evaluating a reopening flag for this question. Regarding parsing `/proc/meminfo`, it is not a real text file - everything inside `/proc` are kernel interfaces. Any API providing such information would probably read from `/proc/some_pseudo_file`. Since text and file based interfaces are very traditional in Linux (and Unixes in general), do you care to explain why are you ruling out this kind of solution? – Paulo Scardine Jan 16 '17 at 17:44
  • Question was asked because sysinfo function on Ubuntu that I am working on do not provide correct values: sysinfo.totalram returns like 97% of correct value like 15.52 GB instead 16 GB that I have on this machine ( additionally, this field is described as "unsigned long totalram; /* Total usable main memory size */". – Zayl Feb 07 '17 at 17:56
  • Also, I decided to avoid reading from pseudofile as I am writing C/C++ compiled program and such parameters are probably available as library function (piece of code that provided implementation to mentioned pseudofile is getting this information from somewhere). – Zayl Feb 07 '17 at 18:01

0 Answers0