Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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. – Ymareth Jul 2 '14 at 15:03
Meta-type question: To the Gods of Olympus... Does this deserve the 'bug' tag and if so what's the process? – Ymareth Jul 8 '14 at 9:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.