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

Bug introduced in 8.0 or earlier and persisting through 10.3

This is an example LibaryLink code from Mathematica documentation, which takes an integer and return an integer list:

 $HistoryLength = 0;    

 MMASrc = "
   #include \"WolframLibrary.h\"
   #include \"WolframCompileLibrary.h\"

   DLLEXPORT int demo_I_T(WolframLibraryData libData, 
              mint Argc, MArgument *Args, MArgument Res) {
      MTensor T0;
      mint i, I0, dims[1];
      int err = LIBRARY_NO_ERROR;

      I0 = MArgument_getInteger(Args[0]);
      dims[0] = I0;

      err = libData->MTensor_new(MType_Integer, 1, dims, &T0);
      for ( i = 1; i <= I0 && !err; i++) {
          err = libData->MTensor_setInteger( T0, &i, i*2);
      MArgument_setMTensor(Res, T0);
      return err;

CreateLibrary[MMASrc, "mylib", "TargetDirectory" -> "/tmp"];   
libFunc = LibraryFunctionLoad["/tmp/mylib.dylib", "demo_I_T", {Integer}, {Integer, 1}];

The library function never returns the memory :

Table[Table[libFunc[100], {40000}]; Pause[0.1]; MemoryInUse[]/1024.^2, {10}]
(* {66.3692, 100.548, 134.727, 168.906, 203.085, 237.264, 271.443, 305.622, 339.8, 373.979} *)

compared to Mathematica function:

mmaFunc[i_] := 2*Range[i]

Table[Table[mmaFunc[100], {40000}]; Pause[0.1]; MemoryInUse[]/1024.^2, {10}]
(* {373.984, 373.984, 373.984, 373.984, 373.984, 373.984, 373.984, 373.984, 373.984, 373.984} *)

Why and how to fix it?

A different facet of the same bug is described here.

share|improve this question
By "returns the memory", do you mean "frees the memory" -- that is, returns it to the system to be reused? As opposed to the function returns no value? – Michael E2 Aug 1 '13 at 3:54
@MichaelE2 yes I mean frees the memory to the system. – xslittlegrass Aug 1 '13 at 5:43
added bugs per ruebenko. – rcollyer Aug 2 '13 at 14:16
up vote 11 down vote accepted

This is not an issue with LibraryLink as such. It seems to be an issue with how the compiled function interpreter calls a library link function. It's a bug and submitted as such. If you replace your code with

Table[Table[libFunc[100];, {40000}]; Pause[0.1]; 
 MemoryInUse[]/1024.^2, {10}]

there is no memory leak. Note the ; after the call to libFunc[100].

share|improve this answer
Could you explain more about your code and why it gives a different memory usage? And also why I don't need the ; in the mathematica version? Thanks. – xslittlegrass Aug 1 '13 at 14:55
@xslittlegrass, I do not know why the ; is necessary but I'll try to find out. Should I find something I'll report it here. – user21 Aug 2 '13 at 6:55
@xslittlegrass, please note the update. – user21 Aug 2 '13 at 15:50

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.