# Kernel inexplicably hangs if I define a pure function without suppressing output

Bug introduced in 9.0 and fixed in 10.0.2

Save any unsaved work before trying as it may crash Mathematica and other programs by filling up the memory!

In v9.0.1.0 on OSX 10.8.5, if I start a new session and define two functions

f1P = Sin[#^Sin[Sqrt[Gamma[#]]]] &;


and

f2P = Sin[#^Sin[Gamma[(# + 1)]]] &;


they both work fine. Now, if I quit the kernel and define the exact same functions but removing the ;s when I evaluate the cell with f1P:

f1P = Sin[#^Sin[Sqrt[Gamma[#]]]] &


I get some output (Sin[#^Sin[Sqrt[Gamma[#]]]] &). Then if I try to evaluate the cell with the second function definition the kernel hangs indefinitely. In fact the kernel can't handle the definition of f1P without ; at all (if I try to give it values nothing happens). Strangely, this isn't the case with f2P: I remove the semicolon, evaluate and it runs fine. But with f1P the kernel consumes more and more CPU memory. I have not idea why CPU usage spiked yesterday - I tried this today and it increased to ~35% but not more. The memory demand keeps growing though until the Kernel gets terminated or until Mathematica crashes.

This seems to be a bug with Mathematica on OSX as other people confirmed and I have now reported it to WR so I will edit again once I have confirmation from them. Still, if anyone has any idea what might be happening I'd like to know.

---EDIT---

This issue has been resolved in version 10. WR have confirmed the bug which "causes unusual memory usage on a Mac because of the Predictive Interface"

-
No problem on my system (Windows). Seems like a very strange kind of hang. – Oleksandr R. Nov 8 '13 at 16:59
I can reproduce what you see on OS X 10.9.0, M9.0.1. It eats up all the memory, so anyone trying this, be careful!! It hung another program on my system and I lost a document. I had to kill the remaining MathKernels by hand. I think you should report this bug. – Szabolcs Nov 8 '13 at 20:04
Have you reported this? – rcollyer Nov 9 '13 at 4:06
Has anyone reproduced this in a version other than 9.0.1? – Mr.Wizard Nov 9 '13 at 13:40

This specific problem has indeed been resolved as of version 10.0.2, although it is still possible to run into similar behavior in other computations. The Mac failure mode is worse than on other systems.

To give an idea what was behind the system freeze, one of the operations attempted in the background by the Predictive Interface was a heuristic (based on Wolfram|Alpha code) aiming to find "interesting" plot ranges. At some point this routine was trying to solve the following ODE

NDSolve[{Derivative[1][f][x] == x^Sin[Sqrt[Gamma[x]]]*
Cos[x^Sin[Sqrt[Gamma[x]]]]*((Cos[Sqrt[Gamma[x]]]*Sqrt[Gamma[x]]*Log[x]*
PolyGamma[0, x])/2 + Sin[Sqrt[Gamma[x]]]/x), f[0.00031328334725241403] == 0},
f, {x, 0.00031328334725241403, -999.9996867166527}, PrecisionGoal -> 2, MaxSteps -> 1000]


On operating systems like Linux and Windows, this call would quickly fail with a General::nomem message and return

SystemException["MemoryAllocationFailure"]


since the following intermediate number

Cos[-4.58522132471158144626987635475521616\
3333161912637025515.954589770191005*^-527458083171
- 3.0631280865040788933314322086592174\
7953962658848367215.954589770191005*^-527458083171 I]


requires a huge amount of memory to compute -- it is a somewhat extreme example of precision coercion similar to what happens in Precision[1 - 1.*^-20000] or Precision[Cos[1.*^-10000]] and the OS refuses to allocate it.

On Mac OS X, the operating system behavior is more interesting; it actually pretends to succeed in allocating the huge chunk of memory requested by the kernel, in essence assuming unlimited amounts of virtual memory, and that is the cause of the hang.

While the underlying numerical computation issue still exists, the Predictive Interface will no longer attempt such predictions for pure function input.

-
So can we conclude that this is another hint that Mathematica does not run best on MacOSX ? I worked on Linux, Windows and sometimes on MacOSX and I find Mathematica runs best on Windows ... – Rolf Mertig Sep 25 '15 at 13:37
No, I don't think that this example justifies such a conclusion. – ilian Sep 25 '15 at 15:41

This isn't exactly an answer, but more of an observation.

I'm running V.9.0.1 on OS X 10.6.8. When I try to work with your functions f1P and f2P, I experience behavior that is somewhat different from the behavior you describe, but certainly not any more pleasant.

With a fresh kernel I can evaluate either function definition without any apparent problem. However, as soon as I try to perform another evaluation of any kind, even 2+ 2, one the two running kernels grabs every bit of available memory and continues requesting system memory indefinitely (i.e., until I kill it). This causes my system to swap memory pages out to my system hard drive and causes all running processes, not just the ones Mathematica is running, to slow down dramatically. CPU usage remains moderate.

From my experiments, I would say this is a bug and a nasty one at that.

-