{"id":10775,"date":"2019-05-31T09:00:27","date_gmt":"2019-05-31T13:00:27","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=10775"},"modified":"2019-05-30T21:21:19","modified_gmt":"2019-05-31T01:21:19","slug":"memory-info","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2019\/05\/31\/memory-info\/","title":{"rendered":"Memory Info"},"content":{"rendered":"<div xmlns:mwsh=\"http:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45598\">Memory Info<\/a> by <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/4254251\">Sviatoslav Danylenko<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>Have you ever wanted to get the current memory used by MATLAB?  I do this occassionally at the command line and have done this in code to prevent creating an array that would cause me to use more memory than available RAM.\n   <\/p>\n<p>A few years ago development added the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/matlab_env\/set-workspace-and-variable-preferences.html#bqxytks-1\">ability to limit array size<\/a> as a preference, but that is a setting and is not something that can be used on the fly in code.\n   <\/p>\n<p>If you&#8217;re on Windows, you can run the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/ref\/memory.html\"><tt>memory<\/tt><\/a> command.  If you&#8217;re not on Windows or need to write cross-platform code, then Sviatoslav&#8217;s function is for you.  It gives the same outputs, in the same units, on all three supported platforms.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=5938\">fn_structdisp<\/a>(memoryInfo)<\/pre>\n<pre style=\"font-style:oblique\">:\r\n         total: 16612188\r\n          free: 7360540\r\n          used: 9251648\r\n         cache: 0\r\n    usedMatlab: 6529128\r\n          swap: [1&times;1 struct]\r\n.total:\r\n    16612188\r\n.free:\r\n     7360540\r\n.used:\r\n     9251648\r\n.cache:\r\n   0\r\n.usedMatlab:\r\n     6529128\r\n.swap:\r\n         total: 1.3744e+11\r\n          free: -2288104\r\n          used: 1.3744e+11\r\n    usedMatlab: 0\r\n.swap.total:\r\n   1.3744e+11\r\n.swap.free:\r\n    -2288104\r\n.swap.used:\r\n   1.3744e+11\r\n.swap.usedMatlab:\r\n   0\r\n<\/pre>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = rand(12000);<\/pre>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fn_structdisp(memoryInfo)<\/pre>\n<pre style=\"font-style:oblique\">:\r\n         total: 16612188\r\n          free: 6230044\r\n          used: 10382144\r\n         cache: 0\r\n    usedMatlab: 7654196\r\n          swap: [1&times;1 struct]\r\n.total:\r\n    16612188\r\n.free:\r\n     6230044\r\n.used:\r\n    10382144\r\n.cache:\r\n   0\r\n.usedMatlab:\r\n     7654196\r\n.swap:\r\n         total: 1.3744e+11\r\n          free: -2286092\r\n          used: 1.3744e+11\r\n    usedMatlab: 0\r\n.swap.total:\r\n   1.3744e+11\r\n.swap.free:\r\n    -2286092\r\n.swap.used:\r\n   1.3744e+11\r\n.swap.usedMatlab:\r\n   0\r\n<\/pre>\n<h3>Comments<a name=\"4\"><\/a><\/h3>\n<p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=10775#respond\">here<\/a> or leave a <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45598#comments\">comment<\/a> for Sviatoslav.\n   <\/p>\n<p><script language=\"JavaScript\">\n<!--\n\n    function grabCode_31136fd541a0436fa812980b82266bfb() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='31136fd541a0436fa812980b82266bfb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 31136fd541a0436fa812980b82266bfb';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Sean de Wolski';\n        copyright = 'Copyright 2019 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }   \n      \n-->\n<\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\">\n<p>Published with MATLAB&reg; R2019a<\/p>\n<\/div>\n<p><!--\n31136fd541a0436fa812980b82266bfb ##### SOURCE BEGIN #####\n%% Memory Info\n%\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45598 Memory Info> by\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/4254251 Sviatoslav Danylenko>.\n% \n\n%% \n%\n% Have you ever wanted to get the current memory used by MATLAB?  I do this\n% occassionally at the command line and have done this in code to prevent\n% creating an array that would cause me to use more memory than available\n% RAM.  \n%\n% A few years ago development added the\n% <https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/matlab_env\/set-workspace-and-variable-preferences.html#bqxytks-1\n% ability to limit array size> as a preference, but that is a setting and\n% is not something that can be used on the fly in code.\n%\n% If you're on Windows, you can run the\n% <https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/ref\/memory.html\n% |memory|> command.  If you're not on Windows or need to write\n% cross-platform code, then Sviatoslav's function is for you.  It gives the\n% same outputs, in the same units, on all three supported platforms.\n\nfn_structdisp(memoryInfo)\n%%\nx = rand(12000);\n%%\nfn_structdisp(memoryInfo)\n\n\n%% Comments\n% \n% Give it a try and let us know what you think\n% <http:\/\/blogs.mathworks.com\/pick\/?p=10775#respond here> or leave a\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45598#comments\n% comment> for Sviatoslav.\n%\n \n\n##### SOURCE END ##### 31136fd541a0436fa812980b82266bfb\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sean&#8216;s pick this week is Memory Info by Sviatoslav Danylenko.<\/p>\n<p>Have you ever wanted to get the current memory used by MATLAB?  I do this occassionally at the command line and&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2019\/05\/31\/memory-info\/\">read more >><\/a><\/p>\n","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10775"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=10775"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10775\/revisions"}],"predecessor-version":[{"id":10783,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10775\/revisions\/10783"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=10775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=10775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=10775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}