From b02d0000e965074fcbb45a8f4f13f78c70cd3e81 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Fri, 9 Jan 2015 14:10:26 +0100 Subject: [PATCH] pulled Joe136 solution to MemAvailable being present in /proc/meminfo on some systems --- linux/memory.cc | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/linux/memory.cc b/linux/memory.cc index 6497ccb..56d76ad 100644 --- a/linux/memory.cc +++ b/linux/memory.cc @@ -28,10 +28,17 @@ std::string mem_string( bool use_colors = false ) { for( unsigned int i = 0; i < 3; i++ ) { getline( meminfo_file, mem_line ); - substrStart = mem_line.find_first_of( ':' ) + 1; - substrLen = mem_line.find_first_of( 'k' ); - unused_mem = stoi(mem_line.substr(substrStart, substrLen)); - used_mem -= unused_mem; + // accomodate MemAvailable potentially being in lines 2-4 of + // /proc/meminfo. do this in a way to not break the original logic of the + // loop + if( mem_line.find("MemAvailable") == 0 ) + i--; + else { + substrStart = mem_line.find_first_of( ':' ) + 1; + substrLen = mem_line.find_first_of( 'k' ); + unused_mem = stoi(mem_line.substr(substrStart, substrLen)); + used_mem -= unused_mem; + } } meminfo_file.close();