Monodroid GREF problem best practice?

前端 未结 3 1713
醉话见心
醉话见心 2021-01-27 03:51

I have the following test code (based on standard monodroid HelloWorld)

namespace TestGREF
{
    [Activity (Label = \"TestGREF\", MainLauncher = true)]
    publi         


        
3条回答
  •  悲哀的现实
    2021-01-27 04:14

    for(int i=0;i<10000;i++){
        var obj = new Java.Lang.Object(new System.IntPtr(i));
    
        //...some stuff here. Instead of Java.Lang.Object may be
        //something much more useful.
    
        obj.Dispose(); //Deletes an object and GREF too. 
        //Cannot be used if object is still used in dalvik VM
    }
    

    If you cannot use Dispose() (for example, unmanaged object is a part of layout, which will be used by android lated, but not by C# code), use GC.Collect() wisely. GC.Collect() kills all the GREFs to variables, which are out of usage by Mono Environment and out of current scope.

提交回复
热议问题