36

Bug was introduced in 11.0 and persisting through 11.1


It looks like the Mathematica 11 stack tracing feature (see red ellipsis in front of any warning message) prevents the garbage collector from removing temporary variables. For example, the following code leaves three copies of hugeTempVar in memory after each evaluation.

test[a_]:= Module[{hugeTempVar},
    hugeTempVar = ConstantArray[a, 1000];
    a/0; (* some code which produces a message *)
    a
    ];

Table[test[i], {i, 1, 10}];

Names[$Context<>"*$*"]

If one removes message-producing code a/0 there is no memory leak.

In this particular example, one can also use ParallelTable to enable old-style messages without a stack trace and thus prevent a memory leak.

I don't want to disable messages, because they provide important diagnostic information.

Is there a possibility to disable stack tracing, but keep the messages?

corey979
  • 23,947
  • 7
  • 58
  • 101
Ray Shadow
  • 7,816
  • 1
  • 16
  • 44
  • 2
    The memory leak is reproduced with version 11.0.1 on Windows 7 x64. With version 10.4.1 there is no memory leak. – Alexey Popkov Mar 17 '17 at 14:42
  • 2
    I would suggest filing a report with Wolfram on this, I think it's annoying enough that it's worth bringing to their attention and letting them decide. – ktm Mar 17 '17 at 15:13
  • 4
    See here: http://mathematica.stackexchange.com/q/123965/12 – Szabolcs Mar 17 '17 at 15:13
  • @Kuba The "Stack Trace ..." window which can be opened from the ellipsis in front of the warning message doesn't show the value of hugeTempVar$541, it does show the code hugeTempVar$541 = ConstantArray[1, 1000] what doesn't require keeping the actual variable hugeTempVar$541. – Alexey Popkov Mar 17 '17 at 15:26
  • @AlexeyPopkov you see, I missed the point. Let me delete my confusing comments – Kuba Mar 17 '17 at 15:28

2 Answers2

29

Is there a possibility to disable stack tracing, but keep messages?

Internal`$MessageMenu = False

reverts back to the old messages. Seems to do the trick and prevent the leak from my testing.

ktm
  • 4,242
  • 20
  • 28
26

Analysis current as of Mathematica version 11.0.1 and 11.1.0.

We can disable the Show Stack Trace item in the new message menu as follows:

MessageMenu`$PruneStack;
MessageMenu`Dump`$IncludeStack = False;

The reference to MessageMenu`$PruneStack is there to ensure that the message menu packages have been autoloaded (otherwise our setting will be lost should the packages be loaded later).

Stack information is stored as down-values on the symbol MessageMenu`MessageStackList. We can reclaim any memory from earlier messages by executing ClearAll on this symbol. We can also inspect these down values to verify that the $IncludeStack setting is taking effect.

WReach
  • 68,832
  • 4
  • 164
  • 269