C
C#Srejon Khan

Stack and Heap Memory [Answered]

I've been correcting some of my misconception about stack and heap memory allocation. So, this is my final verdict , I just want to make sure that I'm not holding any misconceptions. I had a misconception that all value types are stored on stack and all reference type are stored on heap, which I found out that partially wrong. So, after some research, this is my final verdict - "Short lived variables are considered to go on the stack/registers, where long lived variables are considered to go on the heap." The variable of a reference type just hold the reference (address) of the instance of that reference type, and the instance of a reference type always stored in heap, right? And this variable of reference type can either stay in stack or heap depending on it's lifetime, right? So, if value type is defined as local variable (including parameter), it will be stored on stack if not captured by closure. If value type is defined as a member of a reference type, then that value type is also going to be stored on the heap? Along with that reference type? And, whatever variables Closure captures, will always goes to heap, right? As that is considered as long lived.
When we use ref keyword with a value type, it just passes the reference. It doesn't box the value, right?
C
canton7570d ago
I think the "short-lived" vs "long-lived" thing isn't relevant, and is confusing you Variables/fields can be reference or value types. With a value type, the value is stored right there in the variable/field; with a reference type, a block of memory is allocated on the heap, the value is stored in that heap memory, and the variable/field holds a pointer to that memory. Local variables are stored on the stack, unless they're captured by a closure in which case they might become fields in a compiler-generated class. Fields are stored in whatever memory holds the containing class/struct
K
Kuinox570d ago
"Short lived variables are considered to go on the stack/registers, where long lived variables are considered to go on the heap."
no
The variable of a reference type just hold the reference (address) of the instance of that reference type, and the instance of a reference type always stored in heap
yes, but no. Today that technically right, but tomorrow the runtime may sometimes allocate it on the stack, because it's cheaper: https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/jit/object-stack-allocation.md
GitHub
runtime/object-stack-allocation.md at main · dotnet/runtime
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - runtime/object-stack-allocation.md at main · dotnet/runtime
K
Kuinox570d ago
Well first, the stack: You have a function, your functions have variables, the computer need a place to put these. The easiest way to do it, is the stack, when you run a function, your variables values are put on top of the stack, when exiting the function, all variables declared in this functions are forgotten
whatever variables Closure captures, will always goes to heap, right?
closures also may be on the stack, like my previous point, afaik that not the case today, it's the case in java.
And, whatever variables Closure captures, will always goes to heap, right? As that is considered as long lived.
I think you are mixing up GC concept of short lived, and long lived the GC only manage heap objects, and there is short, and long lived objects there.
A
Auger570d ago
There is also a large object heap, I think
UU
Unknown User570d ago
A
Accord569d ago
✅ This post has been marked as answered!
SK
Srejon Khan569d ago
Hello, thanks for your response and the info about compiler-generated class.
SK
Srejon Khan569d ago
Hello, first of all, thanks for your response. I actually got the idea about long lived and short lived from this article: https://learn.microsoft.com/en-us/archive/blogs/ericlippert/the-truth-about-value-types#:~:text=Code%20in%C2%A0a,required%20by%20M.) where the author mention about short living and long living concept from activation period of a method. Is it referencing GC concept?
K
Kuinox569d ago
not here article is 12 year old, today short/long lived objects is more often talked for the objects generation in the GC
C
canton7569d ago
The article is very clear to draw a distinction between references and instances of reference types. Your question lost that, I think
K
Kuinox569d ago
Fundamentals of garbage collection
Learn how the garbage collector works and how it can be configured for optimum performance.
C
canton7569d ago
I mean, it's true that local variables and parameters tend to be short-lived, and fields tend to be long-lived
K
Kuinox569d ago
in fact, the local variable are destroyed when you exit the function scope
C
canton7569d ago
I'd say that's an effect rather than a cause, but it's fine. The article isn't wrong
K
Kuinox569d ago
Seeing the author of the article, i'll question myself before saying article is wrong when
C
canton7569d ago
But I think you read it, and its very careful distinction between value types, references, and instances of reference types, and lost the nuance there. If you replace "short-lived" with "local variable" etc, you should find it agrees with what I said above
SK
Srejon Khan569d ago
Yes. So, i guess the choosing of word was wrong for me.
C
canton7569d ago
Where a variable (value type or reference) is stored depends on whether it is a local or field. That's cut and dried. The article adds the nuance that most of the time, locals are short-lived and fields are long-lived
SK
Srejon Khan569d ago
Thanks @here
K
Kuinox569d ago
np !
Want results from more Discord servers?
Add your server
More Posts
PartialType from Nest.js in CsharpIs there is a possibility to recreate a behaviour like the partialType from Nestjs (=> https://docs.Trying to add GrossWeight each time it shows up in the listTrying to learn how to use a ForEach loop while doing my homework. I feel like this should be easierHow to read DateTime from SQLite database using System.Data.SQLite?I have a sqlite database with a DateTime entry. Since SQLite doesn't have a DateTime data type, it sBlazor VSCode --unable to find an installation of the browser on your system.-- [Answered]I'm getting an error `Unable to launch browser: "Unable to find an installation of the browser on yoCollection of different classesI have a situation where I need to store two different types together, and I'm not sure what's a gooNuGet behind a proxyHey I am in the process of releasing a .NET project as a Docker container. How can I configure NugetFluentAPI, Joining Table of the same entitiesI have entities User: id, payments; Payments: id, user1, user2 I want user to have n payments, and EF sets a property out of nowhere [Solved] [Answered]```cs public int RepliesLength { get; set; } = 0; public int RepliesCount { get; set; } = 0; publicJenkins unable to build CSharp project [Answered]any ideas why that might be?✅ Embedding an application WinUI 3This may be a stupid question, but is it possible to have the UI of a different program be a compone.Net 6 on IIS (Port conflict)Hey! I have an application that I would like to run on port 8081, and it does use port 8081 in startupload file that have huge size using asp net framework formhi all, i understand that we can't upload file to a form with size larger than 2GB using asp net (reCast failing on runtimeI have a construct. that i simplified in a little example seen below. I was wondering how i can resoNeed help on how to make an AOL style movable window inside of the main windowI wanna try to make a little AOL like program that has moveable windows inside of the main window wiNested KeyValuePair```private KeyValuePair<string, double> priceAndCurrency; private KeyValuePair<string, KeyVWhere is AssemblyInfo.cs [Answered]I just learned that I am not supposed to make my projects using the .NET Framework project templatesTrack gitignored ChangesI work on a software where the base product is an off the shelf vendor product and we have added cus.NET 6.0 not showing up in Visual Studio [Answered]I have .NET 6.0 installed, but it's not in the dropdown. How can I switch to .NET 6?SignalR to SockJs [Answered]Trying to used SignalR in C# to set up a websocket with a SockJS server (Octopi for a 3D printer). ICommunication between variables in Multiple ThreadsHello, I am kinda new to this and I've never really worked with multi threads, decided to finally l