Reported to WRI [CASE:3406803]
Why does generating an array in "C"-compiled function uses 50% more memory than doing same thing in "WVM"-compiled function?
Does this depend on OS or used C compiler?
Simple example:
ClearAll[zeroArrayWVM, zeroArrayC]
zeroArrayWVM =
Compile[{{i, _Integer}}, Table[0, {i}], CompilationTarget -> "WVM"];
zeroArrayC =
Compile[{{i, _Integer}}, Table[0, {i}], CompilationTarget -> "C"];
TableForm[
Table[
{#1, #2, N[#2/#1]} &[
zeroArrayWVM[i] // MaxMemoryUsed,
zeroArrayC[i] // MaxMemoryUsed
],
{i, 10^Range[8]}
],
TableHeadings -> {None, {"WVM", "C", "C/WVM"}}
]
\begin{array}{lll} \text{WVM} & \text{C} & \text{C/WVM} \\ 320 & 512 & 1.6 \\ 1824 & 2768 & 1.51754 \\ 16544 & 24848 & 1.50193 \\ 160240 & 240392 & 1.5002 \\ 1600240 & 2400392 & 1.50002 \\ 16000240 & 24000392 & 1.5 \\ 160000240 & 240000392 & 1.5 \\ 1600000240 & 2400000392 & 1.5 \\ \end{array}
This results were obtained on Linux x86_64 with gcc 4.8.3, if that is relevant. They are the same in Mathematica versions 9.0, 10.0, 10.1 and 10.2.
Import["http://goo.gl/NaH6rM"]["http://i.stack.imgur.com/GTpLl.png"]If we only could trust thatMaxMemoryUsedis working correctly.. – halirutan Aug 23 '15 at 19:59MaxMemoryUsed, but I don't know why memory was not freed after usingzeroArrayCGen. I had$HistoryLength = 0;and memory was automatically freed afterzeroArrayWVMandzeroArrayC. Do library functions use some additional caching? – jkuczm Aug 23 '15 at 22:41