Halcon与C#联合编程时的内存回收机制

来源:互联网 发布:处理数据 英文 编辑:程序博客网 时间:2024/06/05 15:10

摘自halcon文档:

The .net philosophy is to let the garbage collector remove unused objes. However, because the garbage collector detects unused objects only from time to time, the used memory increases in the meantime. Even more important is that, to the garbage collector, Halcon's iconic variables(image,regions,...) seem to be rather "small",because they only contains a reference to the (in many cases rather large) iconic objects in the database. Thus ,the garbage collector may free such variables even if they are not used anymore.

 Therefor, you need to force the removal of (unused) objects. There are two ways to do this:

1) Call the garbage collector mannually . In the example Matching ,this is done after each processing run in the timer event:

private void Timer_Tick(object sender, System.EventArgs e)

                {

Action();

GC.Collect();

GC.WaitForPendingFinalizers():

}

C++ applications

GC::Collect():

GC::WaitForPendingFinalizers();

2) Dispose of individual objects manually by calling the method Dispose:

HImage  Image = new HImage("fuse");

...

Image.Dispose():


Please note that Halcon operators always create a new object instance for output parameters and return values(but not in the "constructor-like" operator calls that modify the calling instance). If the variable was already initialized,its old content( and the memory allocated for it ) still exists until the garbage collector removes it .If you want to remove it  mannualy ,you must call Dispose before assinging an object to it.


总结为一句话:对象在使用前后都需要Dispose():

原创粉丝点击