Deploying Excel add-in causes 32-bit Excel to crash or throw an out of memory error.

2 views (last 30 days)
I have compiled functions from 32-bit MATLAB to be used in 32-bit Excel as an Excel add-in. Everything works fine in MATLAB but when I run the add-in in Excel I receive out of memory errors or Excel just crashes without warning.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Jan 2017
In general Excel Add-ins can run in-process or out-of-process. When they run in-process the Add-ins need to share the virtual memory space with Excel itself and any other Add-ins which you may be running in Excel. When they run out-of-process they have a virtual memory space all to themselves. For a (non /LARGEADDRESSAWARE) 32-bit application (like most Excel versions still being used) the virtual memory space is limited to only 2 GB. Therefore running into memory issues when working with multiple in-process Add-ins in 32-bit Excel is quite common; especially when one of these Add-ins is a MATLAB Compiler/Builder EX component as these typically need a large amount of memory.
Prior to release R2015b, MATLAB Compiler/Builder EX Excel Add-ins could only run in-process; this meant that if you had 32-bit Excel you could only work with 32-bit MATLAB Compiler/Builder EX Excel Add-ins. As of release R2015b MATLAB Compiler Excel Add-ins can run out-of-process, this now makes it possible to use 64-bit MATLAB Compiler Excel Add-ins in 32-bit Excel.
Therefore the best solution, when running into out of memory issues when working with 32-bit Excel, is switching to 64-bit MATLAB Compiler release R2015b or above and creating a 64-bit Add-in.
If you are already working with 32-bit MATLAB Compiler release R2015b but switching to 64-bit is not an option:
Excel Add-ins created with 32-bit MATLAB Compiler R2015b will still run in-process by default when working with 32-bit Excel. This behavior can be overridden however by modifying the VBA code which was generated by MATLAB Compiler.
You will need to change the line which loads MWUtil (typically there is only one such line in the code):
Set MCLUtil = CreateObject("MWComUtil.MWUtil9.0")
Into:
Set MCLUtil = CreateObject("MWComUtil.MWUtil9.0", "localhost")
Similarly update any lines of code (there may be multiple of these) which load your Add-in:
Set Class1 = CreateObject("YourAddin.YourClass.1_0")
Into:
Set Class1 = CreateObject("YourAddin.YourClass.1_0", "localhost")
If you cannot upgrade to MATLAB Compiler release R2015b or above:
You may still be able to switch to 64-bit Excel such that you can then also upgrade to the 64-bit version of the MATLAB release which you want/need to work with.
 
If you cannot switch to any 64-bit MATLAB Compiler/Builder EX version
As mentioned before, 32-bit Excel is limited to 2GB of address space.  This limit can be removed and increased to 3 GB on 32-bit Windows systems and 4 GB on 64-bit Windows system. First you will need to set the large address aware option on the Excel executable. To do this:
a.  Make sure Excel is not running.
b.  Open a Visual Studio command prompt with administrator privileges.
c.  Run the following commands.
cd <location of EXCEL.exe>
editbin /LARGEADDRESSAWARE EXCEL.exe
<location of EXCEL.exe> typicall is something like "C:\Program Files (x86)\Microsoft Office\Office15" depending on your particular Excel version and installation.
If you're on 32-bit Windows, the user space limit must additionally be increased to 3 GB.  The instructions below explain how to increase the limit.
https://technet.microsoft.com/en-us/library/bb124810(v=exchg.65).aspx
(Note the page above talks about Windows server versions but the instructions for Windows Server 2003 also apply to Windows Vista/7/8/10 and the instructions for Windows 2000 also apply to Windows XP).
Note that if you are on 32-bit Windows, the above methods might not be enough to resolve memory issues.  It's possible Excel will continue to fail because even the increased 3GB of memory is not enough (which is shared between all user applications), or that Windows itself might start failing in unpredictable ways (e.g., network stops working) because the reduced 1GB of address space it has for itself is not enough.  In this case, the only feasible solution is to upgrade to 64-bit Windows.
 
If none of the above is possible or helps in resolving the issue
You can try disabling the JVM if your MATLAB code does not use any code that requires the JVM (e.g., graphics, database or hardware connectivity).  The JVM is designed to spawn a lot of threads as part of its operation, which in turn creates a lot of stacks and thus increase memory fragmentation, on top of using more memory.  To disable it, go into your Excel developer tab, open the Visual Basic editor and go to the VB code associated with the deployed add-in.  At the top you’ll see code like:
Call MCLUtil.MWInitApplication(Application)
Replace that with:
Dim mcrOptions(1 To 1) as String
mcrOptions(1) = "-nojvm"
Call MCLUtil.MWInitApplicationWithMCROptions(Application, mcrOptions)
Restart Excel and re-run the deployed add-in.

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products


Release

No release entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!