Microsoft Knowledge Base Email Alertz

Problem: Native DLL loaded from .NET application can fail if it requests too much memory at startup

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











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 - View products that this article applies to.
Source: Microsoft Support

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.

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.]

Result

DLL fails to load, generating an error such as:
    "LoadLibrary failed! MyUser.dll"

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.

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)

2.    Consider using the /3GB switch 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.

More Information

x64 Primer: Everything You Need To Know To Start Programming 64-Bit Windows Systems
Dynamic-Link Library Data (Windows)
"Out of Memory" error message appears when you have a large number of programs running

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.

Properties

Article ID: 951949 - Last Review: April 18, 2008 - Revision: 1.1
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
Keywords: 
kbnomt kbrapidpub KB951949
       

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

rednael - http://blog.rednael.com Report As Irrelevant  
Written: 8/29/2008 7:37 AM
Please take a look at the following blog post: http://blog.rednael.com/2008/08/29/MarshallingUsingNativeDLLsInNET.aspx It discusses in-depth about which types are interoperable, how to import a DLL, how to pass strings and how to de-reference pointers. C# source code samples included...