Explanation
It is not a leak if this is intended, right? It happens because MMA is trying to determine ImageSizeCache for DynamicBox that will be generated.
ImageSizeCache is explained in
tutorial / AdvancedDynamicFunctionality / ImageSizeCache in Dynamic
My approach to fix it
This tutorial will teach us the the fix is to use a fixed value of ImageSizeCache and SynchronousUpdating -> Automatic.
The problem is that one may want to have a better control over SynchronousUpdating at the end.
To circumvent that we can add additional Dynamic which won't be triggered more than needed and will alow to preserve inner Dynamic's options:
ExportString[
Dynamic[
Dynamic[
Print@1
],
SingleEvaluation -> True,
SynchronousUpdating -> Automatic,
ImageSizeCache -> {28., {0., 9.}}
],
"NB"
]
WRI support response
I felt like it is something very fundamental for working with UI developement in MMA so I asked WRI Support to help me.
It took a while but I received an answer, which is to suspend DynamicUpdating when Export takes place.
[...]
For the effect you want, turn off the global DynamicUpdating option
for the fronend just before the call to Export, and then clear that
setting after Export.
For example:
CurrentValue[$FrontEnd, DynamicUpdating] = False;
Export[file, nbexpr]
CurrentValue[$FrontEnd, DynamicUpdating] = Inherited;
[...]
One should know it may not work well with Dynamics that have UpdateInterval option: Toggling DynamicUpdating aborts dynamics with UpdateInterval
ExporttoNBwith dynamic elements does not leak evaluation in dynamics. I don't have the time right now to check whether any such statement was made somewhere in the docs. But in any case, this is certainly an inconvenience, and this behavior seems to be problematic in the majority of cases. – Leonid Shifrin Sep 09 '16 at 13:20ImageSizeCacheis calculated... – Kuba Sep 14 '16 at 10:58