Memory Allocation in MATLAB
5 views (last 30 days)
Show older comments
David C
on 9 May 2015
Answered: Walter Roberson
on 10 May 2015
Two questions on the topic of memory:
1. Do the zeros() and ones() functions allocate memory dynamically? That is, are they similar to the malloc() function in C/C++?
2. Of course, if we try to allocate too much memory, the system will refuse:
>> a=zeros(1e10,1);
Out of memory. Type HELP MEMORY for your options.
I am running MATLAB in Linux. Will increasing the swap increase the size of matrix I can allocate? That is, can I increase swap to a point that a=zeros(1e10,1) will execute successfully?
0 Comments
Accepted Answer
Walter Roberson
on 10 May 2015
If I recall correctly, MATLAB keeps a pool of small blocks (I do not recall the block size) that it tries to reuse for small variables. When the pool is exhausted or if the memory is too large for the pool, then MATLAB will use malloc() or equivalent. As per usual, this means that the memory might never be given back to the operating systems when it goes out of use, but MATLAB does appear to sometimes shrink its memory in practice.
If you are running a 32 bit version of MATLAB, then you are not going to be able to create an object larger than 2 Gb, due to limitations in 32 bit architectures.
If you are running a 64 bit version of MATLAB then the limit on the size of objects is determined by how many bits of addressing your processor supports. The Intel x64 architecture in its present incarnations supports 48 bits of physical addressing.
Double precision values are 8 bytes, which is 3 bits. So the x64 architecture would support up to 2^(48-3) = 2^45 such values. That is about 1.37E11 objects.
So yes, if you have a 64 bit architecture, then if you increased your swap space far enough you could do zeros(1e10,1) . You will need about 80 GB for that. The full maximum is 256 TB
0 Comments
More Answers (0)
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!