Xamarin.Android 应用程序多久调用一次垃圾收集器?
How often does garbage collector gets called in Xamarin.Android application?
我有以下 class:
public class CurrentOrder
{
//contains current order values which is global to all application
public static List<OrderArticleViewModel> listOfOrderArticles = new List<OrderArticleViewModel>();
public static string orderCustomerName;
public static string orderCustomerId;
public static string orderNumber;
public static string orderDateAndHour;
public static DateTime executionOrderDate = DateTime.Now.AddDays(1);
private CurrentOrder()
{
}
}
我在整个应用程序中使用它的字段作为全局变量,例如:CurrentOrder.orderNumber
。当我在某些 activity 并按下后退按钮时,我想清除所有 class 字段值,我是这样做的:
CurrentOrder.listOfOrderArticles = new List<OrderArticleViewModel>();
CurrentOrder.orderCustomerName = null;
CurrentOrder.orderCustomerId = null;
CurrentOrder.orderNumber = null;
CurrentOrder.orderDateAndHour = null;
CurrentOrder.executionOrderDate = DateTime.Now.AddDays(1);
但据我所知,这些字段的值保留在内存中,唯一的问题是现在我的变量指向另一个地方。如果我单击后退按钮 1000 次,我将在内存中拥有 1000 次没有引用它们的字段。我听说垃圾收集器会注意销毁没有任何东西指向它们的值,但这种情况发生的频率如何?是否可以在没有垃圾收集器清理的情况下按返回按钮 100 次?
垃圾回收之间没有固定的时间间隔。垃圾收集器根据剩余可分配内存的大小调用。 c#和java都是object-oriented语言,所以我们不需要像c/c++那样手动分配和释放内存。
垃圾收集器将帮助开发者释放内存。 Xamarin.Android使用的是c#语言,所以需要CLR帮助进程管理内存(Native Android based on ART and Dalvik)。
Here是什么时候调用GC的条件:
Garbage collection occurs when one of the following conditions is true:
1.The system has low physical memory. This is detected by either the low memory notification from the OS or low memory indicated by the host.
2.The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.
3.The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.
而且我认为memory churn会证明GC调用没有固定的间隔。
关于您的问题:
Is it posible to press back button 100 times without the garbage collector cleaning?
根据你的Android系统环境(你的app是前台还是后台?内存够不够?)。不过最后还是会gc的。
所以,关于内存问题,我认为内存泄漏和OOM(主要是由于Bitmap)应该得到更多的关注。并且内存流失也应该避免,因为它会影响 Android render(UI Performance).
我有以下 class:
public class CurrentOrder
{
//contains current order values which is global to all application
public static List<OrderArticleViewModel> listOfOrderArticles = new List<OrderArticleViewModel>();
public static string orderCustomerName;
public static string orderCustomerId;
public static string orderNumber;
public static string orderDateAndHour;
public static DateTime executionOrderDate = DateTime.Now.AddDays(1);
private CurrentOrder()
{
}
}
我在整个应用程序中使用它的字段作为全局变量,例如:CurrentOrder.orderNumber
。当我在某些 activity 并按下后退按钮时,我想清除所有 class 字段值,我是这样做的:
CurrentOrder.listOfOrderArticles = new List<OrderArticleViewModel>();
CurrentOrder.orderCustomerName = null;
CurrentOrder.orderCustomerId = null;
CurrentOrder.orderNumber = null;
CurrentOrder.orderDateAndHour = null;
CurrentOrder.executionOrderDate = DateTime.Now.AddDays(1);
但据我所知,这些字段的值保留在内存中,唯一的问题是现在我的变量指向另一个地方。如果我单击后退按钮 1000 次,我将在内存中拥有 1000 次没有引用它们的字段。我听说垃圾收集器会注意销毁没有任何东西指向它们的值,但这种情况发生的频率如何?是否可以在没有垃圾收集器清理的情况下按返回按钮 100 次?
垃圾回收之间没有固定的时间间隔。垃圾收集器根据剩余可分配内存的大小调用。 c#和java都是object-oriented语言,所以我们不需要像c/c++那样手动分配和释放内存。
垃圾收集器将帮助开发者释放内存。 Xamarin.Android使用的是c#语言,所以需要CLR帮助进程管理内存(Native Android based on ART and Dalvik)。
Here是什么时候调用GC的条件:
Garbage collection occurs when one of the following conditions is true:
1.The system has low physical memory. This is detected by either the low memory notification from the OS or low memory indicated by the host.
2.The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.
3.The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.
而且我认为memory churn会证明GC调用没有固定的间隔。
关于您的问题:
Is it posible to press back button 100 times without the garbage collector cleaning?
根据你的Android系统环境(你的app是前台还是后台?内存够不够?)。不过最后还是会gc的。
所以,关于内存问题,我认为内存泄漏和OOM(主要是由于Bitmap)应该得到更多的关注。并且内存流失也应该避免,因为它会影响 Android render(UI Performance).