What and where are the stack and heap?

Unity3D/C# 2019. 1. 18. 17:58
반응형

public void Method1()
{
    int i = 4;
    int y = 2;
    class1 cls1 = new class1();
}

Here's how the memory is managed

Picture of variables on the stack

Local Variables that only need to last as long as the function invocation go in the stack. The heap is used for variables whose lifetime we don't really know up front but we expect them to last a while. In most languages it's critical that we know at compile time how large a variable is if we want to store it on the stack.

Objects (which vary in size as we update them) go on the heap because we don't know at creation time how long they are going to last. In many languages the heap is garbage collected to find objects (such as the cls1 object) that no longer have any references.

In Java, most objects go directly into the heap. In languages like C / C++, structs and classes can often remain on the stack when you're not dealing with pointers.

More information can be found here:

The difference between stack and heap memory allocation « timmurphy.org

and here:

Creating Objects on the Stack and Heap

This article is the source of picture above: Six important .NET concepts: Stack, heap, value types, reference types, boxing, and unboxing - CodeProject

but be aware it may contain some inaccuracies.








참조 : https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap

반응형

'Unity3D > C#' 카테고리의 다른 글

ReSharper  (0) 2019.01.23
C# Split List  (0) 2019.01.22
Visual Studio Code 에서 Newtonsoft.JSON 사용하기  (0) 2018.12.28
Visual Studio Code 에서 C# 콘솔 프로젝트 하기  (0) 2018.12.28
SOLID (객체 지향 설계)  (0) 2018.09.03
: