我從舊的StackOverflow文章中讀取關於何時使用stackalloc的示例。現在這個例子中有我有點納悶:Stackalloc和值類型
public unsafe void DoSomeStuff()
{
byte* unmanaged = stackalloc byte[100];
byte[] managed = new byte[100];
//Do stuff with the arrays
//When this method exits, the unmanaged array gets immediately destroyed.
//The managed array no longer has any handles to it, so it will get
//cleaned up the next time the garbage collector runs.
//In the mean-time, it is still consuming memory and adding to the list of crap
//the garbage collector needs to keep track of. If you're doing XNA dev on the
//Xbox 360, this can be especially bad.
}
現在隨時糾正我,如果我錯了,因爲我仍然在一般C#和編程相當新手。但是不是字節值類型?而不是值類型存儲在他們聲明的位置?這是否意味着在這個例子中,managed
也存儲在堆棧中,並且通過擴展當這個堆棧幀結束並且它到達主叫地址時,存儲器被自動清理,因此managed
應該以與unmanaged
在這個例子中?
使用數組時,是否存儲在堆上的字節的實際實例? –
是的,每個陣列都分配在一個連續的託管堆內存塊中。 –