11
$\begingroup$

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;    
 Needs["CCompilerDriver`"];

 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.

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$ – Michael E2 Aug 1 '13 at 3:54
  • $\begingroup$ @MichaelE2 yes I mean frees the memory to the system. $\endgroup$ – xslittlegrass Aug 1 '13 at 5:43
  • $\begingroup$ added bugs per ruebenko. $\endgroup$ – rcollyer Aug 2 '13 at 14:16
11
$\begingroup$

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].

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ – xslittlegrass Aug 1 '13 at 14:55
  • $\begingroup$ @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. $\endgroup$ – user21 Aug 2 '13 at 6:55
  • $\begingroup$ @xslittlegrass, please note the update. $\endgroup$ – user21 Aug 2 '13 at 15:50

Your Answer

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

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