Skip to main content

Mathematica memory management for large arrays


I have come across a weird phenomenon in Mathematica when dealing with large arrays. When generating a list with all the possible subsets of three elements of another list (thus having elements which are lists of 3 elements), I have observed that if you extract these elements in three separate arrays, Mathematica uses much less memory to store the data, even if the total number of elements we have is exactly the same. My question is surely naive: why does this happen?


This is a minimal code that sets an example of what I'm saying, recording the memory used by Mathematica in every step:


n = 100;
memvec = {MemoryInUse[]};

timesubset = Subsets[Range[1, n], {3}];

AppendTo[memvec, MemoryInUse[]];

(*Extract all the first elements*)
t0 = timesubset[[All, 1]];
AppendTo[memvec, MemoryInUse[]];

(*Extract all the second elements*)
t1 = timesubset[[All, 2]];
AppendTo[memvec, MemoryInUse[]];


(*Extract all the third elements*)
t2 = timesubset[[All, 3]];
AppendTo[memvec, MemoryInUse[]];

Clear[timesubset];
AppendTo[memvec, MemoryInUse[]];

Clear[t1];
Clear[t2];
Clear[t0];

AppendTo[memvec, MemoryInUse[]];

ListLinePlot[memvec]

I feel there is an important concept on how is the memory managed that I am missing here.



Answer



You can use ByteCount to look at the memory usage. ByteCount is not precise (doesn't take into account sharing), but it will make it easier to understand what is going on.


Let's do this experiment:


In[1]:= list = Subsets[Range[100], {3}];


In[2]:= ByteCount[list]
Out[2]= 19404080

In[3]:= ByteCount[Transpose[list]]
Out[3]= 11642704

Note that Transpose[list] is the same as {list[[All,1]], list[[All,2]], list[[All,3]]}. We get the same result you got: Transpose[list] takes less memory. The reason for this is that every expression, including Lists has some storage overhead in addition to the elements it contains. When Mathematica stores {1,2,3}, i.e. List[1,2,3], it needs to store more than the elements 1, 2 and 3. It need to store the head of the expression, i.e. List, and the number of elements it contains.


Now Transpose[list] only contains four lists. The outer one, and the three inner ones it holds. This is much less overhead than what's necessary for list which has 161700 inner expressions.




A little practical experimentation shows that on my machine a compound expression with no elements, such as List[], takes 40 bytes. Every element takes an additional 8 bytes for the reference (I'm on a 64 it system where pointers are 8 byte long), plus the storage needed for an element itself. An integer takes 16 bytes. Thus {1} will take 64 bytes, out of which 40 are for the compound expression, 16 for the integer 1 and 8 for the pointer to the integer.



If you use this information to estimate how much space list and Transpose[list] should take up, you'll get values which are close to the actual ones, though not identical. I'm not sure what the difference is due to.




This is a good opportunity to talk about packed arrays a bit. While Mathematica's expression format is very general, it is not too efficient either for storage (memory wise) or for numerical computations. To solve this problem, Mathematica can automatically use an alternate storage format called packed arrays. This only works for proper arrays (i.e. lists for which ArrayQ would return True) that contain only numbers of the same kind (integers, reals, complexes). It stores the numbers as a flat array in memory with extra information about the dimensions of the array. Thus the memory needed to store a numerical array with n elements in packed format is just a few bytes more than n*8 bytes (note that a machine precision number takes up 8 bytes).


You can use some Developer` context functions to test if arrays are stored in a packed format (Developer`PackedArrayQ) and to convert between internal storage formats (ToPackedArray and FromPackedArray).


In[4]:= ByteCount[Developer`ToPackedArray[list]]
Out[4]= 3880952

In[5]:= ByteCount[Developer`ToPackedArray@Transpose[list]]
Out[5]= 3880952




A final word about ByteCount and subexpression sharing: ByteCount returns the number of bytes needed to store an expression without any sharing. However, when not using a packed format, Mathematica will usually only store s once in the expression {s, s, s}. Even though s is present three times, these are represented as three references to the same memory location. The function Share[] will try to discover repeated subexpressions and optimise their storage to avoid repetition.


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...