How to properly use memory dynamically allocated in C++ dll within Matlab -


i wrote c++ interface dll call key image processing statistical measuring methods intel's integrated performance primitives (ipp) library can exploit simd capabilities. able pass 2 2d matlab arrays on function using calllib

a = single( rand(3,4) ); b = single( rand(3,2) );  pout = calllib('ippinterface', 'myfunc', a' , b' , size(a,1), size(a,2) , size(b,1), size(b,2) ); 

where in c++ myfunc has following signature, , snippet of body...

float* myfunc( float a[] , float b[] , int nrowsa , int ncolsa , int nrowsb , int ncolsb ) {     ...     float[] pdst = new float[nrowsa*ncolsa];     ...     return pdst; } 

what i'm unsure of how , if matlab deletes such buffers memory. wind in matlab process lib.pointers:

>> class(pout)  ans =  lib.pointer 

i'm curious if i'm doing bad idea, or if calling matlab's clear pout take care of , avoid conceivable memory leak.

so i've been doing memory testing creating in matlab 2 arrays follows...

i = single( rand( 1200 , 1600 ) ); t = single( rand( 100  , 100  ) ); 

i put function in loop printing of memory usage text file , clearing returned libpointer.

for = 1:10000     lp = calllib('ippinterface', 'myfunc', , t , size(i,2) , size(i,1) , size(t,2) , size(t,1) );     clear lp;      [u,s] = memory;      fprintf( fileid ,'%13g    %13s\n' ,  u.memusedmatlab , s.systemmemory.available ); end  

going route fills physical memory can see below, , has convinced me doing bad idea , matlab not handle in automated garbage collection fashion you.

>> memory maximum possible array:              38799 mb (4.068e+010 bytes) * memory available arrays:     38799 mb (4.068e+010 bytes) * memory used matlab:               21393 mb (2.243e+010 bytes) physical memory (ram):               34814 mb (3.650e+010 bytes)  *  limited system memory (physical + swap file) available. >> clear >> memory maximum possible array:              38803 mb (4.069e+010 bytes) * memory available arrays:     38803 mb (4.069e+010 bytes) * memory used matlab:               21388 mb (2.243e+010 bytes) physical memory (ram):               34814 mb (3.650e+010 bytes)  *  limited system memory (physical + swap file) available. >>  

matlab memory profile log data

task manager showing memory leak

if close matlab os naturally reclaims memory.

task manager showing reclaimed memory matlab closed

after digging around matlab's website did find close enough example whereby example had dynamically allocated c-struct in shared library, , later on matlab had call function written in same library sole task delete struct.

working pointer arguments; multilevel pointers

this example right on, after implementing simple interface call shared lib deallocate array , using after done

void deallocatearray( float* ) {     delete[] a; } 

the memory profile flat, , entire 10000 iterations complete more rapidly...

for = 1:10000     lp = calllib('ippinterface', 'myfunc', , t , size(i,2) , size(i,1) , size(t,2) , size(t,1) );     calllib('ippinterface', 'deallocatearray', lp );     clear lp;      [u,s] = memory;      fprintf( fileid ,'%13g    %13s\n' ,  u.memusedmatlab , s.systemmemory.available ); end  

enter image description here


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -