5
$\begingroup$

First of all, all appreciate all suggestions how to rephrase the title to make it more friendly.

Here's the background:

Let's say your top level procedure flow is complicated, involves url calls, calculations etc. You can't check/assert/dialog everything because there is no time for that on prototyping stage. Additionally some things you want to preview later do not need to be wrong, you just want to confirm that call's headers are fine etc.

The common way is to put myPrint function here and there which will log this information somehow and is enabled by MyPackage`Private`$debugMode = True or similar flag.

But,

it may happen that expressions are big, by accident or by nature, yet you want to see them (on demand) anyway.

  • transferring big expressions between Kernel and FrontEnd can be time consuming.

  • rendering them also takes time.

To delay both we can just create a button to prompt content on demand e.g.:

myPrint[label_, expr_] := Module[
  {x = expr}
, Print[
    StringTemplate["`` ``[`` MB]: "][label, Head[x], ByteCount[x]/2.^20]
  , Button[">>", CreateDocument @ ExpressionCell @ x, Method -> "Queued"]
  ]
]

All myPrint sends and FE needs to render is a small expression with a reference to Module's x.

MemoryInUse[]/2.^20   (* something *)

Module[{A}
, A = RandomReal[1, 10^7]
; myPrint["array", A]
]

MemoryInUse[]/2.^20  (* something + ~76MB *)

enter image description here

As we can see by MemoryInUse, x was not garbage collected, as expected, and can be accessed on demand by clicking the >> button. Whether you want to render it verbatim is another story.

Now comes the problem

it is not garbage collected by how to clean up? Clearing messages notebook does not affect memory in use. Should it? I don't know.

How to do this, or how to implement myPrint in a memory friendly way so that it fulfills needs mentioned in background section?

$\endgroup$
3
  • $\begingroup$ Would putting a Remove[x] after the create document statement inside the button serve your purpose? $\endgroup$
    – TimRias
    Dec 5, 2017 at 22:20
  • $\begingroup$ @mmeent it is something but then you would not be able to click again. $\endgroup$
    – Kuba
    Dec 6, 2017 at 10:13
  • $\begingroup$ You will need some criterion on when to clear the data from memory. Upon first use is I guess the most aggressive option that maintains minimal functionality. Much less aggressive would a function that clears all temporary dat stored in this way. Suppose the myPrint function is implemented in a package myPrint, then a function like: " clearPrintData[]:= Remove[myPrintPrivatex$@]" could do the trick. $\endgroup$
    – TimRias
    Dec 6, 2017 at 19:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.