Thursday, 3 October 2013

Probing system memory info in MATLAB with Linux

I came across a MATLAB library lately which checks for available memory before proceeding, so that the algorithm knows how much memory it should use (e.g., use up to 10% of available memory).

On Windows the function memory() comes in handy for this need, while in Linux we will have to write a few lines of code.
function [ totalmem, freemem ] = linuxMemory()
% Usage: No input arguments needed.
% Returns total and free memory in Kilobytes.

    % probe system memory information
    [~,meminfo] = system('cat /proc/meminfo');

    % get total memory
    tokens = regexpi(meminfo,'^MemTotal:\s*(\d+)\s', 'tokens');
    totalmem = str2double(tokens{1}{1});

    % get available memory
    tokens = regexpi(meminfo,'^*MemFree:\s*(\d+)\s','tokens');
    freemem = str2double(tokens{1}{1});

end

No comments:

Post a Comment