MATLAB Out of Memory Error - matlab

Relevant code:
Hf = ((j*2*pi*f).^2 + Wo.^2)/((j*2*pi*f).^2 + 2*Wo*j*2*pi*f + Wo.^2);
It runs out of memory on the Hf = ' ' line. The only non-numerical part of this statement is the f term. Typing in the command window :
size(f)
ans = 65536 1
I'm not used to MATLAB at all, but I don't think this is something I can fix by freeing up memory before I make that statement.
Here's the memory after getting the error:
Maximum possible array: 10096 MB (1.059e+10 bytes) *
Memory available for all arrays: 10096 MB (1.059e+10 bytes) *
Memory used by MATLAB: 3716 MB (3.896e+09 bytes)
Physical Memory (RAM): 8173 MB (8.570e+09 bytes)
Limited by System Memory (physical + swap file) available.
Here it is before (after I hit >>clear all)
Maximum possible array: 10095 MB (1.059e+10 bytes) *
Memory available for all arrays: 10095 MB (1.059e+10 bytes) *
Memory used by MATLAB: 3693 MB (3.872e+09 bytes)
Physical Memory (RAM): 8173 MB (8.570e+09 bytes)
Limited by System Memory (physical + swap file) available.
I tried restarting MATLAB -- No luck

The part before / is a column vector (of 65536) elements, as well as the part after that. I guess you wanted to do elementwise division - in such a case, use ./ .
This way, the backslash operator, "dividing" given vectors, creates a matrix with 65536*65536 elements, so it is not surprising that there is a problem with memory.

Related

Matlab: your opinion about a small memory issue working with matrix

I have a small question regarding MATLAB memory consumption.
My Architecture:
- Linux OpenSuse 12.3 64bit
- 16 GB of RAM
- Matlab 2013a 64 bit
I handle a matrix of double with size: 62 x 11969100 (called y)
When I try the following:
a = bsxfun(#minus,y,-1)
or simply
a = minus(y, -1)
I got a OUT of MEMORY error (in both cases).
I've just computed the ram space allocated for the matrix:
62 x 11969100 x 8 = 5.53 GB
Where am I wrong?!
Thanks a lot!
I'm running on Win64, with 16GB RAM.
Starting with a fresh MATLAB, with only a couple of other inconsequential applications open, my baseline memory usage is about 3.8GB. When I create y, that increases to 9.3GB (9.3-3.8 = 5.5GB, about what you calculate). When I then run a = minus(y, -1), I don't run out of memory, but it goes up to about 14.4GB.
You wouldn't need much extra memory to have been taken away (1.6GB at most) for that to cause an out of memory error.
In addition, when MATLAB stores an array, it requires a contiguous block of memory to do so. If your memory was a little fragmented - perhaps you had a couple of other tiny variables that happened to be stored right in the middle of one of those 5.5GB blocks - you would also get an out of memory error (you can sometimes avoid that issue with the use of pack).
The output of memory on windows platform:
>> memory
Maximum possible array: 2046 MB (2.145e+009 bytes) *
Memory available for all arrays: 3226 MB (3.382e+009 bytes) **
Memory used by MATLAB: 598 MB (6.272e+008 bytes)
Physical Memory (RAM): 3561 MB (3.734e+009 bytes)
* Limited by contiguous virtual address space available.
** Limited by virtual address space available.
The output of computer on linux/mac:
>> [~,maxSize] = computer
maxSize =
2.814749767106550e+14 % Max. number of elements in a single array
with some hacks (found here):
>> java.lang.Runtime.getRuntime.maxMemory
ans =
188416000
>> java.lang.Runtime.getRuntime.totalMemory
ans =
65011712
>> java.lang.Runtime.getRuntime.freeMemory
ans =
57532968
As you can see, aside from memory limitations per variable, there are also limitations on total storage for all variables. This is not different for Windows or Linux.
The important thing to note is that for example on my Windows machine, it is impossible to create two 1.7GB variables, even though I have enough RAM, and neither is limited by maximum variable size.
Since carrying out the minus operation will assign a result of equal size to a new variable (a in your case, or ans when not assigning to anything), there need to be at least two of these humongous things in memory.
My guess is you run into the second limit of total memory space available for all variables.
bsxfun is vectorized for efficiency. Typically vectorized solutions require more than just minimal memory.
You could try using repmat, or if that does not work a simple for loop.
In general I believe the for loop will require the least memory.

Out of memory on a rather small matrix

I am doing motion detection on a fairly small video. 56 frames of 288x384xRGB. I keep two copies of it, so it should amount to about 40 Mb tops, including my other variables.
Now, this line gives me an out of memory error
output = uint8(zeros(this.videoHeight,2.*this.videoWidth,3,size(this.originalFrames,4)));
typing memory reports
>> memory
Maximum possible array: 202 MB (2.114e+08 bytes) *
Memory available for all arrays: 863 MB (9.045e+08 bytes) **
Memory used by MATLAB: 527 MB (5.526e+08 bytes)
Physical Memory (RAM): 3071 MB (3.220e+09 bytes)
* Limited by contiguous virtual address space available.
** Limited by virtual address space available.
>>
I'm new to MATLAB, but not totally new to programming. What am i not understanding?
EDIT
So i did som disp'ing:
disp(this.videoHeight)
disp(2.*this.videoWidth)
disp(size(this.originalFrames,4))
produces:
288
768
54
So, it is actually smaller than i suggested...
You should use
zeros(..., 'uint8')
rather than
uint8(zeros(...))
to avoid creating the array in double-precision first, and then copying it to a uint8 array.
I haven't looked in detail but I would be surprised if there weren't quite a bit of overhead imposed by Matlab. You're probably using a lot more memory than you might suspect.
Try dialing down the number of frames you process to see if that fixes the problem.

Matlab - out of memory while calling mex-file in the midde of program

I am asking this question because I failed to solve it myself and hope someone can give me any suggestion.
My problem is:
my program includes 3 parts:
part-1: video encoder,
part-2: channel coding - also call to a mex-files: complie from C-function "encodeBits.c" to Matlab-mexfile "encodeBits.mexw32",
part-3: video decoder.
I also perform 4 checks:
check 1: part-1 + part-2 as a whole. it means without channel coding, program works well.
check 2: pseudo an input to part-2, it means part-2 stays alone. part-2 works well.
part-2 still works well even under this condition:
possible array: **632** MB (6.623e+08 bytes) *
Memory available for all arrays: **1054** MB (1.105e+09 bytes) **
Memory used by MATLAB: 653 MB (6.842e+08 bytes)
Physical Memory (RAM): 2795 MB (2.930e+09 bytes)
check 3: part-1 + part-2 + part-3 as a whole.
memory before part 1 runs:
Maximum possible array: 877 MB (9.199e+08 bytes) *
Memory available for all arrays: 1265 MB (1.326e+09 bytes) **
Memory used by MATLAB: 441 MB (4.628e+08 bytes)
Physical Memory (RAM): 2795 MB (2.930e+09 bytes)
memory before part-2 runs, means after part-1 finished:
Maximum possible array: **869** MB (9.107e+08 bytes) *
Memory available for all arrays: **1270** MB (1.332e+09 bytes) **
Memory used by MATLAB: 430 MB (4.511e+08 bytes)
Physical Memory (RAM): 2795 MB (2.930e+09 bytes)
and it results error right after part-2 starting with error
Error using encodeBits
Out of memory. Type HELP MEMORY for your options.
check 4: tried part-2 alone again after clear all variable from check 3
it still results error
Error using encodeBits
Out of memory. Type HELP MEMORY for your options.
check 5: close Matlab window, open Matlab window and run part-2 alone
it works well.
So, I cannot understand why it happens, I tried to search hard but I still cannot solve it.
Judging by your check under load failing at/after the mex call, and using clear all not fixing it, it sounds like you have a memory leak in your mex code. If the mexFunction allocates memory and doesn't clear it matlab is unable to clear the space, and so it will remain allocated until you shut matlab.

How can we handle large matrices in matlab(larger than 10000x10000)

In my program I am faced with some matrices that are larger than 10000x10000.
I cannot transpose or inverse them, how can this problem be overcome?
??? Error using ==> ctranspose
Out of memory. Type HELP MEMORY for your options.
Error in ==> programname1 at 70
B = cell2mat(C(:,:,s))';
Out of memory. Type HELP MEMORY for your options.
Example 1: Run the MEMORY command on a 32-bit Windows system:
>> memory
Maximum possible array: 677 MB (7.101e+008 bytes) *
Memory available for all arrays: 1602 MB (1.680e+009 bytes) **
Memory used by MATLAB: 327 MB (3.425e+008 bytes)
Physical Memory (RAM): 3327 MB (3.489e+009 bytes)
* Limited by contiguous virtual address space available.
** Limited by virtual address space available.
Example 2: Run the MEMORY command on a 64-bit Windows system:
>> memory
Maximum possible array: 4577 MB (4.800e+009 bytes) *
Memory available for all arrays: 4577 MB (4.800e+009 bytes) *
Memory used by MATLAB: 330 MB (3.458e+008 bytes)
Physical Memory (RAM): 3503 MB (3.674e+009 bytes)
==============================================================================
memory
% Maximum possible array: 1603 MB (1.681e+009 bytes) *
% Memory available for all arrays: 2237 MB (2.346e+009 bytes) **
% Memory used by MATLAB: 469 MB (4.917e+008 bytes)
% Physical Memory (RAM): 3002 MB (3.148e+009 bytes)
I have used sparse for C.
B = cell2mat(C);
clear C %# to reduce the allocated RAM
P=B\b;
Name Size Bytes Class Attributes
B 5697x5697 584165092 double sparse, complex
C 1899x1899 858213576 cell
b 5697x1 91152 double complex
==============================================================================
??? Error using ==> mldivide
Out of memory. Type HELP MEMORY for your options.
Error in ==> programname at 82
P=B\b;
==============================================================================
Edit: 27.05.11
Name Size Bytes Class Attributes
C 997x997 131209188 cell
B 2991x2991 71568648 single complex
Bdp 2991x2991 143137296 double complex
Bsparse 2991x2991 156948988 double sparse, complex
Bdp=double(B);
Bsparse=sparse(Bdp);
I used single precision, witch gave the same accuracy as in double precision
It's better, Am I right?
A few suggestions:
If possible, as #yoda suggested, use sparse matrices
Do you really need the inverse? If you're solving a linear system (Ax=b), you should use MATLAB's backslash operator.
If you really need huge dense matrices, you can harness the memory of several machines using distributed arrays and MATLAB distributed computing server.
3GB isn't alot when each matrix you have is 600 MB all by it's self. If you can't make the algorithmic changes, you need 64-bit matlab on a 64-bit OS, with alot more RAM. It's the only way to get alot of memory. Notice that with 3 GB, Matlab only has 2.2 GB, and the largest chunk is 1.5 GB - that's only 2 of your matricies.
Matlab has an easy way to handling huge matrices of orders like 1000000*1000000.
These matrices are usually sparse matrices and it's not necessary to allocate RAM memory for matrix elements of zero value.
So you should just use this command:
A=sparse(1000000,1000000); "Defining a 1000000 by 1000000 matrix of zeroes."
Then you can set the diagonal nonzero elements by commands like "spdiags".
See this Link : http://www.mathworks.nl/help/matlab/ref/spdiags.html
Note that you can not use "inv" command to invert matrix A, because "inv" made a normal matrix and uses a lot of space of the RAM.(probably with "out of memory" error)
To solve an equation like A*X=B, you can use X=A\B.

Maximum array size in MATLAB?

I'm writing a MATLAB program that will generate a matrix with 1 million rows and an unknown amount of columns (at max 1 million).
I tried pre-allocating this matrix:
a=zeros(1000000,1000000)
but I received the error:
"Maximum variable size allowed by the program is exceeded."
I have a feeling that not pre-allocating this matrix will seriously slow the code down.
This made me curious: What is the maximum array size in MATLAB?
Update: I'm going to look into sparse matrices, because the result I am aiming for in this particular problem will be a matrix consisting for the larger part of zeros.
Take a look at this page, it lists the maximum sizes: Max sizes
It looks to be on the order of a few hundred million. Note that the matrix you're trying to create here is: 10e6 * 10e6 = 10e12 elements. This is many orders of magnitude greater than the max sizes provided and you also do not have that much RAM on your system.
My suggestion is to look into a different algorithm for what you are trying to accomplish.
To find out the real maximum array size (Windows only), use the command user = memory. user.maxPossibleArrayBytes shows how many bytes of contiguous RAM are free. Divide that by the number of bytes per element of your array (8 for doubles) and you know the max number of elements you can preallocate.
Note that as woodchips said, Matlab may have to copy your array (if you pass by value to a subfunction, for example). In my experience 75% of the max possible array is usually available multiple times.
The Limits
There are two different limits to be aware of:
Maximum array size (in terms of number of elements) allowed by MATLAB, regardless of current memory availability.
Current bytes available for a single array -- the (current) maximum possible array size in bytes.
The first limit is what causes "Maximum variable size allowed by the program is exceeded", not the second limit. However the second one is also a practical limit of which you must be aware!
Checking the Limits
The maximum number of elements allowed for an array is checked as follows:
>> [~,maxsize] = computer
maxsize =
2.8147e+14
According to the documentation for the computer command, this returns:
maximum number of elements allowed in a matrix on this version of MATLAB
This is a static MATLAB limit on number of elements, not affected by the state of the computer (hardware specs and current memory usage). And at over 2 petabytes for a double array of that length, it's also way higher than any computer of which I am aware!
On the other hand, the largest practical array size that you can create at any given moment can be checked by the memory command:
>> memory
Maximum possible array: 35237 MB (3.695e+10 bytes) *
Memory available for all arrays: 35237 MB (3.695e+10 bytes) *
Memory used by MATLAB: 9545 MB (1.001e+10 bytes)
Physical Memory (RAM): 24574 MB (2.577e+10 bytes)
* Limited by System Memory (physical + swap file) available.
As the message says, these values are based on actual current memory availability, taking into account both physical memory and the swap file (collectively, virtual memory).
If needed, these values can accessed programmatically by m = memory;.
Adjusting the Limits
The first limit (the hard limit) has been fixed up until R2015a, where it can now be changed (but only reduced to a fraction of system memory) through the following setting:
You can't increase it beyond your system limits.
The second limit obviously has no "setting" in MATLAB since it's based on available memory and computer configuration. Aside from adding RAM, there's not a lot you can do: (1) pack to consolidate workspace memory and perform "garbage collection", but this may only help on certain platforms, and (2) increasing page file size to allow other stuff to swap out and give MATLAB more physical memory. But be cautious when relying on your page file as your computer may become unresponsive if page file thrashing happens.
In older versions of Matlab that don't include the memory command, you can use:
feature memstats
Physical Memory (RAM):
In Use: 738 MB (2e2c3000)
Free: 273 MB (11102000)
Total: 1011 MB (3f3c5000)
Page File (Swap space):
In Use: 1321 MB (529a4000)
Free: 1105 MB (45169000)
Total: 2427 MB (97b0d000)
Virtual Memory (Address Space):
In Use: 887 MB (37723000)
Free: 1160 MB (488bd000)
Total: 2047 MB (7ffe0000)
Largest Contiguous Free Blocks:
1. [at 4986b000] 197 MB ( c585000)
2. [at 3e1b9000] 178 MB ( b2a7000)
3. [at 1f5a0000] 104 MB ( 6800000)
4. [at 56032000] 77 MB ( 4d3e000)
5. [at 68b40000] 70 MB ( 4660000)
6. [at 3a320000] 54 MB ( 3610000)
7. [at 63568000] 45 MB ( 2d48000)
8. [at 35aff000] 40 MB ( 2821000)
9. [at 60f86000] 37 MB ( 25ca000)
10. [at 6f49d000] 37 MB ( 25b3000)
======= ==========
842 MB (34ac0000)
ans =
207114240
You can't suppress the output, but it returns the largest memory block available ( 207,114,240 Bytes / 8 = 25,889,280 doubles )