打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用delegate时产生的GC

public delegate void Del();
void Fun()
{

}
void TestDelegete()
{
    Del d = Fun;
}

这几行代码,在调用TestDelegete()时总会产生104B的GC,而由于各种原因这个方法是需要被调用很多次的。问下这个GC能够被减少或者避免么?



Mono 对象可以用GC,Unity的对象有自己的处理规则,不适用GC处理;
话说实在是要停止或减少GC的话,可以尝试下面的办法:

using UnityEngine;
using System.Collections.Generic;
using System;
using System.Runtime.InteropServices;
public class gc_manager : MonoBehaviour {
    [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
    static extern IntPtr GetModuleHandle(string lpModuleName);
    [DllImport("kernel32.dll", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
    static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
    static Action mono_gc_disable;
    static Action mono_gc_enable;
    static bool mono_gc_loaded=false;
    static bool load_mono_gc() {
        if (mono_gc_loaded) {
            return true;
        }
        unsafe {
            //extracted from mono.pdb using dbh.exe (using the "enum *!*mono_gc_*" command)
            //note: for the 64 bit editor, there is only a 64 bit version of mono.pdb, so you need to also download the 32 bit editor to update this for 32 bit standalone builds
            // (you also need to decide which version of the dll to use; this can be done by comparing the mono_gc_collect offset with the two offsets for the 32 bit and 64 bit dlls)
            int offset_mono_gc_disable=0x1b100;
            int offset_mono_gc_enable=0x1b108;
            int offset_mono_gc_collect=0x1b0b4; //this is used to verify that mono.dll hasn't changed
            IntPtr mono_module=GetModuleHandle("mono.dll");
            IntPtr func_ptr_mono_gc_collect=new IntPtr(mono_module.ToInt64()+offset_mono_gc_collect);
            IntPtr expected_func_ptr_mono_gc_collect=GetProcAddress(mono_module, "mono_gc_collect");
            if (func_ptr_mono_gc_collect!=expected_func_ptr_mono_gc_collect) {
                //if you see this error, you need to update the "offset_mono_gc_" variables above
                Debug.Log("Cannot load gc functions. Expected collect at "+ func_ptr_mono_gc_collect.ToInt64() +" Actual at "+ func_ptr_mono_gc_collect.ToInt64() +" Module root "+ mono_module.ToInt64());
                return false;
            }
            mono_gc_enable=(Action)Marshal.GetDelegateForFunctionPointer(new IntPtr(mono_module.ToInt64()+offset_mono_gc_enable), typeof(Action));
            mono_gc_disable=(Action)Marshal.GetDelegateForFunctionPointer(new IntPtr(mono_module.ToInt64()+offset_mono_gc_disable), typeof(Action));
        }
        mono_gc_loaded=true;
        return true;
    }
};



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
golang弹出消息框
海康SDK编程指南
C# 实现会定时关闭的弹出对话框
如何在C#中直接操作C++结构体 | 学步园
C#中使用OpenGL:(二)C#调用C/C++的dll
鼎砥投资论坛
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服