垃圾收集跨C#和C ++ / CLI对象 [英] Garbage Collection Across C# and C++/CLI Objects

查看:112
本文介绍了垃圾收集跨C#和C ++ / CLI对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究使用C ++ / CLI弥合托管的C#和本地,非托管C ++代码之间的差距。我期待解决的一个具体问题是,在C#和C ++不同数据类型的转换。

I'm currently looking into using C++/CLI to bridge the gap between managed C# and native, unmanaged C++ code. One particular issue I'm looking to resolve, is the conversion of data types that are different in C# and C++.

在阅读起来就采用了这样的桥接方式和所涉及的性能问题,我想知道垃圾收集将如何工作。具体来说,垃圾收集器将如何处理两边创建的,如果他们被引用/摧毁了彼岸对象的清理工作。

While reading up on the use of such a bridging approach and the performance implications involved, I wondered how Garbage Collection would work. Specifically, how the Garbage Collector would handle cleanup of objects created on either side, if they are referenced / destroyed on the 'other side'.

到目前为止,我已经阅读计算器并的 MSDN ,这使得我相信,垃圾收集器应该在这两个类型的代码工作时,在同一进程中运行。 - 如果一个对象是在C#创建并传递给C / CLI桥即它将不被收集,直到双方的引用中使用不再

So far, I've read various articles and forum questions on StackOverflow and MSDN, which has lead me to believe that the Garbage Collector should work across both types of code when running in the same process - i.e if an object was created in C# and passed to the C++/CLI bridge, it would not be collected until the references on both sides were no longer in use.

我在这种情况下的问题,分解成三个部分:

My question in this case, breaks down into three parts:


  1. 我是正确的结论是垃圾收集器可以跨?它是如何在这样的情况下(具体地说工作在清理对象术语:的代码(C#和C ++ / CLI)在同一进程中运行时

  2. 在相对于1两个部分双方的代码库中引用)

  3. 是否有如何监控垃圾收集器的活动有什么建议 - 即编写测试时,垃圾收集时要检查。或者监视垃圾收集器本身的程序。

我已经有了的垃圾收集器一般是如何工作的理解一些,所以在这里我的问题是特定于以下情形:

I already have somewhat of an understanding of how the Garbage Collector works in general, so my questions here are specific to the following scenario:

组件


  • 大会A - (C#编写的)

  • 组件b - (用C ++ / CLI)

  • Assembly A - (written in C#)
  • Assembly B - (written in C++/CLI)

程序执行


  1. 对象 0 大会A

  2. 对象 0 <创建/ code>被传递给函数里面的组件b

  3. 参考对象 0 大会A 被释放。

  4. 组件b 保存到参考对象 0

  5. 执行结束(即通过程序退出)。

  6. 组件b 发布参考对象 0

  1. Object O is created in Assembly A.
  2. Object O is passed into a function inside Assembly B.
  3. Reference to object O in Assembly A is released.
  4. Assembly B holds onto reference to object O.
  5. Execution ends (i.e. via program exit).
  6. Assembly B releases reference to object O.

感谢。让或者我知道,如果需要进一步的信息,如果事情是不够明确。

Thanks in advance for any thoughts on this question. Let me know if further information is needed or if something is not clear enough.

修改

根据要求,我写我想描述的情况的一个粗略的例子。该 C# C ++ / CLI 代码可以在引擎收录被发现。

As per request, I have written a rough example of the scenario I'm trying to describe. The C# and C++/CLI code can be found on PasteBin.

推荐答案

在代码实际运行,没有这将是C#或C ++ / CLI。所有这一切都将是IL从C#和C ++ / CLI和机器代码从你与互操作本机代码。

When the code is actually running, none of it will be C# or C++/CLI. All of it will be IL from the C# and C++/CLI and machine code from the native code you're interoperating with.

因此,你可以重新写属于你问题为:

Hence you could re-write part of your question as:


  • 大会A - (IL,我们不知道它是写在)

  • 组件b - (IL,我们不知道它是写在)

被管理的对象,所有的他们将收集按照相同的规则垃圾,除非您使用一种机制,以防止它(GC.KeepAlive)。他们都可以在内存中移动,除非你固定这些(因为你传递地址非托管代码。

Of the managed objects, all of them will be garbage collected as per the same rules, unless you use a mechanism to prevent it (GC.KeepAlive). All of them could be moved in memory unless you pin them (because you're passing addresses to unmanaged code.

.NET探查器会给你垃圾收集一些信息,这将在性能监视器收集计数。

.NET Profiler will give you some information on garbage collection, as will the collection counts in performance monitor.

这篇关于垃圾收集跨C#和C ++ / CLI对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆