From: Bruno Luong on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hrrk5c$rkh$1(a)fred.mathworks.com>...
>
>
> James, I run this code on 2010A/Vista64, and it obviously runs until the end, showing that mxRealloc is functioning.

I carry out more tests:

Same command fails on 2010A/Vista32, and works with 2006B/Vista32.

So the problem seems to be real, and affects 32-bit OS.

Bruno
From: Bruno Luong on
I have run the code and track down the pointer address as well as the memory. Here is the code

#include "mex.h"

#define MAXTEST 100

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

void *ptr[MAXTEST];
int n;

/* Allocate/Reallocate MAXTEST times */
for (n=0; n<MAXTEST; n++) {
ptr[n] = mxMalloc(1073741824); /* <- 1 Gbytes */
if (!ptr[n]) break;
mexPrintf("Allocate %d times, ptr[%d] = %p\n", n+1, n, ptr[n]);
/* If the following line is removed, 'Out of memory' error will be issued assuming RAM is less than 100 Gbytes*/
ptr[n] = mxRealloc(ptr[n], 1);
mexPrintf(" reallocate at ptr[%d] = %p\n", n, ptr[n]);
mexEvalString("[uV] = memory;");
mexEvalString("fprintf('Matlab uses %d Mbytes\\n', round(uV.MemUsedMATLAB/1024^2));");
}
for (; n--;) {
if (ptr[n]) mxFree(ptr[n]);
}

/* Allocate MAXTEST times */
for (n=0; n<MAXTEST; n++) {
ptr[n] = mxMalloc(1073741824); /* <- 1 Gbytes */
if (!ptr[n]) break;
mexPrintf("Allocate %d times, ptr[%d] = %p\n", n+1, n, ptr[n]);
/* If the following line is removed, 'Out of memory' error will be issued assuming RAM is less than 100 Gbytes*/
mexEvalString("[uV] = memory;");
mexEvalString("fprintf('Matlab uses %d Mbytes\\n', round(uV.MemUsedMATLAB/1024^2));");
}
for (; n--;) {
if (ptr[n]) mxFree(ptr[n]);
}
return;
}

When running on 2010A 64bit,
The output of the first part with reallocation is:

Allocate 1 times, ptr[0] = 0000000080000040
reallocate at ptr[0] = 0000000080000040
Matlab uses 507 Mbytes
....
Allocate 100 times, ptr[99] = 00000019C0620040
reallocate at ptr[99] = 00000019C0620040
Matlab uses 508 Mbytes

Note stability of that the memory used Matllab, even the pointer value does not change by reallocation.

----------------------------------------------------------------------------
The output of the first part WITHOUT reallocation is:

Allocate 1 times, ptr[0] = 0000000080000040
Matlab uses 1532 Mbytes
Allocate 2 times, ptr[1] = 00000000C0010040
Matlab uses 2556 Mbytes
Allocate 3 times, ptr[2] = 0000000180010040
Matlab uses 3580 Mbytes
Allocate 4 times, ptr[3] = 00000001C0020040
Matlab uses 4604 Mbytes
Allocate 5 times, ptr[4] = 0000000200030040
Matlab uses 5628 Mbytes
Allocate 6 times, ptr[5] = 0000000240040040
Matlab uses 6652 Mbytes
Allocate 7 times, ptr[6] = 0000000280050040
Matlab uses 7676 Mbytes
Allocate 8 times, ptr[7] = 00000002C0060040
Matlab uses 8700 Mbytes
Allocate 9 times, ptr[8] = 0000000300070040
Matlab uses 9724 Mbytes
Allocate 10 times, ptr[9] = 0000000340080040
Matlab uses 10748 Mbytes
Allocate 11 times, ptr[10] = 0000000380090040
Matlab uses 11772 Mbytes
Allocate 12 times, ptr[11] = 00000003C00A0040
Matlab uses 12796 Mbytes
Allocate 13 times, ptr[12] = 00000004000B0040
Matlab uses 13820 Mbytes
Allocate 14 times, ptr[13] = 00000004400C0040
Matlab uses 14844 Mbytes
Allocate 15 times, ptr[14] = 00000004800D0040
Matlab uses 15868 Mbytes
??? Error using ==> testrealloc
Out of memory. Type HELP MEMORY for your options.

The memory used Matllab increases until it crashes.

Bruno
From: Bruno Luong on
I makes similar test on 2010A/32 bit Vista and the mxRealloc() doesn't free the memory. My conclusion: it is buggy on 32-bit OS, but works fine on 64 bit OS.

Bruno
From: James Tursa on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hrs9jk$iqe$1(a)fred.mathworks.com>...
> I makes similar test on 2010A/32 bit Vista and the mxRealloc() doesn't free the memory. My conclusion: it is buggy on 32-bit OS, but works fine on 64 bit OS.

I don't have a 64-bit system to test with, but I also consider the 32-bit behavior a bug, although I will admit I don't know exactly what the C standard says is required behavior for realloc calls with regards to returning memory to the heap. Maybe a post to the C newsgroup is in order. In any event, I went ahead and submitted a bug report to TMW.

James Tursa
From: Roland Kruse on
The problem seems to be even stranger.
I use R2010 and Win 7 x64.
If I run the first example

A = sprand(10000,10000,0.1);
C = cell(1,100);
for k=1:100; C{k} = 0*A; end
clear

I loose no memory, even when I use mtimesx.
On the other hand, if I make A more sparse, e.g.

A = sprand(10000,100000,0.001);

then mtimesx will make me loose approx. 70 MB whereas Matlab's mtimes works flawlessly.

So the problem is not restricted to x32 but less severe there.