I want to instrument a Mathematica script so that, upon receiving any system signal (e.g. SIGINT
, aka Ctrl-C
) it will print1 the value returned by MaxMemoryUsed[]
.
I have read this earlier answer and the LibraryLink
docs, but I still can't figure out how to adapt the solution given in that answer so that my_handler
can invoke MaxMemoryUsed
, and print out its result. IOW, I can't figure out what to replace the ???
below with so that my_handler
's printout shows the value returned by MaxMemoryUsed[]
:
void my_handler (int s) {
fprintf(stderr, "MaxMemoryUsed[] returned: %d\n", ???);
/* ------------------------------------------------------- */
fprintf(stderr, "Received signal: %d\n", s);
/* additional signal-handling code follows */
}
(One other possible way to achieve what I'm trying to do would be something like an atexit
mechanism, but I have not been able to find anything like that for Mathematica. Yet a third possibility would be to implement the script so that it periodically prints the value of MaxMemoryUsed
to a file. Unfortunately, for most of the script's lifetime, it is evaluating a single, irreducible Mathematica expression, and I don't know how to implement frequent periodic updating of an output file under these circumstances. In any case, even if I knew how to do it, this last alternative is a distant second-best.)
1 preferably to stderr, but either stdout or some file is also OK.
MaxMemoryUsed
is critical. $\endgroup$