4

What is the default stack size of MATLAB R2018a (64-bit)?

It seems that the stack-size is larger than an 64-bit C# program.

Why I'm asking that

I'm asking this question because I'm calling Intel MKLs LAPACKE_dtrtri which is heavily recursive.

I'm using that function inside .NET application and I'm getting a stack overflow error when calling it from C#, see What is the stack size of a BackgroundWorker DoWork Thread? Is there way to change it?

On the other side if I call my .NET application from MATLAB I'm not getting a stack overflow error. That's the reason I wanted to know what the stack size of MATLAB.

Wollmich
  • 1,616
  • 1
  • 18
  • 46
  • 3
    It's 42. Jokes aside: Can you please further elaborate on your question? At least to me, it's not clear, what you exactly want to know. – HansHirse Jun 05 '19 at 09:45
  • Is it possible that this question get's reopened so that I can answer my own question. By the way the stack size of MATLAB seems to be 64 MB and I would like to explain in my answer. – Wollmich Jun 05 '19 at 12:16
  • 2
    I asked the following question: [What is the stack limit when MATLAB calls function in DLL](https://stackoverflow.com/questions/42101043/what-is-the-stack-limit-when-matlab-calls-function-in-dll). I thought stack size is 64MByte – Rotem Jun 05 '19 at 16:21

3 Answers3

4

Using the GetCurrentThreadStackLimits function from kernel32.dll I can get the stack size of MATLAB.

I've created the following helper method in an .NET assembly called IntelMKL.dll:

static class _kernel
{
    [DllImport("kernel32.dll")]
    internal static extern void GetCurrentThreadStackLimits(out uint lowLimit, out uint highLimit);
}

and

public static class MKL
{
    public static uint GetStackSize()
    {
        uint low, high;
        _kernel.GetCurrentThreadStackLimits(out low, out high);
        return high - low;
    }
}

I can call the GetStackSize from MATLAB using the following code:

NET.addAssembly('IntelMKL.dll')
IntelMKL.MKL.GetStackSize() % this return 67108864 Bytes which is 64 Mega Bytes

The stack size of MATLAB is 64 MB.

It seems that the stack-size is larger than an 64-bit C# program.

The default C# stack size is 1 MB (32-bit) and 4 MB (64-bit), see What is the stack size of a BackgroundWorker DoWork Thread? Is there way to change it?

Wollmich
  • 1,616
  • 1
  • 18
  • 46
1

While I do not have a direct answer to your question, MATLAB's recursion limit can be obtained by get(0,'RecursionLimit') or set by set(0,'RecursionLimit',N).

I believe than other than that, MATLAB will keep storing variables until your RAM runs out, but not 100% sure.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • I guess this `RecursionLimit` is only used for MATLAB functions (m-files) but I've I call a .NET method from MATLAB the stack size is relevant. – Wollmich Jun 05 '19 at 10:47
  • @Wollmich I suspect the *question* is harder than "what is the stack size of MATLAB". Possibly "What is the stack size that MATLAB gives .NET applications when called from within" – Ander Biguri Jun 05 '19 at 12:28
  • If MATLAB is not creating a new thread for .NET it's the same stack size as MATLAB has. Or not? – Wollmich Jun 05 '19 at 12:31
  • @Wollmich are you doing it through the mex interface? I *think* that matlab creates a locking new thread when it calls it, but I am not sure. – Ander Biguri Jun 05 '19 at 12:36
  • I'm doing it over the .NET interface, see https://ch.mathworks.com/help/matlab/getting-started.html – Wollmich Jun 05 '19 at 12:38
1

Using the dumpbin command I can take look at the header of the MATLAB.exe.

dumpbin /headers "C:\Program Files\MATLAB\R2018a\bin\win64\MATLAB.exe"

This returns

Dump of file C:\Program Files\MATLAB\R2018a\bin\win64\MATLAB.exe

PE signature found

File Type: EXECUTABLE IMAGE

FILE HEADER VALUES
            8664 machine (x64)
...

OPTIONAL HEADER VALUES
...
         4000000 size of stack reserve
            1000 size of stack commit
          100000 size of heap reserve
            1000 size of heap commit

The size of stack reserve is the stack size in hex.

So the stack size of MATLAB is 67108864 Bytes which is 64 Mega Bytes.

Wollmich
  • 1,616
  • 1
  • 18
  • 46