Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Problem: Native DLL loaded from .NET application can fail if it requests too much memory at startup
| Article ID | : | 951949 |
| Last Review | : | April 18, 2008 |
| Revision | : | 1.1 |
Source: Microsoft Support
Back to the top
RAPID PUBLISHING
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.
Back to the top
Action
Create a Windows-based .NET application that loads, using Platform Invoke, an unmanaged User DLL that has global variables (e.g. large arrays) that occupy ~1 GB or more of memory.
[When loaded from unmanaged Windows application, the DLL may load successfully. But this issue involves loading from a .NET application such as written in C# or VB.NET.]
Back to the top
Result
DLL fails to load, generating an error such as:
"LoadLibrary failed! MyUser.dll"
Back to the top
Cause
Memory occupied by the .NET (C#, VB.NET, etc.) module is more than when compared to the VC++ module, due to the following reasons:
1. Managed module loads loads other managed system DLLs, in addition to the native system DLLs
e.g.,
mscorlib, system.windows,forms, System, mscorwks, system.drawing, mscorlib, system.data etc..
2. Available memory has to be divided for managed and unmanaged modules.
3. Even if enough memory remains overall, this DLL may not be loaded due to memory fragmentation, since a contiguous block would be required to allocate global variables.
Back to the top
Resolution
In the first place, it is generally a bad programming design to request around ~1 GB or more of memory at a time. Following are ways that this problem may be overcome, but they are just temporary solutions:
1. Consider using the
Low-fragmentation Heap (LFH) (http://msdn2.microsoft.com/en-us/library/aa366750.aspx)2. Consider using the
/3GB switch (http://kbalertz.com/Feedback.aspx?kbNumber=328882) to increase the user address space of the process.
3. Consider compiling the application targeting a x64 or IA64 machine, as a much greater amount of user memory will be available.
Back to the top
More Information
DISCLAIMER
MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.
Back to the top
APPLIES TO
| • | Microsoft .NET Framework 1.1 |
| • | Microsoft .NET Framework 2.0 |
| • | Microsoft .NET Framework 3.0 |
| • | Microsoft .NET Framework 3.5 |
| • | Microsoft Visual Studio .NET 2003 Academic Edition |
| • | Microsoft Visual Studio .NET 2003 Professional Edition |
| • | Microsoft Visual Studio .NET 2003 Enterprise Developer |
| • | Microsoft Visual Studio .NET 2003 Enterprise Architect |
| • | Microsoft Visual Studio 2005 Express Edition |
| • | Microsoft Visual Studio 2005 Standard Edition |
| • | Microsoft Visual Studio 2005 Professional Edition |
| • | Microsoft Visual Studio 2005 Team Suite |
| • | Microsoft Visual Studio 2008 Academic Edition |
| • | Microsoft Visual Studio 2008 Standard Edition |
| • | Microsoft Visual Studio 2008 Professional Edition |
| • | Microsoft Visual Studio Team System 2008 Team Suite |
Back to the top
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please