10
$\begingroup$

How to abort or stop this (accidently large defined) SparseArray production?

s = N[SparseArray[Table[{2^i, 4} -> i, {i, 30}]]]  

Alt+. or Alt+, seem to give up. Even your Windows task manager is struggling.

$\endgroup$
  • 10
    $\begingroup$ Evaluation->Quit Kernel -> Local often succeeds where Abort Evaluation fails for me. $\endgroup$ – image_doctor Jan 13 '13 at 16:42
  • 1
    $\begingroup$ Related: Do you really want to quit the kernel? Yes!, How to automate a FrontEnd return?. $\endgroup$ – István Zachar Jan 13 '13 at 17:17
  • 1
    $\begingroup$ Concerning the Task Manager statement: Although the TM struggles, and can take a minute or two to respond, I find that patience pays off: navigate to the [Processes] tab, right-click on the instance of Mathkernel.exe that is hogging all the RAM, and choose "end process." Each step can require a painfully long wait, but in the end it does work. $\endgroup$ – whuber Jan 13 '13 at 19:12
  • 1
    $\begingroup$ Another related question: mathematica.stackexchange.com/q/2789/5 $\endgroup$ – rm -rf Jan 13 '13 at 20:58
10
$\begingroup$

Could use $Pre to wrap things in MemoryConstrained. I'll illustrate with an unusually tight constraint.

SetAttributes[memcon, HoldAll]
memcon[new_] := MemoryConstrained[new, 10^4]
$Pre = memcon;

Examples:

ByteCount[Range[10^6]]

(* Out[4]= $Aborted *)

s = N[SparseArray[Table[{2^i, 4} -> i, {i, 20}]]]

(* Out[5]= $Aborted *)
$\endgroup$
  • 3
    $\begingroup$ If you don't preclude the use of $Pre as I did in my earlier question this is an excellent method. I recommend it to anyone using version 7 or earlier. Belisarius gives a nice method for v8+ in the linked thread that does not tie up $Pre. $\endgroup$ – Mr.Wizard Jan 14 '13 at 1:14
  • $\begingroup$ Another question would be: why does this fill up the memory at all? The array is very sparse with only a few elements. Is there a limit on the size of SparseArrays that's exceeded here? $\endgroup$ – Szabolcs Jan 29 '13 at 22:25
  • $\begingroup$ @Szabolcs InputForm[SparseArray[Table[{2^i, 4} -> i, {i, 5}]]] seems to show a dense 1-d listwith the jth element indicating how many nontrivial elements have been seen prior to row j. That at least is my guess. $\endgroup$ – Daniel Lichtblau Jan 29 '13 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.