10

When I run a sample script in MATLAB, it says:

Out of memory. Type HELP MEMORY for your options.

When I type "memory", it reports:

Maximum possible array:             156 MB (1.638e+008 bytes) *
Memory available for all arrays:    740 MB (7.756e+008 bytes) **
Memory used by MATLAB:             1054 MB (1.105e+009 bytes)
Physical Memory (RAM):             3070 MB (3.219e+009 bytes)

*  Limited by contiguous virtual address space available.
** Limited by virtual address space available.

Is there any way to get around this error? I'm using Windows XP x32 with MATLAB 2009a.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Contango
  • 76,540
  • 58
  • 260
  • 305

6 Answers6

13

pack does a memory defragmentation. It might help you a bit as far as the contiguous memory available.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marcin
  • 3,437
  • 1
  • 22
  • 15
10

Remember, when MATLAB says it's out of memory, it means it's out of contiguous memory, so rebooting or restarting MATLAB may work.

But, I'd recommend optimizing your code and identifying how you're eating up so much memory. It could be an ill-designed recursive loop, or a bad indexing function (using doubles instead of logicals to index a huge matrix).

I practically lived with memory errors for a while since I was dealing with huge datasets, but there's always a workaround, ask specific questions and you'll be surprised.

Community
  • 1
  • 1
Jacob
  • 34,255
  • 14
  • 110
  • 165
6

Problem fixed.

Under Windows XP x32, I managed to almost double the amount of memory available to MATLAB by editing boot.ini to add the switch /3GB /USERVA=3030

[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /noexecute=optin /fastdetect /3GB /USERVA=3030

Together with reducing our array sizes, this completely fixed the problem :)

I could have also fixed the problem by upgrading to Windows x64 or Windows 7 x64. This act also doubles the amount of memory available to MATLAB, even if you stick with MATLAB x32 and don't upgrade to MATLAB x64. Windows x64 is just far more memory efficient, even with systems that only have 4 GB of physical RAM installed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Contango
  • 76,540
  • 58
  • 260
  • 305
  • 3
    "only have 4GB of physical RAM" -- heh, it's all relative – Jason S Aug 17 '09 at 13:19
  • Yes! When I was 16 years old, and the proud owner of an XT with 640KByte of RAM, I would have never believed it if someone had said "Well, boy, someday you'll make the following comment ..." – Contango May 19 '15 at 14:31
  • 1
    [Timex-Sinclair 1000](http://en.wikipedia.org/wiki/Timex_Sinclair_1000) ca. 1982 had 2K of RAM; we bought a plug-in 16K memory module for something like $40. I work with dsPIC devices for my job, they come with anywhere from 1K to 48K of RAM. The 8-bit PICs have less: some PIC10 parts have 16 bytes (not kilobytes, *bytes*... **BYTES** HA HA HA) of RAM. So even in 2015 it's still relative. :-) – Jason S May 19 '15 at 17:59
  • If one is running win 10 (or anything beyond win xp) on 32 bit machine, you can achieve the same effect by using the command: BCDEdit /set increaseuserva 3072 – Jerry T Jul 27 '16 at 17:00
  • “Together with reducing our array sizes, this completely fixed the problem” Are you sure it’s not just reducing the array sizes that fixed the problem? – Cris Luengo Sep 14 '20 at 13:33
4

Try this, it works well for me.

  • Go to Home -> Preference icon -> General -> Java Heap Memory -> Allocate what size of memory you want
  • In Preference window, go to "Workspace" (out of Java heap memory level) -> See "Matlab Array size limit" Make sure uncheck the 'Limit the maximum array size to a percentage of RAM'. Because you want to extend memory so we don't need this feature.
  • Done.
Vinh Trieu
  • 993
  • 8
  • 12
2

What are you attempting to allocate when it runs out of memory (OOM)? Do you have code to reproduce? A wide range of problems can cause out of memory errors.

To diagnose, use "dbstop if all error" to set a breakpoint on errors. The out of memory will trigger this, and you can use dbup, dbdown, and whos() to see what's consuming memory. Often an OOM is caused by a bad array size or index calculation, not just by big data structures. E.g. this will trigger an OOM in pretty much any 32-bit MATLAB.

>> x = 1;
>> x(2^30) = 2
??? Out of memory. Type HELP MEMORY for your options.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
1

I faced a similar error while running an (old) C file in MATLAB using mex.

I found my solution at this issue on GitLab.

First, uncheck the option "Limit the maximum Array Size to a % of RAM" located under Preferences -> Workspace, as also indicated in this earlier answer.

Once applied, run your C file in the command window using

mex filename.c -compatibleArrayDims
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • @CrisLuengo is the attribute -comptatibleArrayDims similar to increasing the Java Heap Memory option? Because, for some reason, that solution didn't work for me. The MATLAB simply got stuck and closed. Pardon me for my ignorance. – VIREN RAMCHANDANI Sep 15 '20 at 14:16
  • `-compatibleArrayDims` changes `mx...` function calls in your MEX-file to use 32-bit integers for array sizes and indices, and limits the maximum array size. The GitHub issue you linked was caused by an `int` array being used for array sizes, which is the wrong type now as MATLAB expects 64-bit integers. This caused MATLAB to re-interpret that array as a series of 64-bit integers (which they were not) leading it to think that the user requested a ridiculously large array. [cont.] – Cris Luengo Sep 15 '20 at 14:51
  • [cont.] There are two fixes proposed: (1) use a `mwSize` array, as one should according to the documentation, or (2) use `-compatibleArrayDims`, which modifies all function calls to call the legacy version that take `int` as input instead of `mwSize`. `-compatibeArrayDims` is meant to allow newer versions of MATLAB to compile and use old MEX-file code, it does not increase maximum array size (rather, it limits it to what is addressable by 32-bit integers, ~2 GB). [cont.] – Cris Luengo Sep 15 '20 at 14:51
  • @CrisLuengo Thank you for pointing it out to me. Is the edit fine? – VIREN RAMCHANDANI Sep 15 '20 at 15:20