<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/236532</link>
    <title>MATLAB Central Newsreader - Out of memory  when allocating</title>
    <description>Feed for thread: Out of memory  when allocating</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 25 Sep 2008 10:43:01 -0400</pubDate>
      <title>Out of memory  when allocating</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/236532#602019</link>
      <author>Ida Haggstrom</author>
      <description>Hi!&lt;br&gt;
I'm trying to allocate some empty matrices that I will use in a for loop later on. They're quite big, and I keep getting the &quot;Out of Memory&quot; error message all the time, before I even started with my actual calculations!&lt;br&gt;
Aside from the matrix I already have (matrix1, same size as empty ones), I want to allocate at first one matrix:&lt;br&gt;
&lt;br&gt;
matrix2 = zeros(128,128,63,32);&lt;br&gt;
&lt;br&gt;
After some work on matrix2 and 1, I then clear the original matrix1 and allocate a new one:&lt;br&gt;
&lt;br&gt;
matrix3 = zeros(128,128,63,32);&lt;br&gt;
&lt;br&gt;
Any this is where I get out of memory! I should only have 2 matrices in memory I guess. I changed my RAM from 2046 to to 4096. Can anyone give me some tips how to get around this problem?? Thanks!&lt;br&gt;
/Ida</description>
    </item>
    <item>
      <pubDate>Thu, 25 Sep 2008 11:09:13 -0400</pubDate>
      <title>Re: Out of memory when allocating</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/236532#602024</link>
      <author>Rune Allnor</author>
      <description>On 25 Sep, 12:43, &quot;Ida Haggstrom&quot; &amp;lt;ida_haggst...@yahoo.se&amp;gt; wrote:&lt;br&gt;
&amp;gt; Hi!&lt;br&gt;
&amp;gt; I'm trying to allocate some empty matrices that I will use in a for loop later on. They're quite big, and I keep getting the &quot;Out of Memory&quot; error message all the time, before I even started with my actual calculations!&lt;br&gt;
&lt;br&gt;
First, in matlab an 'empty matrice' is one which size is 0x0. From&lt;br&gt;
the&lt;br&gt;
context I udnderstand your matrices have large sizes, but are&lt;br&gt;
probably&lt;br&gt;
filled with zeros.&lt;br&gt;
&lt;br&gt;
Initializing variables will allocatre memory, even if that memory has&lt;br&gt;
not&lt;br&gt;
yest been initialized by  a program. So it is perfectly possible that&lt;br&gt;
you&lt;br&gt;
run out of memory in the allocation stage.&lt;br&gt;
&lt;br&gt;
&amp;gt; Aside from the matrix I already have (matrix1, same size as empty ones), I want to allocate at first one matrix:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; matrix2 = zeros(128,128,63,32);&lt;br&gt;
&lt;br&gt;
That's some 264 MBytes, assuming double-precision floating&lt;br&gt;
point number format. Doesn't seem too bad.&lt;br&gt;
&lt;br&gt;
&amp;gt; After some work on matrix2 and 1, I then clear the original matrix1 and allocate a new one:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; matrix3 = zeros(128,128,63,32);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Any this is where I get out of memory! I should only have 2 matrices in memory I guess. I changed my RAM from 2046 to to 4096. Can anyone give me some tips how to get around this problem??&lt;br&gt;
&lt;br&gt;
Several suggestions come to mind. One is to re-use the space&lt;br&gt;
of matrix1 for whatever you use matrix3 for. They take up the&lt;br&gt;
same space, so you can re-use that space once you are finished&lt;br&gt;
with matrix1. Doing that is a bit risky, though, as you are balancing&lt;br&gt;
on the RAM limit of your system. It doesn't take more than one&lt;br&gt;
extra function call before the system blows up with another 'out of&lt;br&gt;
memory' error.&lt;br&gt;
&lt;br&gt;
The other suggestion is to break down the data flow. A 4D data&lt;br&gt;
structure indicates to me that it is possible to structure the&lt;br&gt;
data flow differently, to spend less space.&lt;br&gt;
&lt;br&gt;
Utilizing the available space has always been a bottleneck&lt;br&gt;
in large-volume data processing. The trick is to read the&lt;br&gt;
data in in manageable chunks, so that each chunk fit&lt;br&gt;
comprtably into available RAM, at the same time one&lt;br&gt;
reduces the disc accesses in order to redeuce I/O overhead.&lt;br&gt;
&lt;br&gt;
So I would try and restructure the computations into at&lt;br&gt;
least one, maybe two, for-loops and do the computations&lt;br&gt;
on a 2D or 3D array. That would significantly reduce RAM&lt;br&gt;
requirements, safety (as I would save results to file in each&lt;br&gt;
iteration) at the expense of a little bit longer run-time.&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Thu, 25 Sep 2008 11:28:02 -0400</pubDate>
      <title>Re: Out of memory  when allocating</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/236532#602029</link>
      <author>Thomas Clark</author>
      <description>Ida,&lt;br&gt;
&lt;br&gt;
IDEA NUMBER 1&lt;br&gt;
&lt;br&gt;
Lets do some basic maths. MATLAB uses the double precision data type - that means 8 bytes of memory are taken up for every number you use - thats 3 matrices x 128 x 128 x 63 x 32 = 99090432 numbers.&lt;br&gt;
&lt;br&gt;
8 bytes x 99090432 numbers =&amp;gt;&amp;gt;&amp;gt; 756 MB&lt;br&gt;
&lt;br&gt;
So actually all three of your matrices should fit well within your 4Gb of ram, even with other system processes running (I presume you're tried shutting down other programs etc that might be using memory)&lt;br&gt;
&lt;br&gt;
This indicates that there might be a problem with the program. You can try using the whos command to determine what variables are taking up what amount of memory. It may be that you're preallocating in the wrong place - or multiple times, or you have a memory 'leak' (i.e. there's something in a loop that keeps growing... use 'whos' to identify variables that are likely candidates... then use size() command to print out the size of these variables - check its what its supposed to be.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
IDEA NUMBER TWO&lt;br&gt;
&lt;br&gt;
If you've heard of 'passing by reference' then be aware that MATLAB mostly but not always does this. So, say you allocate a matrix in the main program, then pass it to a subroutine:&lt;br&gt;
&lt;br&gt;
% Define main function&lt;br&gt;
function [] = IdasMain()&lt;br&gt;
&lt;br&gt;
% Preallocate&lt;br&gt;
matrix1 = zeros(128,128,63,32);&lt;br&gt;
&lt;br&gt;
% Use a subfunction&lt;br&gt;
result = IdasSub(matrix1)&lt;br&gt;
&lt;br&gt;
end % END MAIN FUNCTION&lt;br&gt;
&lt;br&gt;
If your subfunction uses matrix1 but does not alter its content in any way, you're fine. However, if your subfunction looks like this:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
% Define a subfunction, or other m-file&lt;br&gt;
function [result] = IdasSub(matrix1)&lt;br&gt;
&lt;br&gt;
matrix1 = someOperation(matrix1);&lt;br&gt;
&lt;br&gt;
end % END SUB FUNCTION&lt;br&gt;
&lt;br&gt;
... Then it performs an operation on matrix1, but does not pass it back to the main function. As the operation is performed, a copy of matrix1 is made and the operation performed on that copy, instead of the operation being made on the existing variable. In that case, you end up with TWO variables stored in memory - both called matrix1 - one of these variables is known to the subfunction, the other known to the main function.&lt;br&gt;
&lt;br&gt;
If this is your problem, you'll need to restructure. &lt;br&gt;
&lt;br&gt;
I'm not too hot on this topic, but it's a possibility that if you write the subfunction as &lt;br&gt;
[results, matrix1] = IdasSub(matrix1)&lt;br&gt;
that its entirely passed by reference. Of course, you then end up with the altered version of matrix1 in your function.&lt;br&gt;
&lt;br&gt;
IDEA NUMBER 3&lt;br&gt;
&lt;br&gt;
If numerical precision isn't critical... then try altering the data type to 'single', with the command single(matrix1). You'd need to change the other variables that operate on matrix1 too. Doing this, you revert to single-precision accuracy, so use 4 bytes per number instead of 8 - thus your matrices take up only 378MB.&lt;br&gt;
&lt;br&gt;
This could be ill advised - be sure to read up on the rules pertaining to data types found in the help documentation so you're sure you know what type of variable is what.&lt;br&gt;
&lt;br&gt;
Beware that some (many!) functions can't work with single data types.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
IDEA NUMBER FOUR&lt;br&gt;
&lt;br&gt;
If a high proportion (at least half, preferably more) of the values in your matrix are zeros, then try using the sparse() format - check it out in the help documentation.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I hope this helps&lt;br&gt;
&lt;br&gt;
Tom Clark</description>
    </item>
    <item>
      <pubDate>Mon, 29 Sep 2008 10:05:04 -0400</pubDate>
      <title>Re: Out of memory  when allocating</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/236532#602738</link>
      <author>Ida Haggstrom</author>
      <description>Thanks both Tomas and Rune for all your help!&lt;br&gt;
The tips are very good and I will surely check them out! For sure I'm running on the verge of memory breakdown... =) &lt;br&gt;
I did find some documentation (Matlab Help under &quot;resolving 'Out of memory' errors&quot;) on how to increase memory available to Matlab, by adding a line in the system startup document for windows. This actually worked, so now I can run my functions without errors. I suspect I'm still playing with fire though, and will probably have to follow your advice to do some for-loops instead. Thanks for your time guys! Cheers,&lt;br&gt;
Ida&lt;br&gt;
&lt;br&gt;
&quot;Thomas Clark&quot; &amp;lt;t.clarkremove_spam@cantab.net&amp;gt; wrote in message &amp;lt;gbfsk2$9k3$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Ida,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; IDEA NUMBER 1&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Lets do some basic maths. MATLAB uses the double precision data type - that means 8 bytes of memory are taken up for every number you use - thats 3 matrices x 128 x 128 x 63 x 32 = 99090432 numbers.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 8 bytes x 99090432 numbers =&amp;gt;&amp;gt;&amp;gt; 756 MB&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So actually all three of your matrices should fit well within your 4Gb of ram, even with other system processes running (I presume you're tried shutting down other programs etc that might be using memory)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This indicates that there might be a problem with the program. You can try using the whos command to determine what variables are taking up what amount of memory. It may be that you're preallocating in the wrong place - or multiple times, or you have a memory 'leak' (i.e. there's something in a loop that keeps growing... use 'whos' to identify variables that are likely candidates... then use size() command to print out the size of these variables - check its what its supposed to be.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; IDEA NUMBER TWO&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If you've heard of 'passing by reference' then be aware that MATLAB mostly but not always does this. So, say you allocate a matrix in the main program, then pass it to a subroutine:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Define main function&lt;br&gt;
&amp;gt; function [] = IdasMain()&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Preallocate&lt;br&gt;
&amp;gt; matrix1 = zeros(128,128,63,32);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Use a subfunction&lt;br&gt;
&amp;gt; result = IdasSub(matrix1)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; end % END MAIN FUNCTION&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If your subfunction uses matrix1 but does not alter its content in any way, you're fine. However, if your subfunction looks like this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Define a subfunction, or other m-file&lt;br&gt;
&amp;gt; function [result] = IdasSub(matrix1)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; matrix1 = someOperation(matrix1);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; end % END SUB FUNCTION&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ... Then it performs an operation on matrix1, but does not pass it back to the main function. As the operation is performed, a copy of matrix1 is made and the operation performed on that copy, instead of the operation being made on the existing variable. In that case, you end up with TWO variables stored in memory - both called matrix1 - one of these variables is known to the subfunction, the other known to the main function.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If this is your problem, you'll need to restructure. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'm not too hot on this topic, but it's a possibility that if you write the subfunction as &lt;br&gt;
&amp;gt; [results, matrix1] = IdasSub(matrix1)&lt;br&gt;
&amp;gt; that its entirely passed by reference. Of course, you then end up with the altered version of matrix1 in your function.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; IDEA NUMBER 3&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If numerical precision isn't critical... then try altering the data type to 'single', with the command single(matrix1). You'd need to change the other variables that operate on matrix1 too. Doing this, you revert to single-precision accuracy, so use 4 bytes per number instead of 8 - thus your matrices take up only 378MB.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This could be ill advised - be sure to read up on the rules pertaining to data types found in the help documentation so you're sure you know what type of variable is what.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Beware that some (many!) functions can't work with single data types.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; IDEA NUMBER FOUR&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If a high proportion (at least half, preferably more) of the values in your matrix are zeros, then try using the sparse() format - check it out in the help documentation.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I hope this helps&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Tom Clark&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; </description>
    </item>
  </channel>
</rss>

