function [userdata systemdata] = memory(flag)
  
# intended to become a MATLAB compatible memory() for Octave
# very first version, just released for testing purposes 
# written by Lars Kindermann   
  
  if !isunix
    warning ('memory function not yet implemented for this archiecture')
    return
  endif
  
  linux_proc = nargin && strcmp (flag, 'linux');
  
  pid = num2str(getpid);
  
##  proc_statm = ['/proc/' pid '/statm'];
##  f = fopen (proc_statm);
##  buffer = textscan (f);
##  fclose (f);
##  statm = buffer{}'; #sizes in pages a 4096 kB
##  user.VirtMemUsedOctave = statm(1)*4096/1024;
##  user.MemUsedOctave = statm(2)*4096/1024;
  
  proc_pid_status = ['/proc/' pid '/status'];
  f = fopen (proc_pid_status);
  buffer = textscan(f, '%s %s', 'delimiter', ':\n');
  fclose (f);
  
  for i=1:size(buffer{1})
    status.(buffer{1}{i}) = buffer{2}{i};
  endfor
 
  user.VirtMemUsedOctave = textscan(status.VmSize){};
  user.MemUsedOctave = textscan(status.VmRSS){};
  
  user.MaxPossibleArrayBytes = nan;             #MATLAB compatibility
  user.MemAvailableAllArrays = nan;             #MATLAB compatibility
  user.MemUsedMATLAB = user.MemUsedOctave;      #MATLAB compatibility

  proc_meminfo = '/proc/meminfo';
  f = fopen (proc_meminfo);
  buffer = textscan (f, '%s %s', 'delimiter', ':\n');
  fclose (f);
  for i=1:size (buffer{1})
    # all fields are numeric / kB
    meminfo.(buffer{1}{i}) = textscan (buffer{2}{i}){};
  endfor
  
  sys.PhysicalMemory.Available = meminfo.MemAvailable;
  sys.PhysicalMemory.Total = meminfo.MemTotal;
  sys.VirtualAddressSpace.Available = nan;
  sys.VirtualAddressSpace.Total = nan;
  sys.SystemMemory = meminfo.MemTotal + meminfo.SwapTotal;
  
  if nargout
    if linux_proc
       userdata = status;
       systemdata = meminfo;
    else 
      userdata = user;
      systemdata = sys;
    endif
  else  
    if linux_proc
      disp ('')
      disp (['  ' proc_pid_status])
      disp (status)
      disp ('')
      disp (['  ' proc_meminfo])
      disp (meminfo)
    else
      disp (sprintf ('Used by Octave RAM: %6.1f MB Virtual adress space: %6.1f MB', 
                   user.MemUsedOctave/1024, user.VirtMemUsedOctave/1024 ) )
      disp (sprintf ('System Totals  RAM: %6.1f MB including swap space: %6.1f MB', 
                   round (sys.PhysicalMemory.Total / 1024),
                   round (sys.SystemMemory./ 1024) ) )
      disp (sprintf ('Available      RAM: %6.1f MB Virtual: %6.1f MB', 
                   round (sys.PhysicalMemory.Available / 1024),
                   round (sys.VirtualAddressSpace.Available / 1024) ) )
    endif               
  endif
  
endfunction
