473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Unmanaged memory in a managed program (wrappers for small objects)

Hi all,

I am writing a component that exposes a C++ library as a .NET
component. The approach is somewhat automatic: every library C++ class
has its managed C++ counterpart that keeps a pointer to the unmanaged
class instance.

As usually in such a scenario, the IDisposable/Dispose()/Dispose(bool)
pattern should be used. The problem is that in my approach it is often
impractical, as the library's unmanaged objects are quite small and
many of them are created in various places. For example, such a
statement:

O1 o = o2.Prop1.Prop2;

looks quite nice and it is a lot more convenient than:

using (O3 o3 = o2.Prop1) {
using (O1 o = o3.Prop) {

}
}

The problems stems from the fact that lots of small objects are
created. I have found the following 2 approaches to deal with
unmanaged memeory releasing:

1. Do not do anything (wait for GC to collect managed objects and run
finalizers on them, which call delete on internal pointers). Well, I
have performed some stress tests and it seems to work fine - the
memory as displayed by the Windows Task Manager periodically grows and
decreases to the (approximately) the same value.

2. The problem may appear when the unmanaged heap goes out of space
first. So, to address such a situation, I believe that instead of
calling Dipose() on every small object (wrapper), one could do:

using (UnmanagedMemoryManager mm = MyLib.GetMemeoryManager()) {
// do whatever you want, all new objects (wrappers) created
// by the library in this thread will be automatically added
// to the manager and released in Dispose()

O1 o = o2.Prop1.Prop2;
// we want 'o' to stay usable after the using() block finishes;
// tell the manager to not bother about 'o'
mm.DoNotManage(o);

} // call mm.Dispose() and release all objects managed by the
manager,
// which does not include 'o'

return o;

As one can see, the library could have a thread-relative memory
manager and all library objects that require releasing the memory
could be released in "one shot".

What do you think about it?

Best regards,

Marek
Jul 21 '05 #1
2 1915
Marek,

I wouldn't like your second approach if I was a consumer of your
library. One of the prime reasons to use .NET is to *not* have to
worry about memory allocation issues.

I would instead try to do something similar to the handle collector
pattern used internally be the System.Drawing and
System.Windows.Forms. They keep track of how many unmanaged window
handles (or drawing resources) have been created, and after it reaches
a certain threshold they perform a garbage collection. Genghis
(http://www.sellsbrothers.com/tools/genghis/) also has an
implementation of this you can look at.

Either that, or go with option 1 and simply do nothing.

In the future (specifically in the Whidbey release) you'll be able to
tell the garbage collector about the size of unmanaged resorces you
have allocated with a method GC.AddMemoryPressure, so that it's taken
into account.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2
Thanks for your input.

Well, I am a bit curious why you think that the "memory manager" is
inconvenient. In fact, you can use it or not, and then you rely on the
default behavior (GC-based collection of both managed and unmanaged
memory). Moreover, if unmanaged new fails, then a dedicated for this
purpose UnmanagedOutOfMemoryException is thrown by the library's code
and a user can manually trigger garbage collection. If this is not
desired, then the manager could be used.

The difference from Gengis is probably that here the allocated objects
are small and, of course, we could still trace their number, but would
it be an interesting info for a library's user?

Marek

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
0
by: Frank Lopez | last post by:
Does anyone know if Microsoft generated a whitepaper on this topic? Does anyone know what the solution is? (meaning, eliminate the leak problem -- I am seeing three memory leaks from...
2
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking afterwards with ILDASM at what is visible in those assemblies from a...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
15
by: Chetan Raj | last post by:
Hi All, We have a web-application in asp.net that interacts with legacy code written in COM. The memory usage in aspnet_wp.exe increases every sec and never reduces. Using the .NET performance...
2
by: Marek Malowidzki | last post by:
Hi all, I am writing a component that exposes a C++ library as a .NET component. The approach is somewhat automatic: every library C++ class has its managed C++ counterpart that keeps a pointer...
20
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
Sorry to bring up a topic that is just flogging a dead horse.... but... On the topic of memory management.... I am doing some file parcing that has to be done as quick as posible, but what I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.