[英]Do array properties cause memory allocation on the heap?
Consider the following: 考虑以下:
public class FooBar {
public int[] SomeNumbers {
get { return _someNumbers; }
private set;
}
private int[] _someNumbers;
public FooBar() {
_someNumbers = new int[2];
_someNumbers[0] = 1;
_someNumbers[1] = 2;
}
}
// in some other method somewhere...
FooBar foobar = new FooBar();
Debug.Log(foobar.SomeNumbers[0]);
What I am wondering is, does calling the SomeNumbers property cause a heap allocation; 我想知道的是,调用SomeNumbers属性是否会导致堆分配? basically does it cause a copy of the array to be created, or is it just a pointer?
基本上是导致要创建数组的副本,还是仅仅是指针?
I ask because I am trying to resolves some GC issues I have due to functions that return arrays, and I want to make sure my idea of caching some values like this will actually make a difference 我问是因为我试图解决由于返回数组的函数而导致的某些GC问题,并且我想确保将这样的值缓存的想法实际上会有所作为
Arrays are always reference types, so yes, it is "basically returning a pointer". 数组始终是引用类型,因此,是的,它是“基本上返回指针”。
If you are trying to debug memory issues I recommend using a memory profiler. 如果您要调试内存问题,建议您使用内存分析器。 There is one built in to Visual Studio or you can use a 3rd party one (I personally like DotMemory , it has a 5 day free trial).
Visual Studio内置了一个,也可以使用第3方(我个人很喜欢DotMemory ,它有5天的免费试用期)。 Using a profiler will help you identify what is creating memory objects and what is keeping memory objects alive.
使用探查器将帮助您确定正在创建内存对象的内容以及使内存对象保持活动状态的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.