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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With