How to access memory information in Matlab on Unix - equivalent of user view.MaxPossibleArrayBytes

前端 未结 1 1345
小蘑菇
小蘑菇 2021-01-05 14:51

I am looking for an equivalent of the data returned by memory on windows platform on unix, in matlab.

I am aware of the possibility of using unix(\'vm_stat\'), but t

相关标签:
1条回答
  • 2021-01-05 15:38

    Call command 'free' and parse the results. This works on linux

    [r,w] = unix('free | grep Mem');
    stats = str2double(regexp(w, '[0-9]*', 'match'));
    memsize = stats(1)/1e6;
    freemem = (stats(3)+stats(end))/1e6;
    

    The output is in Gbytes. The last number free returns is 'cached' memory used by the OS, e.g. dynamic libraries. It can in general be used, but you can decide to leave it out and just use what free reports as 'Free' - the third numerical field in the output.

    Edit On Linux, memory allocation within MATLABs mxMalloc/mxCalloc most likely simply calls malloc and friends. To get a hint that this is the case do the following experiment. In a mex file allocate an array using the following code, and return it to MATLAB:

      rout = calloc(sizeof(Double),M*N);
      pargout[0] = mxCreateNumericMatrix(0,0,mxDOUBLE_CLASS,mxREAL);
      mxSetM(pargout[0], m);
      mxSetN(pargout[0], n);
      mxSetData(pargout[0], rout);
      mexMakeMemoryPersistent(rout);
    

    You can normally use the variable returned in MATLAB. You can even clear it - this does not cause any problems. If indeed MATLAB simply uses malloc, there is no way that I know of in which they can enforce physically contiguous memory.

    I know that you can not run the above code on Windows though. This code crashes MATLAB. Of course, you should not do that in your codes. It merely illustrates the point.

    0 讨论(0)
提交回复
热议问题