Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the specific part of information I require is the largest contiguous free memory block.

This information is returned by memory as follows:

[userview, ~] = memory;
a = userview.MaxPossibleArrayBytes

Does anybody no how to write a unix command that could return this same information?

like image 448
Steven Avatar asked Sep 10 '12 11:09

Steven


People also ask

How do I check memory in MATLAB?

Retrieve Memory Information Use the structure to display the amount of memory reserved for the MATLAB process. Return both the user-focused and system-focused memory information. Access the Available field of the PhysicalMemory structure to display the amount of available physical memory on the computer.

Which command display the amount of memory present on the system?

Entering cat /proc/meminfo in your terminal opens the /proc/meminfo file. This is a virtual file that reports the amount of available and used memory. It contains real-time information about the system's memory usage as well as the buffers and shared memory used by the kernel.

How much memory does MATLAB use?

Direct link to this question But, I just figured out that MATLAB doesn't use any memory more than 33% (75 GB). The simulations take a really long time and sometimes the simulations fail to converge to a solution.


1 Answers

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.

like image 198
angainor Avatar answered Nov 15 '22 06:11

angainor