加载到内存中的函数中的引用变量在堆和堆栈之间的哪里?
我创建了一个函数来从父游戏对象的所有子游戏对象中获取“RectTransform”组件。
using UnityEngine;
public class Test_Script : MonoBehaviour
{
public GameObject parent_G;
RectTransform[] child_R;
private void Awake()
{
child_R = Get_ChildRectTransform(parent_G);
}
public RectTransform[] Get_ChildRectTransform(GameObject parent)
{
int sum_Child = parent.transform.childCount;
RectTransform[] temporary_R = new RectTransform[sum_Child];
for (int i = 0; i < sum_Child; i++)
temporary_R[i] = parent.transform.GetChild(i).GetComponent<RectTransform>();
return temporary_R;
}
}
我的问题是... 函数中的“temporary_R”是否加载到堆栈中? 函数执行后是否终止? 否则,它会像我写的“new”和“array”一样加载到堆中吗?
I've made a function to get "RectTransform" components from all child GameObjects of a parent GameObject.
using UnityEngine;
public class Test_Script : MonoBehaviour
{
public GameObject parent_G;
RectTransform[] child_R;
private void Awake()
{
child_R = Get_ChildRectTransform(parent_G);
}
public RectTransform[] Get_ChildRectTransform(GameObject parent)
{
int sum_Child = parent.transform.childCount;
RectTransform[] temporary_R = new RectTransform[sum_Child];
for (int i = 0; i < sum_Child; i++)
temporary_R[i] = parent.transform.GetChild(i).GetComponent<RectTransform>();
return temporary_R;
}
}
My question is...
Is "temporary_R" in the Function loaded in Stack?
And Is it terminated after the Function?
Otherwise, is it going to be loaded in Heap as I wrote "new" and as "array"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
temporary_R
它是一个局部变量,引用RectTransform
数组。局部变量存储在堆栈中,并在函数返回时自动“释放”。即使我们通常不谈论分配/释放局部变量。实际的 RectTransforms 存储在堆上,如果它是值类型/结构,则作为数组对象的一部分,或者如果它是引用类型/类,则单独存储。这些对象将被垃圾收集器释放。
您通常不会担心这样的分配。
sum_Child
可能会相当小,因此内存量也应该受到限制。而且由于它仅保持较短的生存时间,因此很可能会在 gen 0/1 中被收集,即廉价的垃圾收集类型。我还假设它运行的频率相当低,比如每帧运行一次。您通常只会在分配大对象(即 >~ 87kb)或非常频繁地分配(例如在紧密循环内)时担心内存分配。即使这样,使用性能或内存分析器来检查分配率、GC 花费的时间等以确定是否存在问题通常也是一个好主意。
temporary_R
it a local variable that references to an array ofRectTransform
. The local variable is stored on the stack, and automatically "freed" when the function returns. Even if we typically do not talk about allocating/freeing local variables.The actual RectTransforms are stored on the heap, either as part of the array-object if it is a value type/struct or or separately if it is a reference type/class. These objects will be released by the garbage collector whenever it gets around to it.
You would typically not worry about allocations like this.
sum_Child
will probably be fairly small, so the amount of memory should also be limited. And since it is only kept alive for a short duration it will most likely be collected in gen 0/1, i.e. the cheap kind of garbage collection. I would also assume it is run fairly infrequently, like once per frame.You would typically only worry about memory allocation when allocating large objects (i.e. >~ 87kb), or allocating very frequently, like inside a tight loop. And even then it is usually a good idea to use a performance or memory profiler to check allocation rates, time spent in GC, etc to determine if it is a problem.