Using MMA 9.0.1 on win7-64.

When creating a large number of java objects I noticed memory being consumed in huge quantities (several Gb). This memory is returned/deallocated after my call completes but is non-linear in the problem size so large problems are unmanageable.

I have reduced the problem to this example (Requires JLink/InstallJava[])...

mem = Reap[
   JavaNew["java.util.Date", 0], {1000}]];][[2, 1]];

   Frame -> True,  FrameLabel -> {"# Objects Created", "Extra Memory Allocated / object"}]

enter image description here

ListPlot[mem, Frame -> True, 
 FrameLabel -> {"# Objects Created", "Total Memory Allocated"}, 
 Epilog -> Text["Not linear!", Scaled[{.3, .35}]]]

enter image description here

You can see, I hope, that the increments in MemoryInUse[] are each time an object is created are not constant (as I would expect) but increase. The first time a date object is created costs about 6kb (which seems a lot in any case), the 1,000 date object costs 14kb.

Why isn't the memory load a constant per object?

A workaround is to wrap each JavaObject creation in JavaBlock but forcing its retention...

mem = Reap[JavaBlock[Do[(Sow[MemoryInUse[]];JavaBlock[KeepJavaObject[JavaNew["java.lang.Long",0]]]),{1000}]];][[2,1]];

This appears to force Mathematica to tidy up its memory management there and then but probably costs a bit of performance.

  • $\begingroup$ Ah yes :) I'd forgotten I'd done that. Yes, linear increase on average. The space/ref at first just increases and then forms series of plateaus of increasing length. Somewhat odd. $\endgroup$ – Ymareth Jul 2 '14 at 15:03
  • $\begingroup$ Meta-type question: To the Gods of Olympus... Does this deserve the 'bug' tag and if so what's the process? $\endgroup$ – Ymareth Jul 8 '14 at 9:00

Your Answer

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

Browse other questions tagged or ask your own question.