Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

Memory Leak

vincent_ferri
Beginner
407 Views

Hi,

I have an issue with dgemm() and other mkl functions not returning the memory that is used internal to the function.How can I release the memory used up. If this example is ran long enough the application will crash.

Environment:

/* MicroSoft Visual C++ 2010        Microsoft Windows 7     Intel Core i7-2600 CPU 3.40GHz      */
/*   Intel(R) C++ Composer XE 2011 Update 9, with Intel(R) C++ Compiler XE 12.1                   */
/*   and Version MKL 10.3 Update 10             

 Sample Code;

int main(int argc, char* argv[])
{

 double *a, *b, *c;
  int n, i;
  double alpha, beta;
  MKL_INT64 AllocatedBytes;
  int N_AllocatedBuffers;
 
  alpha = 1.1; beta = -1.2;
  n = 1000;

 for(int y=0; y<15; y++)
  {
     a = (double*)mkl_malloc(n*n*sizeof(double),64);
     b = (double*)mkl_malloc(n*n*sizeof(double),64);
     c = (double*)mkl_malloc(n*n*sizeof(double),64);

      for (i=0;i<(n*n);i++)
      {
         a = (double)(i+1);
         b = (double)(-i-1);
      }
   
      dgemm("N","N",&n,&n,&n,&alpha,a,&n,b,&n,&beta,c,&n);
       mkl_free_buffers();

      AllocatedBytes = mkl_mem_stat(&N_AllocatedBuffers);
      printf("\nDGEMM uses "FORMAT" bytes in %d buffers",AllocatedBytes,N_AllocatedBuffers);
 
      mkl_free(a);
      mkl_free(b);
      mkl_free(c);
      mkl_free_buffers();

      AllocatedBytes = mkl_mem_stat(&N_AllocatedBuffers);
      if (AllocatedBytes > 0) {
          printf("\nMKL memory leak!");
          printf("\nAfter mkl_free_buffers there are "FORMAT" bytes in %d buffers",
             AllocatedBytes,N_AllocatedBuffers);
      }

}

  mkl_free_buffers();
  mkl_thread_free_buffers();

       AllocatedBytes = mkl_mem_stat(&N_AllocatedBuffers);
      if (AllocatedBytes > 0) {
          printf("\nMKL memory leak!");
          printf("\nAfter mkl_free_buffers there are "FORMAT" bytes in %d buffers",
             AllocatedBytes,N_AllocatedBuffers);
      }
}

CodeOutput:

Version MKL 10.3 Update 10
DGEMM uses 24001112 bytes in 8 buffers
MKL memory leak!
After mkl_free_buffers there are 896 bytes in 5 buffe
DGEMM uses 29278936 bytes in 10 buffers
MKL memory leak!
After mkl_free_buffers there are 5278720 bytes in 7 b
DGEMM uses 29278936 bytes in 11 buffers
MKL memory leak!
After mkl_free_buffers there are 5278720 bytes in 8 b
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
DGEMM uses 31344728 bytes in 13 buffers
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10
MKL memory leak!
After mkl_free_buffers there are 7344512 bytes in 10

Thanks,

Vince

                     

0 Kudos
2 Replies
Gennady_F_Intel
Moderator
407 Views

yes, this is expected. You can find the description of MKL Memory Manager from User's Guide:

Intel MKL Memory Management Software
Intel MKL has memory management software that controls memory buffers for the use by the library
functions. New buffers that the library allocates when your application calls Intel MKL are not deallocated
until the program ends. To get the amount of memory allocated by the memory management software, call
the mkl_mem_stat() function. If your program needs to free memory, call mkl_free_buffers(). If another
call is made to a library function that needs a memory buffer, the memory manager again allocates the
buffers and they again remain allocated until either the program ends or the program deallocates the
memory. This behavior facilitates better performance. However, some tools may report this behavior as a
memory leak.

0 Kudos
SergeyKostrov
Valued Contributor II
407 Views
By definitrion, a Memory Leaks occurs when some memory is completely "lost", Not re-used and another block of memory is allocated instead. If some application or library re-uses the previously allocated memory and releases it when exits then it can Not be considered as a Memory Leaks.
0 Kudos
Reply