MATLAB Answers

Can I avoid matlab to allocate too much memory?

69 views (last 30 days)
GiulioSt on 4 Jun 2015
Answered: Med Aymane Ahajjam on 23 Oct 2019
I'm using Matlab R2014b Version on a Linux server and on a Windows desktop. The server is a very resourceful server (24 core, lots of GB of memory,...). The problem is that the same matlab code needs almost 4GB of memory in my 2core Windows-based desktop and more than 20GB of memory in the server. Since the server is shared with other people and I need to run several simulations in parallel, I would like to know why does Matlab ask for more memory in the server and how can I reduce the allocated memory?
At the beginning I thought that the problem was something related with parfor cicles. I did an attempt substituting all the parfor cicles into for circles in the server but the allocated memory is still almost the same value, maybe 2 or 3 GB less, but still too much compared with the Windows case. I also tried to use the same number of workers in both the computers but the problem still remains.
Where do you think is the problem and how I can solve it? Why does Matlab allocate more memory in the server? Thanks

Answers (3)

Jan on 4 Jun 2015
If a lot of large variables are created, Matlab requests memory from the operating system. When a variable is deleted, the used memory is not free'd immediately, but the operating system can decide, when this happens. Freeing memory causes the clearing of the contents (at least this happens under Windows), such that this needs processing time. Therefore the OS will offer new memory instead of recycling free'd memory, when there is enough free memory available. In consequence the memory usage of Matlab depends on the other applications also and cannot be measured directly.
A strategy to reduce the memory footprint of the code is to avoid the creation of new large variables. See
  1 Comment
GiulioSt on 8 Jun 2015
Thanks for the answer. I checked and my problem doesn't seem related with the allocation of large variables, since the total allocated memory for variables isn't bigger than 300MB. Moreover, this problem appears even before the allocation of the biggest variables I have in my code. (I checked it using the whos() function). About parallel processes, could you suggest me an attempt to be sure that the problem is not related with parfor cycles and/or workers?

Sign in to comment.

Med Aymane Ahajjam
Med Aymane Ahajjam on 23 Oct 2019
Personnaly, when using app designer, i do not use variables to hold big chunks of data, instead I put the functions that would result in these values directly as inputs to the next functions.. It saves LOTS of time!


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by