Re: Will adding RAM help "No more memory available" error?
- To: mathgroup at smc.vnet.net
- Subject: [mg90167] Re: Will adding RAM help "No more memory available" error?
- From: RiffenistRiffRaff at gmail.com
- Date: Tue, 1 Jul 2008 06:59:46 -0400 (EDT)
- References: <g3agpd$pka$1@smc.vnet.net>
Hey there, Here is some information that might be useful. Depending on the code you are running it is possible to obtain the message ~ No more memory available. Mathematica kernel has shut down. Try quitting other applications and then retry. if you have not correctly handled the local variables in Modules[ ] called from the main program. I experienced a similar error message working on a large data set, and after some trouble shooting I discovered the following. I am by no means an expert at Mathematica, nonetheless some users might find this helpful. If I have made errors in my discussion please let me know. I have included some small "artificial" code that simply explains what can go wrong if modules are not written within context (pun intended ~ see below) of Mathematica. You may also use the code snippets to do your own experiments. The MAIN program is simple. It tracks memory usage and performs a While[ ] loop creating a large list (see I below). This list is called x and is constructed with x = Append[ x, Range[1, 10^6] ]. At the end of the program, before exiting, the variable x is cleared and one last call to MemoryInUse[ ] is made. Within the MAIN loop a call is made to the function MSTEPPER (see IIa below) which returns its own large list. In fact MSTEPPER is just the main program written as a module, with two local variables { m0, y0 }. The list it builds is returned to the calling program as y0. Both y0 and m0 appear upfront in a ClearAll[ ] statement. If you run MAIN you find that at the start there is around 2 meg of overhead. Just prior to the Exit[ ] of MAIN there is 63 meg of memory used. It would appear that all variables have been cleared and so MemoryInUse[ ] should be the overhead of around 2 meg. At least part of the answer, is that although y0 is a local variable, memory is not reclaimed. There is a memory leak. To get a clearer picture try uncommenting the two lines in the loop of the MAIN program. The MemoryInUse[ ] might be expected to stay low since we are continually clearing x. It does not. So what have I done wrong? Look at MSTEPPER of IIb. Here I build a package using BeginPackage and EndPackage. I create a subexpression using Begin and End. This creates y0 as a private variable with context limited to the module MSTEPPER. Now when you run main you will see that the memory used for y0 is reclaimed. Hope this helps. It plugged the leak I created, which caused the out of memory error. Pete I. MAIN PROGRAM RUTE = "C:/Program Files/Wolfram Research/Mathematica/5.2/ myMathematica/"; SetDirectory[RUTE]; <<"mstepper.m"; mem = { }; mem = Append[ mem, MemoryInUse[ ] ]; byte = { }; meg = Power[ 2.0, 20 ]; x = { }; n = 1; While[ n =98 4, { (* x = { ]; *) x = Append[x, mstepper[ ] ]; byte = Append[ byte, ByteCount[ x ] ]; mem = Append[ mem, MemoryInUse[ ] ]; (* ClearAll[ x ]; *) }; n++ ]; Dimensions[ x ] mem/meg byte/meg ClearAll[ x ]; MemoryInUse[ ]/meg Exit[ ]; IIa. MODULE MSTEPPER WITHOUT CONTEXT mstepper[ ] := Module[ { m0, y0 }, ClearAll[ m0, y0 ]; y0 = { }; m0 = 1; While[ m0 =98 4, { y0 = Append[ y0, Range[ 1, 10^6 ] ]; }; m0++ ]; Return[ y0 ]; ]; IIb. MODULE MSTEPPER WITH CONTEXT BeginPackage["mstepper`"]; mstepper::"usage" = "Will this work?"; Begin["`Private`"]; mstepper[ ] := Module[ { m0, y0 }, ClearAll[ m0, y0]; y0 = { }; m0 = 1; While[ m0 =98 4, { y0 = Append[ y0, Range[ 1, 10^6 ] ]; }; m0++ ]; Return[ y0 ]; ];