473,407 Members | 2,320 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,407 software developers and data experts.

GC and pinned pointers

Another thread mentioned non-Pinned pointers as a possible reason for an
Interop bug. I'd like to find out more about how this comes about. I
presume that the Garbage Collector makes a sweep, decides to realloc
memory that is not pinned, and that the unmanaged code's pointer becomes
invalid.

How often does the GC run, given a reasonably large app running on
a P4 300ghz with over 1 gig ram? I would have thought that it did not
need to run often at all.

Is there any other possible cause for the unpinned pointer problems?
Nov 16 '05 #1
7 3857
_BNC wrote:
Another thread mentioned non-Pinned pointers as a possible reason for an
Interop bug. I'd like to find out more about how this comes about. I
presume that the Garbage Collector makes a sweep, decides to realloc
memory that is not pinned, and that the unmanaged code's pointer becomes
invalid.
IIRC unmanaged code can only reference pinned objects, the GC is not
allowed to move pinned objects, so the unmanaged pointer does not become
invalid.

How often does the GC run, given a reasonably large app running on
a P4 300ghz with over 1 gig ram? I would have thought that it did not
need to run often at all.


A GC is performed each time your application wants to allocate an object
and generation 0's threshold is reached (something like 256K i think).
So, how often a GC is performed depends on how your application is behaving.

Regards,
Joakim
Nov 16 '05 #2

The number of GC run's depend highly on your usage pattern NOT the systems
CPU performance, if you allocate very frequently, the GC will run very
frequently .
If you don't allocate at all, the GC will not run at all(unless the system
runs low on memory).
You should not care about the number of GC runs, you have to pin the
pointers you pass to unmanaged code that's all.

Willy.

"_BNC" <_B**@nospam.com> wrote in message
news:p4********************************@4ax.com...
Another thread mentioned non-Pinned pointers as a possible reason for an
Interop bug. I'd like to find out more about how this comes about. I
presume that the Garbage Collector makes a sweep, decides to realloc
memory that is not pinned, and that the unmanaged code's pointer becomes
invalid.

How often does the GC run, given a reasonably large app running on
a P4 300ghz with over 1 gig ram? I would have thought that it did not
need to run often at all.

Is there any other possible cause for the unpinned pointer problems?

Nov 16 '05 #3
On Wed, 12 Jan 2005 23:05:39 +0100, "Willy Denoyette [MVP]"
<wi*************@pandora.be> wrote:

The number of GC run's depend highly on your usage pattern NOT the systems
CPU performance, if you allocate very frequently, the GC will run very
frequently .
If you don't allocate at all, the GC will not run at all(unless the system
runs low on memory).
You should not care about the number of GC runs, you have to pin the
pointers you pass to unmanaged code that's all.

Willy.


Thanks for your reply, Willy. I'm not being stubborn about pinning
pointers...I'm trying to figure out if I missed one somewhere. Or maybe
it's another problem entirely, I don't know. I'm not doing that many
memory allocs. I initially didn't figure that the GC would be the
culprit, but since problems turn up only when the program is stressed, it
would seem a likeley suspect. The smaller test models that I've worked up
run fine, of course.

I'm still trying to get a handle on the subtleties of the
managed-to-unmanaged bridge. Much of this seems counterintuitive to me.
For instance, the MSDN code that uses a similar method:

http://msdn.microsoft.com/library/de...nagedtypes.asp

does not __pin the pointer to the unmanaged struct (see constructor:
"city = new CITY;"). I'm trying to figure out why that pointer does not
need to be pinned.

I started my original model from code in a wrox book
"Visual C++.NET: a primer for C++ Developers" (Aravind Corera, etc,
ISBN 1861005962). which outlines the approach of using 'concentric'
classes layered around internal unmanaged code (Center = DLL, then
unmanaged C++ wrapper, then managed C++ wrapper, then C#). They
do not pin the pointer to the unmanaged C++ wrapper either.

I would have gone with PInvoke from the start, but the central DLL is such
a mess, with tons of obscure structs and defined data types, that it
seemed more logical to encapsulate the ugly stuff on the unmanaged side of
the fence.

I'm afraid that by the time I get a good handle on how the interim
layering works, Whidbey will have made it obsolete and my project will be
over. <g> (IOW, the real problem is a stupid typo somewhere)

_B
Nov 16 '05 #4
_BNC wrote:
I'm still trying to get a handle on the subtleties of the
managed-to-unmanaged bridge. Much of this seems counterintuitive to me.
For instance, the MSDN code that uses a similar method:

http://msdn.microsoft.com/library/de...nagedtypes.asp

does not __pin the pointer to the unmanaged struct (see constructor:
"city = new CITY;"). I'm trying to figure out why that pointer does not
need to be pinned.


I can try to help explain this part. The garbage collector ignores
unmanaged objects altogether; it will not move them, it will not free
them, and they cannot be pinned. If you define a managed (__gc) C++
class that contains pointers to unmanaged objects, the garbage collector
simply ignores these pointers. It is the job of that class's destructor
to deallocate the objects properly, and GC invokes this destructor at
the appropriate time. More generally, *managed code* is able to deal
with and contain *unmanaged objects* without much of a problem.

It is going in the other direction that pinning comes into play - if you
use *unmanaged code* to deal with pointers to *managed objects*. In this
case, GC does know about the object, and what it doesn't know about are
the unmanaged pointers to it. This prevents it from updating these
pointers after moving the object. Also, if only unmanaged pointers to an
object exist (which it doesn't know about) it will assume the object can
be freed. Pinning prevents this as well. Unfortunately, excessive
pinning also inhibits the efficiency of the collector, because it's
harder to find space to allocate in a highly fragmented memory pool.
--
Derrick Coetzee, Microsoft Speech Server developer
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included code samples are subject to the terms
specified at http://www.microsoft.com/info/cpyright.htm
Nov 16 '05 #5
On Thu, 13 Jan 2005 19:11:35 -0500, "Derrick Coetzee [MSFT]"
<dc******@online.microsoft.com> wrote:
_BNC wrote:
I'm still trying to get a handle on the subtleties of the
managed-to-unmanaged bridge. Much of this seems counterintuitive to me.
For instance, the MSDN code that uses a similar method:

http://msdn.microsoft.com/library/de...nagedtypes.asp

does not __pin the pointer to the unmanaged struct (see constructor:
"city = new CITY;"). I'm trying to figure out why that pointer does not
need to be pinned.
I can try to help explain this part. The garbage collector ignores
unmanaged objects altogether; it will not move them, it will not free
them, and they cannot be pinned. If you define a managed (__gc) C++
class that contains pointers to unmanaged objects, the garbage collector
simply ignores these pointers. It is the job of that class's destructor
to deallocate the objects properly, and GC invokes this destructor at
the appropriate time. More generally, *managed code* is able to deal
with and contain *unmanaged objects* without much of a problem.


That makes sense. Still, it seems like the example code effectively
allocs the inner unmanaged struct and maintains that pointer to that
struct without having to be pinned. I could swear that I've seen
equivalent pointers pinned in some example code, but I guess this will
make sense after it sinks in.

Now I've got to look for another cause for my problems though, cause I'm
allocing a block of ram in an unmanaged module, and the only thing that
the managed code does is pass the pointer to it to another unmanaged
class. No allocs are done by the managed code, and my understanding
is that the pointer itself is safe, being a low-level item.
It is going in the other direction that pinning comes into play - if you
use *unmanaged code* to deal with pointers to *managed objects*. In this
case, GC does know about the object, and what it doesn't know about are
the unmanaged pointers to it. This prevents it from updating these
pointers after moving the object. Also, if only unmanaged pointers to an
object exist (which it doesn't know about) it will assume the object can
be freed. Pinning prevents this as well.
A lucid explanation, Derrick.
Unfortunately, excessive
pinning also inhibits the efficiency of the collector, because it's
harder to find space to allocate in a highly fragmented memory pool.


I've heard that pinned pointers can effectively slow down the managed
code by a factor of 25 (!) so I've been trying to avoid them.

_B
Nov 16 '05 #6
> Still, it seems like the example code effectively
allocs the inner unmanaged struct and maintains that pointer to that
struct without having to be pinned.
That's true; in fact, there's no point in pinning a pointer to an unmanaged
object, because the GC doesn't know about these objects and won't move them,
and ignores the pointer. Such blocks of memory are outside its control, just
like a block allocated in an unmanaged program. They are, effectively,
already pinned.
I could swear that I've seen equivalent pointers pinned in some example code [ . . . ]

This may have been a pointer to a managed object, or it might have been a
mistake. It might let you create pinned pointers to unmanaged objects,
although this is pointless, since they're already "pinned."
Now I've got to look for another cause for my problems though, cause I'm
allocing a block of ram in an unmanaged module, and the only thing that
the managed code does is pass the pointer to it to another unmanaged
class. No allocs are done by the managed code, and my understanding
is that the pointer itself is safe, being a low-level item.
The Framework shouldn't interfere with unmanaged-to-unmanaged interactions,
as a rule. It sounds like your application has some very subtle problems.
I'm not sure where to tell you to start, but if you can find an alternative
to interfacing managed and unmanaged code using a mixed-code DLL, this might
be the simplest way to go. For example, I once did something similar to this
where in the end I ended up driving the C module through a command-line
interface and input/output files. You may also try using PInvoke to call
directly from the managed to the unmanaged code. In C# this is done with the
DllImport attribute; see an example here:

http://msdn.microsoft.com/library/de...okevisualc.asp
I've heard that pinned pointers can effectively slow down the managed
code by a factor of 25 (!) so I've been trying to avoid them.


The main problem is that it slows down garbage collection cycles and
allocations a great deal if you use many pinned pointers at once, and the
larger the objects you pin the worse the effect. Pinning some small things
here and there is relatively harmless.
--
Derrick Coetzee, Microsoft Speech Server developer
This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 16 '05 #7
On Fri, 14 Jan 2005 10:59:19 -0800, "Derrick Coetzee [MSFT]"
<dc******@online.microsoft.com> wrote:
... there's no point in pinning a pointer to an unmanaged
object, because the GC doesn't know about these objects and won't move them,
and ignores the pointer. Such blocks of memory are outside its control, just
like a block allocated in an unmanaged program. They are, effectively,
already pinned.
That finally makes sense. I've assumed that if the alloc was done within
managed code, that it would be subject to garbage collection. It's one of
those subtleties that was not clear in the docs that I've read.
Now I've got to look for another cause for my problems though, cause I'm
allocing a block of ram in an unmanaged module, and the only thing that
the managed code does is pass the pointer to it to another unmanaged
class. No allocs are done by the managed code, and my understanding
is that the pointer itself is safe, being a low-level item.


The Framework shouldn't interfere with unmanaged-to-unmanaged interactions,
as a rule. It sounds like your application has some very subtle problems.
I'm not sure where to tell you to start, but if you can find an alternative
to interfacing managed and unmanaged code using a mixed-code DLL, this might
be the simplest way to go.


I've heard of subtle bugs in .NET mixed mode in general. It's frustrating
in that so much work went into the mixed mode bridge, and it's difficult
to trace problems. Smaller, or unstressed models of the code work fine.
I thought maybe it was a task prioritization thing, as the access to the
mixed mode code occurs in a thread. Strangely enough, increasing the
thread priority makes the code *less* reliable. I'm not getting runtime
errors or exceptions--just wrong results. Very counterintuitive.
For example, I once did something similar to this
where in the end I ended up driving the C module through a command-line
interface and input/output files. You may also try using PInvoke to call
directly from the managed to the unmanaged code. In C# this is done with the
DllImport attribute; see an example here:

http://msdn.microsoft.com/library/de...okevisualc.asp
Thanks for the link, Derrick. I first considered PInvoke, but thought
that mixed mode would be better for this app (see below). However, if the
..NET mixed-mode DLL has inherent problems, as in:

http://msdn.microsoft.com/library/de...ingProblem.asp

...then I suppose I'll have to give up on it. Unfortunately, the inner C
DLL is very messy, with lots of typedefs and structs that would be tough
to marshal via PInvoke. The fallback plan may be to encapsulate the C DLL
in an unmanaged C++.NET class to simplify the interface, and use PInvoke
to access that.

I've essentially done the encapsulation part already in the course of
writing the mixed-mode DLL, so I suppose I could start from there and
just PInvoke the encapsulating class.

I've also written a completely unmanaged version of the code which works
fine under run-time stress. In other words, I've got lots of clues, all
pointing in different directions. <g>
Derrick Coetzee, Microsoft Speech Server developer


Thanks for your perspective, Derrick. My understanding of the
managed-to-unmanaged transition is improving.

Re MS Speech server: I know you're probably more involved with the
ASP-related side of the MS Speech SDK, but I'm curious about MS's 'local'
speech SDK. I haven't had much luck with the 5.1 SDK under .NET. There
was a v5.2 mentioned somewhere that had more .NET support, but it seems to
have disappeared. Do you happen to know if anything new is imminent?

_B

Nov 16 '05 #8

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

Similar topics

1
by: goyal | last post by:
Hi, Is there a way to find out which tables/indexes/packages/functions have been pinned in the database? Thanks in advance. Cheers, Sachin
0
by: kiplring | last post by:
I'm using managed directx. I made a KeyFrameInterpolator object. And get a exception above. I think it is because GC reconize KeyFrameInterpolator object as Pinned object. So I added next line....
3
by: Ben Schwehn | last post by:
If I pin a member of a garbage collected structure, can I safely assume that the entire struct is pinned? I use something like that: (new whidbey syntax, but that shouldn't make a difference): ...
11
by: _BNC | last post by:
Another thread mentioned non-Pinned pointers as a possible reason for an Interop bug. I'd like to find out more about how this comes about. I presume that the Garbage Collector makes a sweep,...
1
by: Lance Orner | last post by:
I wrote this letter to a colleague, and I thought I'd share it here: --- We had a problem with pointers being passed between managed C++ and unmanaged C++ code, but it only occurred once in...
1
by: Pierre | last post by:
Hi everyone; I have a class with several members and I pin the first reference field in a function; public ref struct MyClass { String str1; List<MySubClass ^^subObjects; ....
1
by: Crash | last post by:
..Net - All versions I have Essential .Net Vol 1 (Don Box) and the Jeffrey Richter articles on the .NET heap - they are both excellent reads. But I am still unclear on the big pinned memory...
16
by: Atmapuri | last post by:
Hi! It would be great, if the pinned arrays would be garbage collectable. This would greatly reduce the amount of copy back and forth the managed and unmanaged memory. I cant think of a single...
2
by: ChaosMageX | last post by:
I copied some code from a Mono 2.4 library to use in my own application. I'm getting a problem because one of the variable declarations involves the keyword "pinned" between the identifier and the...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.