Trying to reproduce the problem, I wrote a minimal working example for your case: a MEX file that refers to a dynamic library and uses one of its public functions. I tested the following on 32-bit WinXP, using MATLAB R2010b with VS2010 as a compiler (for DLL and MEX).
The example simply adds floating point numbers. The MEX file accepts matrices / vectors and loops over the elements that call the add() function from the library on each pair.
Adder.h
#ifndef ADDER_H #define ADDER_H #ifdef __cplusplus extern "C" { #endif #ifdef _WIN32 # ifdef BUILDING_DLL # define DLL_IMPORT_EXPORT __declspec(dllexport) # else # define DLL_IMPORT_EXPORT __declspec(dllimport) # endif #else # define DLL_IMPORT_EXPORT #endif DLL_IMPORT_EXPORT double add(double x, double y); #ifdef __cplusplus } #endif #endif
Adder.c
#include "Adder.h" double add(double x, double y) { return x+y; }
mymexfunction.c
#include "mex.h" #include "Adder.h" #define X_IN input[0] #define Y_IN input[1] #define Z_OUT output[0] void mexFunction(int output_size, mxArray *output[], int input_size, const mxArray *input[]) { double *inX, *inY, *outZ; mwSize m,n; int i; if (input_size != 2) { mexErrMsgTxt("Two input arguments required."); } if (output_size > 1) { mexErrMsgTxt("Too many output arguments."); } m = mxGetM(X_IN); n = mxGetN(X_IN); if ( !mxIsDouble(X_IN) || !mxIsDouble(Y_IN) ) { mexErrMsgTxt("Input arguments must be matrices/vectors of doubles."); } if ( mxGetM(Y_IN)!=m || mxGetN(Y_IN)!=n ) { mexErrMsgTxt("X and Y must be of same size."); } Z_OUT = mxCreateDoubleMatrix(m, n, mxREAL);
First we create a dynamic library, as I mentioned that I use VC ++ for the job. On Unix based systems with GCC, I think this step will be similar (correct me if I am wrong):
gcc -c -DBUILDING_DLL Adder.c -o Adder.o -I. gcc -shared -o libAdder.so Adder.o -Wl,--out-implib,libAdder.a
then in MATLAB we will compile the MEX file:
>> mex mymexfunction.c -I. -L. -lAdder
(Note: I put everything in one folder to avoid path issues.)
Next, we can check the function in MATLAB:
>> mymexfunction([1 2;3 4], [5 6; 7 8]) ans = 6 8 10 12
Using the Process Explorer tool from Sysinternals, we can view loaded DLLs using the MATLAB process, the MEX function, and our custom dynamic library

If we select the clear mex command, then both modules will be unloaded as expected (which is checked using the Process Explorer). This is also confirmed by INMEM as @Praetorian :
clear mex [~,m] = inmem('-completenames'); any( ismember(m,fullfile(pwd,['mymexfunction.' mexext])) )
Finally, if we make some changes to mymexfunction.c :
recompile MEX and check it again (all in the same session, without restarting). The result will reflect the changes you see:
>> mymexfunction([1 2;3 4], [5 6; 7 8]) ans = 16 18 20 22
Try repeating the above on a Mac / Linux computer. If you still get the old amounts, then this should be a mistake specific to non-Windows platforms, and it should be reported to MathWorks ... Otherwise, I suspect that there should be some freed resources in your code that remain in memory ?