Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the list of facts I collected. Instead of GC the term <em>memory (de)allocation</em> seems to be more appropriate in this context.</p> <p>My principal information source is the blog of Loren (especially its comments) and <a href="http://www.mathworks.com/company/newsletters/digest/2008/sept/matlab-objects.html" rel="nofollow noreferrer">this</a> article from MATLAB Digest.</p> <p>Because of its orientation for numeric computing with possible large data sets, MATLAB does really good job on optimizing <a href="http://mathforum.org/kb/thread.jspa?threadID=265365" rel="nofollow noreferrer">stack objects</a> performance like using <a href="http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/" rel="nofollow noreferrer">in-place operations on data</a> and <a href="http://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/" rel="nofollow noreferrer">call-by-reference</a> on function arguments. Also because of its orientation its memory model is fundamentally <a href="http://blogs.mathworks.com/loren/2008/03/04/release-r2008a-available/#comment-27674" rel="nofollow noreferrer">different</a> from such OO languages as Java.</p> <p>MATLAB had officially no user-defined heap memory until version 7 (in version 6 there was undocumented <code>reference</code> functionality in <code>schema.m</code> files). MATLAB 7 has heap both in form of <a href="http://www.c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent" rel="nofollow noreferrer">nested functions (closures) and handle objects</a>, their implementation share the same underpinnings. As a side note OO could be <a href="http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/" rel="nofollow noreferrer">emulated</a> with closures in MATLAB (interesting for pre-2008a). </p> <p>Surprisingly it is possible to examine entire workspace of the enclosing function captured by function handle (closure), see function <a href="http://www.mathworks.com/help/techdoc/ref/functions.html" rel="nofollow noreferrer">functions(fhandle)</a> in MATLAB Help. It means that enclosing workspace is being <em>frozen</em> in memory. This is why <code>cellfun/arrayfun</code> are sometimes very slow when used inside nested functions.</p> <p>There are also interesting posts by <a href="http://blogs.mathworks.com/loren/2008/07/29/understanding-object-cleanup/" rel="nofollow noreferrer">Loren</a> and <a href="http://blogs.mathworks.com/loren/2008/03/10/keeping-things-tidy/" rel="nofollow noreferrer">Brad Phelan</a> on object cleanup.</p> <p>The most interesting fact about heap deallocation in MATLAB is, in my opinion, that MATLAB tries to do it each time the stack is being deallocated, i.e. on leaving every function. This has <a href="http://www.mathworks.com/company/newsletters/digest/2008/sept/matlab-objects.html" rel="nofollow noreferrer">advantages</a> but is also a huge CPU penalty if heap deallocation is slow. And it is actually very slow in MATLAB in some scenarios!</p> <p>The performance problems of MATLAB memory deallocation that can hit code are pretty bad. I always notice that I unintentionally introduce a cyclic references in my code when it suddenly runs x20 slower and sometimes needs some seconds between leaving function and returning to its caller (time spent on cleanup). It is a known problem, see <a href="http://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-29947" rel="nofollow noreferrer">Dave Foti</a> and <a href="http://mathforum.org/kb/message.jspa?messageID=6015847&amp;tstart=0" rel="nofollow noreferrer">this older forum post</a> which code is used to make this picture visualizing performance (tests are made on different machines, so absolute timing comparison of different MATLAB versions is meaningless):</p> <p>Linear increase of pool size for reference-objects means polynomial (or exponential) decrease of MATLAB performance! For value-objects the performance is, as expected, linear.</p> <p>Considering these facts I can only speculate that MATLAB uses not yet very efficient form of <em>reference counting</em> for heap deallocation.</p> <p><strong>EDIT</strong>: I always encountered performance problem with <em>many small</em> nested functions but recently I noticed that at least with 2006a the cleanup of a <em>single nested scope</em> with some megabytes of data is also terrible, it takes 1.5 seconds just to set nested scope variable to empty!</p> <p><strong>EDIT 2</strong>: finally I got the answer - <a href="http://blogs.mathworks.com/loren/2011/03/08/common-design-considerations-for-object-properties/#comment-32092" rel="nofollow noreferrer">by Dave Foti himself</a>. He acknowledges the flaws but says that MATLAB is going to retain its present deterministic cleanup approach.</p> <p><strong>Legend: Shorter execution time is better</strong></p> <p><img src="https://i.stack.imgur.com/FUYi7.gif" alt="R2006a"> <img src="https://i.stack.imgur.com/aqvnK.gif" alt="R2008a"> <img src="https://i.stack.imgur.com/fIt4Y.gif" alt="R2009a"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload