Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How memory is allocated in .Net application

How memory is allocated in .Net application

Scheduled Pinned Locked Moved C#
questioncsharpdata-structuresperformancecareer
26 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee
    wrote on last edited by
    #1

    i like to know in details and want to visualize how memory is allocated in dotnet program. sorry because i will be asking few question. so please answer point wise in detail. 1) suppose i have class called employee which has data member called empNo, Name and Salary. so i like to know when we will create object of employee then how memory will be allocated ? 2) where memory will be allocated for employee object? in heap or stack? 3) why memory is allocated in heap for object and local variable in stack? 4) i hear some time memory is allocated in heap for local variable but when? 5) where actual data is stored like employee name or salary in heap or stack? if in stack why? why not in heap? 6) i heard that memory is allocated for static class in high frequency heap........what is high frequency heap and how it is different than normal heap?

    tbhattacharjee

    OriginalGriffO F 2 Replies Last reply
    0
    • T Tridip Bhattacharjee

      i like to know in details and want to visualize how memory is allocated in dotnet program. sorry because i will be asking few question. so please answer point wise in detail. 1) suppose i have class called employee which has data member called empNo, Name and Salary. so i like to know when we will create object of employee then how memory will be allocated ? 2) where memory will be allocated for employee object? in heap or stack? 3) why memory is allocated in heap for object and local variable in stack? 4) i hear some time memory is allocated in heap for local variable but when? 5) where actual data is stored like employee name or salary in heap or stack? if in stack why? why not in heap? 6) i heard that memory is allocated for static class in high frequency heap........what is high frequency heap and how it is different than normal heap?

      tbhattacharjee

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      I'm not going to answer your questions - but not because I'm nasty: because the questions are wrong! :laugh: The distinctions between stack and heap isn't as simple as you might think: and a variable in C# probably isn't what you think it is either - which is at the root of the "wrongness" of your questions. So: what is a variable? Simply put, a variable in a method is always allocated on the stack - but it's content may or may not be on the stack, depending on what type of data the variable refers to. If it's a ValueType, then the variable is allocated enough space to hold the whole value: and int is a ValueType, and so is a Point. These are always the same size: you cannot extend the size of a ValueType in any way. If it's a reference type, then the variable always holds a reference to the actual data, rather than the data itself, and again the variable is a fixed size - only this time it is always either 32 bits or 64 bits depending on the environment your code is executing in. String is a reference type, so is any array. And that's important: because all reference values are allocated on the Heap: never, ever on the stack.

      int i = 6;

      Integers are ValueTypes, so "i" is on the stack, and is the value of the integer - in this case 6.

      Button b = new Button();

      Button is a reference type, so the actual data for the new instance is stored on the heap - but "b" is the variable and it is located on the stack, and contains a reference to the actual instance. But...that doesn't mean that all ValueType instances are on the stack - they aren't: they can be embedded in reference type instances:

      public class MyClass
      {
      public int Value;
      }
      ...
      MyClass mc= new MyClass();
      mc.Value = 6;

      MyClass is a reference type (all classes are reference types, all structs are value types) so the instance data is on the heap, and that includes the value type integer "Value" it contains. "mc" is on the stack and holds a reference to the heap based actual data. From here it starts to get very, very complicated - there is something called "boxing" to consider as well, where a stack based value is copied to the heap and passed as a reference - far too complicated for a little text box like this one! Get a c# book - they all explain this with pictures (which helps a lot) and when you've read it have a look at this:

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      L T S 3 Replies Last reply
      0
      • T Tridip Bhattacharjee

        i like to know in details and want to visualize how memory is allocated in dotnet program. sorry because i will be asking few question. so please answer point wise in detail. 1) suppose i have class called employee which has data member called empNo, Name and Salary. so i like to know when we will create object of employee then how memory will be allocated ? 2) where memory will be allocated for employee object? in heap or stack? 3) why memory is allocated in heap for object and local variable in stack? 4) i hear some time memory is allocated in heap for local variable but when? 5) where actual data is stored like employee name or salary in heap or stack? if in stack why? why not in heap? 6) i heard that memory is allocated for static class in high frequency heap........what is high frequency heap and how it is different than normal heap?

        tbhattacharjee

        F Offline
        F Offline
        F ES Sitecore
        wrote on last edited by
        #3

        See if this helps, click through the various parts http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx[^] 1 The OS is asked for memory to be reserved to hold the contents of the object, the object is stored at that address on the heap. As already mentioned, the variable that points to the object is on the stack. 2 Heap 3 Performance. Allocating and deallocating memory is expensive (have a for loop from 1 to 1000000 and in that loop update an int "i = i + 1", do a second loop and in that loop concat a string s = s & "x". Time how long both loops take to execute - the string loop is doing memory allocation, the int loop isn't). With the stack the memory is already allocated, you can put things on the stack and take them off "for free". Every time you put something in the heap you need to request a memory allocation, when you take things off that memory has to eventually be deallocated. Because the stack is a known fixed size you can't go storing Word documents in it, it would run out (make a function that just calls itself, every call puts an exit reference on the stack, after a second or so you'll get a stack overflow as you will have run out of stack space). Also things on the stack are "last on first out", they remain in sequential order at known addresses. As the stack is pre-allocated, a "pointer" is held that references the next bit of free space. As you put something on the stack the pointer goes up, you take something off it goes down. If you have, say, an int (10), a string ("ab") and an int (20) on the stack; Stack add 0 - 10 Stack add 1 - a Stack add 2 - b Stack add 3 - 20 Stack add 4 - < stack pointer points here, put something else on and this is where it goes and then wanted to change the string from "ab" to "abc"...where do you store the "c"? There is no space as your second int is on top of it so to increase the string from "ab" to "abc" would require reordering everything above it. So you can only put things on the stack that don't change in size. An int is always going to be 32 bits for example (depending on os and hardware). 5 Value types, things of known, unchangeable sizes, tend to go on the stack. "objects" of unknown sizes that can grow and shrink go o

        T 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          I'm not going to answer your questions - but not because I'm nasty: because the questions are wrong! :laugh: The distinctions between stack and heap isn't as simple as you might think: and a variable in C# probably isn't what you think it is either - which is at the root of the "wrongness" of your questions. So: what is a variable? Simply put, a variable in a method is always allocated on the stack - but it's content may or may not be on the stack, depending on what type of data the variable refers to. If it's a ValueType, then the variable is allocated enough space to hold the whole value: and int is a ValueType, and so is a Point. These are always the same size: you cannot extend the size of a ValueType in any way. If it's a reference type, then the variable always holds a reference to the actual data, rather than the data itself, and again the variable is a fixed size - only this time it is always either 32 bits or 64 bits depending on the environment your code is executing in. String is a reference type, so is any array. And that's important: because all reference values are allocated on the Heap: never, ever on the stack.

          int i = 6;

          Integers are ValueTypes, so "i" is on the stack, and is the value of the integer - in this case 6.

          Button b = new Button();

          Button is a reference type, so the actual data for the new instance is stored on the heap - but "b" is the variable and it is located on the stack, and contains a reference to the actual instance. But...that doesn't mean that all ValueType instances are on the stack - they aren't: they can be embedded in reference type instances:

          public class MyClass
          {
          public int Value;
          }
          ...
          MyClass mc= new MyClass();
          mc.Value = 6;

          MyClass is a reference type (all classes are reference types, all structs are value types) so the instance data is on the heap, and that includes the value type integer "Value" it contains. "mc" is on the stack and holds a reference to the heap based actual data. From here it starts to get very, very complicated - there is something called "boxing" to consider as well, where a stack based value is copied to the heap and passed as a reference - far too complicated for a little text box like this one! Get a c# book - they all explain this with pictures (which helps a lot) and when you've read it have a look at this:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          +5 ! small typo here:

          OriginalGriff wrote:

          copied to the head

          OriginalGriffO 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            I'm not going to answer your questions - but not because I'm nasty: because the questions are wrong! :laugh: The distinctions between stack and heap isn't as simple as you might think: and a variable in C# probably isn't what you think it is either - which is at the root of the "wrongness" of your questions. So: what is a variable? Simply put, a variable in a method is always allocated on the stack - but it's content may or may not be on the stack, depending on what type of data the variable refers to. If it's a ValueType, then the variable is allocated enough space to hold the whole value: and int is a ValueType, and so is a Point. These are always the same size: you cannot extend the size of a ValueType in any way. If it's a reference type, then the variable always holds a reference to the actual data, rather than the data itself, and again the variable is a fixed size - only this time it is always either 32 bits or 64 bits depending on the environment your code is executing in. String is a reference type, so is any array. And that's important: because all reference values are allocated on the Heap: never, ever on the stack.

            int i = 6;

            Integers are ValueTypes, so "i" is on the stack, and is the value of the integer - in this case 6.

            Button b = new Button();

            Button is a reference type, so the actual data for the new instance is stored on the heap - but "b" is the variable and it is located on the stack, and contains a reference to the actual instance. But...that doesn't mean that all ValueType instances are on the stack - they aren't: they can be embedded in reference type instances:

            public class MyClass
            {
            public int Value;
            }
            ...
            MyClass mc= new MyClass();
            mc.Value = 6;

            MyClass is a reference type (all classes are reference types, all structs are value types) so the instance data is on the heap, and that includes the value type integer "Value" it contains. "mc" is on the stack and holds a reference to the heap based actual data. From here it starts to get very, very complicated - there is something called "boxing" to consider as well, where a stack based value is copied to the heap and passed as a reference - far too complicated for a little text box like this one! Get a c# book - they all explain this with pictures (which helps a lot) and when you've read it have a look at this:

            T Offline
            T Offline
            Tridip Bhattacharjee
            wrote on last edited by
            #5

            thanks for your time and answer. you said : that's important: because all reference values are allocated on the Heap: never, ever on the stack. int i = 6; Integers are Value types, so "i" is on the stack, and is the value of the integer - in this case 6. i heard that reference type stored in heap and their corresponding value stored in stack. you said but this is not very clear MyClass is a reference type (all classes are reference types, all structs are value types) so the instance data is on the heap, and that includes the value type integer "Value" it contains. "mc" is on the stack and holds a reference to the heap based actual data. is there any book which i can download its pdf version which you read and tell me which chapter discuss memory management related things for c# or any other book which easily help me to understand with picture how and when data stored in heap and stack. share knowledge if you know what is high frequency heap?

            tbhattacharjee

            OriginalGriffO L 3 Replies Last reply
            0
            • L Lost User

              +5 ! small typo here:

              OriginalGriff wrote:

              copied to the head

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Fixed! (Along with a couple of others... :-O )

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • F F ES Sitecore

                See if this helps, click through the various parts http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx[^] 1 The OS is asked for memory to be reserved to hold the contents of the object, the object is stored at that address on the heap. As already mentioned, the variable that points to the object is on the stack. 2 Heap 3 Performance. Allocating and deallocating memory is expensive (have a for loop from 1 to 1000000 and in that loop update an int "i = i + 1", do a second loop and in that loop concat a string s = s & "x". Time how long both loops take to execute - the string loop is doing memory allocation, the int loop isn't). With the stack the memory is already allocated, you can put things on the stack and take them off "for free". Every time you put something in the heap you need to request a memory allocation, when you take things off that memory has to eventually be deallocated. Because the stack is a known fixed size you can't go storing Word documents in it, it would run out (make a function that just calls itself, every call puts an exit reference on the stack, after a second or so you'll get a stack overflow as you will have run out of stack space). Also things on the stack are "last on first out", they remain in sequential order at known addresses. As the stack is pre-allocated, a "pointer" is held that references the next bit of free space. As you put something on the stack the pointer goes up, you take something off it goes down. If you have, say, an int (10), a string ("ab") and an int (20) on the stack; Stack add 0 - 10 Stack add 1 - a Stack add 2 - b Stack add 3 - 20 Stack add 4 - < stack pointer points here, put something else on and this is where it goes and then wanted to change the string from "ab" to "abc"...where do you store the "c"? There is no space as your second int is on top of it so to increase the string from "ab" to "abc" would require reordering everything above it. So you can only put things on the stack that don't change in size. An int is always going to be 32 bits for example (depending on os and hardware). 5 Value types, things of known, unchangeable sizes, tend to go on the stack. "objects" of unknown sizes that can grow and shrink go o

                T Offline
                T Offline
                Tridip Bhattacharjee
                wrote on last edited by
                #7

                thanks for the answer but still few lines are not very clear. can u plzz explain it more elaborately in easy way. you said not clear "As you put something on the stack the pointer goes up, you take something off it goes down. If you have, say, an int (10), a string ("ab") and an int (20) on the stack; Stack add 0 - 10 Stack add 1 - a Stack add 2 - b Stack add 3 - 20 Stack add 4 - < stack pointer points here, put something else on and this is where it goes and then wanted to change the string from "ab" to "abc"...where do you store the "c"? There is no space as your second int is on top of it so to increase the string from "ab" to "abc" would require reordering everything above it. So you can only put things on the stack that don't change in size. An int is always going to be 32 bits for example (depending on os and hardware). 2) you said but not clear "Of course value types can end up on the heap too due to things already mentioned, due to boxing, static variables etc,"

                tbhattacharjee

                F L 2 Replies Last reply
                0
                • T Tridip Bhattacharjee

                  thanks for your time and answer. you said : that's important: because all reference values are allocated on the Heap: never, ever on the stack. int i = 6; Integers are Value types, so "i" is on the stack, and is the value of the integer - in this case 6. i heard that reference type stored in heap and their corresponding value stored in stack. you said but this is not very clear MyClass is a reference type (all classes are reference types, all structs are value types) so the instance data is on the heap, and that includes the value type integer "Value" it contains. "mc" is on the stack and holds a reference to the heap based actual data. is there any book which i can download its pdf version which you read and tell me which chapter discuss memory management related things for c# or any other book which easily help me to understand with picture how and when data stored in heap and stack. share knowledge if you know what is high frequency heap?

                  tbhattacharjee

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Tridip Bhattacharjee wrote:

                  is there any book which i can download its pdf version

                  Do not ask that here again! Most (if not all) PDF copies of books are in violation of copyright: and we do not support that in any way, shape, or form. Bear in mind that nearly all of the 11 million member here are in the business of getting paid to write something it is very easy to copy and download and you might see part of why! You will not get links to anything illegal or immoral here. And if you ask again, there is a good chance you may be banned from the site...permanently. There are several different type of heap in .NET - and it really does get complicated. Seriously, find a book - and buy it - it will be able to go into such things in a lot more detail than we can here.

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  T J 2 Replies Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Tridip Bhattacharjee wrote:

                    is there any book which i can download its pdf version

                    Do not ask that here again! Most (if not all) PDF copies of books are in violation of copyright: and we do not support that in any way, shape, or form. Bear in mind that nearly all of the 11 million member here are in the business of getting paid to write something it is very easy to copy and download and you might see part of why! You will not get links to anything illegal or immoral here. And if you ask again, there is a good chance you may be banned from the site...permanently. There are several different type of heap in .NET - and it really does get complicated. Seriously, find a book - and buy it - it will be able to go into such things in a lot more detail than we can here.

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                    T Offline
                    T Offline
                    Tridip Bhattacharjee
                    wrote on last edited by
                    #9

                    i am very sorry but it would be helpful if you suggest any book name which help me to understand memory management including heap & stack. looking for help. thanks

                    tbhattacharjee

                    OriginalGriffO 1 Reply Last reply
                    0
                    • T Tridip Bhattacharjee

                      thanks for your time and answer. you said : that's important: because all reference values are allocated on the Heap: never, ever on the stack. int i = 6; Integers are Value types, so "i" is on the stack, and is the value of the integer - in this case 6. i heard that reference type stored in heap and their corresponding value stored in stack. you said but this is not very clear MyClass is a reference type (all classes are reference types, all structs are value types) so the instance data is on the heap, and that includes the value type integer "Value" it contains. "mc" is on the stack and holds a reference to the heap based actual data. is there any book which i can download its pdf version which you read and tell me which chapter discuss memory management related things for c# or any other book which easily help me to understand with picture how and when data stored in heap and stack. share knowledge if you know what is high frequency heap?

                      tbhattacharjee

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      There's a legal PDF Book linked here: http://www.codeproject.com/Messages/4977275/free-introductory-book-on-Csharp-programming-from.aspx[^] I don't know if it covers memory management but it probably contains other useful stuff.

                      Tridip Bhattacharjee wrote:

                      share knowledge if you know what is high frequency heap?

                      The CLR (Common Language Runtime) uses the high frequency heap to store frequently used data required to run a .Net program, e.g. method tables. It's nothing that's particularly important to know for writing a .Net application.

                      T 1 Reply Last reply
                      0
                      • T Tridip Bhattacharjee

                        i am very sorry but it would be helpful if you suggest any book name which help me to understand memory management including heap & stack. looking for help. thanks

                        tbhattacharjee

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        No, since you have already indicated that you are planning on downloading it. We don't encourage that at all.

                        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        1 Reply Last reply
                        0
                        • T Tridip Bhattacharjee

                          thanks for your time and answer. you said : that's important: because all reference values are allocated on the Heap: never, ever on the stack. int i = 6; Integers are Value types, so "i" is on the stack, and is the value of the integer - in this case 6. i heard that reference type stored in heap and their corresponding value stored in stack. you said but this is not very clear MyClass is a reference type (all classes are reference types, all structs are value types) so the instance data is on the heap, and that includes the value type integer "Value" it contains. "mc" is on the stack and holds a reference to the heap based actual data. is there any book which i can download its pdf version which you read and tell me which chapter discuss memory management related things for c# or any other book which easily help me to understand with picture how and when data stored in heap and stack. share knowledge if you know what is high frequency heap?

                          tbhattacharjee

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #12

                          Tridip Bhattacharjee wrote:

                          i heard that reference type stored in heap and their corresponding value stored in stack.

                          No. The instance (which holds the actual values) is always on the heap: only the reference to the instance is ever on the stack. Let's think about instances for a moment. What is an instance? Basically, it's a specific example of something. "Car" is a generic idea - you know what a car is, it's got an engine, four wheels, that sort of thing. But a generic idea is not something you can drive to the shops in: for that you need "this car", "that car", "your car", "my car" - these are instances of the generic idea. Consider this:

                          What colour is a Car?

                          You can't answer, because a generic idea doesn't have a colour.

                          What colour is your car?

                          That's a lot easier: "your car is red", or "your car is blue" - because now you are talking about an instance instead of the generic idea. So in terms of variables all instances of a Car are stored on the Heap, but the references to them are stored on the stack:

                          Car myCar = new Mercedes("Red"); // I wish, it's not even close to new...
                          Car yourCar = new Bugatti("Black"); // Probably...:laugh:

                          "myCar" and "yourCar" are the variables and they are on the stack - but the actual Cars the refer to are on the heap. And when I change cars:

                          myCar = new BrokenDownHeapOfJunkThatWasABMWOnce();

                          That only changes the reference that "myCar" held - it doesn't destroy the "old" car I used to have, it just lets it be referenced by it's new owner. If you crash your car, then I could sell you my car, to get a new one myself:

                          yourCar.Dispose();
                          yourCar = myCar;
                          myCar = new BrokenDownHeapOfJunkThatWasABMWOnce();

                          The variables are the same, but the items they reference have changed. Make sense?

                          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          1 Reply Last reply
                          0
                          • T Tridip Bhattacharjee

                            thanks for the answer but still few lines are not very clear. can u plzz explain it more elaborately in easy way. you said not clear "As you put something on the stack the pointer goes up, you take something off it goes down. If you have, say, an int (10), a string ("ab") and an int (20) on the stack; Stack add 0 - 10 Stack add 1 - a Stack add 2 - b Stack add 3 - 20 Stack add 4 - < stack pointer points here, put something else on and this is where it goes and then wanted to change the string from "ab" to "abc"...where do you store the "c"? There is no space as your second int is on top of it so to increase the string from "ab" to "abc" would require reordering everything above it. So you can only put things on the stack that don't change in size. An int is always going to be 32 bits for example (depending on os and hardware). 2) you said but not clear "Of course value types can end up on the heap too due to things already mentioned, due to boxing, static variables etc,"

                            tbhattacharjee

                            F Offline
                            F Offline
                            F ES Sitecore
                            wrote on last edited by
                            #13

                            The heap is just a big section of memory that you can allocate if you need, it's a free-for-all where objects are placed wherever there is space. A stack in a small, pre-allocated piece of memory that you use in a specific way. Imagine a stack of plates. Each time you want to store something you place it on the top of the stack of plates. On top of the existing plates is the only place a new plate can go. To take plates off, you can only take off the top plate. Think of this code

                            function A()
                            {
                            int x = 1;
                            Person p = new Person();
                            B();
                            int y = 2;
                            }

                            function B()
                            {
                            int x = 100;
                            }

                            When "A" is run, 1 is put on the stack for x Stack address: value at that address 0: 1 (x) 1: 0 3: 0 4: 0 5: 0 6: 0 7: 0 8: 0 9: 0 A new "Person" is created so a memory allocation request is made and the OS gives you memory address 10,000, so your Person data is stored at memory address 10,000 on the heap. "p" is actually the number 10,000, ie the place on the heap the Person can be found, so p goes on the stack 0: 1 (x) 1: 10,000 (p) 3: 0 4: 0 5: 0 6: 0 7: 0 8: 0 9: 0 The code has to call "B", however it needs to know where to come back to after B, so the address in memory of the current line of code is stored on the stack. Let's say that is address 1234 0: 1 (x) 1: 10,000 (p) 3: 1234 (ref to code in function A) 4: 0 5: 0 6: 0 7: 0 8: 0 9: 0 Now stack slots 0, 1 and 3 belong to function "A" and we start running code in "B". "B" defines x as 100 so that goes on the stack. 0: 1 (x) 1: 10,000 (p) 3: 1234 (ref to code in function A) 4: 100 (x) 5: 0 6: 0 7: 0 8: 0 9: 0 So stack spots 0-3 belong to A and 4 belongs to B. B is done so we de-allocate everything it put on the stack 0: 1 (x) 1: 10,000 (p) 3: 1234 (ref to code in function A) 4: 0 5: 0 6: 0 7: 0 8: 0 9: 0 To work out where to go back to we read the top-most value of the stack (1234) and resume executing the code at that address. As we have read the value off the stack we also remove it 0: 1 (x) 1: 10,000 (p) 3: 0 4: 0 5: 0 6: 0 7: 0 8: 0 9: 0 This will take us back to the "int y = 2" line in A. Note that not only are we back in the code for A right where we left off, but all of A's variables are on the stack for us to access. So define y = 2 0: 1 (x) 1: 10,000 (p) 3: 2 (y) 4: 0 5: 0 6: 0 7: 0 8: 0 9: 0 A finishes so all of its variables are taken from the stack, and as "p" was a reference, the object at that reference is dispo

                            1 Reply Last reply
                            0
                            • T Tridip Bhattacharjee

                              thanks for the answer but still few lines are not very clear. can u plzz explain it more elaborately in easy way. you said not clear "As you put something on the stack the pointer goes up, you take something off it goes down. If you have, say, an int (10), a string ("ab") and an int (20) on the stack; Stack add 0 - 10 Stack add 1 - a Stack add 2 - b Stack add 3 - 20 Stack add 4 - < stack pointer points here, put something else on and this is where it goes and then wanted to change the string from "ab" to "abc"...where do you store the "c"? There is no space as your second int is on top of it so to increase the string from "ab" to "abc" would require reordering everything above it. So you can only put things on the stack that don't change in size. An int is always going to be 32 bits for example (depending on os and hardware). 2) you said but not clear "Of course value types can end up on the heap too due to things already mentioned, due to boxing, static variables etc,"

                              tbhattacharjee

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              Go back and read OriginalGriff's post agian.

                              Tridip Bhattacharjee wrote:

                              a string ("ab")

                              A String[^] is not a value type, so it does not go on the stack. You really need to go and study some reference guides. Asking questions here is not a good way to learn the internal structure or implementation of a programming language.

                              T 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                Tridip Bhattacharjee wrote:

                                is there any book which i can download its pdf version

                                Do not ask that here again! Most (if not all) PDF copies of books are in violation of copyright: and we do not support that in any way, shape, or form. Bear in mind that nearly all of the 11 million member here are in the business of getting paid to write something it is very easy to copy and download and you might see part of why! You will not get links to anything illegal or immoral here. And if you ask again, there is a good chance you may be banned from the site...permanently. There are several different type of heap in .NET - and it really does get complicated. Seriously, find a book - and buy it - it will be able to go into such things in a lot more detail than we can here.

                                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                                J Offline
                                J Offline
                                jschell
                                wrote on last edited by
                                #15

                                OriginalGriff wrote:

                                Most (if not all) PDF copies of books are in violation of copyright:

                                Eh? There are most definitely books one can download in pdf format for free which do not violate copyright. Given the sum total of all books ever in print then one might suppose that pdfs of all of those, percentage wise, would be violating copyrights. However books published in the past 20 years, either popular ones or those with a low sell rate can be found for free, legally, or for sale, again legally, at various places. All in pdf format. And this includes programming. One might hypothesize that most, percentage wise, of the good books on programming would in fact require a payment. But would be downloadable. And if one expands the definition the percentage is going to go up when one considers content from e-books.

                                T 1 Reply Last reply
                                0
                                • L Lost User

                                  Go back and read OriginalGriff's post agian.

                                  Tridip Bhattacharjee wrote:

                                  a string ("ab")

                                  A String[^] is not a value type, so it does not go on the stack. You really need to go and study some reference guides. Asking questions here is not a good way to learn the internal structure or implementation of a programming language.

                                  T Offline
                                  T Offline
                                  Tridip Bhattacharjee
                                  wrote on last edited by
                                  #16

                                  ok i will try to find out right book for memory management. just tell me i have this code String s="Hello"; when the above line execute then what will happen? where the value "Hello" will be stored? in stack or heap? and where the memory location for "s" will be stored? thanks

                                  tbhattacharjee

                                  L 2 Replies Last reply
                                  0
                                  • J jschell

                                    OriginalGriff wrote:

                                    Most (if not all) PDF copies of books are in violation of copyright:

                                    Eh? There are most definitely books one can download in pdf format for free which do not violate copyright. Given the sum total of all books ever in print then one might suppose that pdfs of all of those, percentage wise, would be violating copyrights. However books published in the past 20 years, either popular ones or those with a low sell rate can be found for free, legally, or for sale, again legally, at various places. All in pdf format. And this includes programming. One might hypothesize that most, percentage wise, of the good books on programming would in fact require a payment. But would be downloadable. And if one expands the definition the percentage is going to go up when one considers content from e-books.

                                    T Offline
                                    T Offline
                                    Tridip Bhattacharjee
                                    wrote on last edited by
                                    #17

                                    just tell me a books name which discuss in details how memory is allocated when we run our program. thanks

                                    tbhattacharjee

                                    P 1 Reply Last reply
                                    0
                                    • T Tridip Bhattacharjee

                                      ok i will try to find out right book for memory management. just tell me i have this code String s="Hello"; when the above line execute then what will happen? where the value "Hello" will be stored? in stack or heap? and where the memory location for "s" will be stored? thanks

                                      tbhattacharjee

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #18

                                      Did you actually read the explanation that OriginalGriff posted, or the previous message that I wrote? A string is not a value type so its data will be stored on the heap. Its reference pointer s will be on the stack. If you want a simple explanation then read Charles Petzold's excellent .NET Book Zero[^].

                                      T S 2 Replies Last reply
                                      0
                                      • L Lost User

                                        Did you actually read the explanation that OriginalGriff posted, or the previous message that I wrote? A string is not a value type so its data will be stored on the heap. Its reference pointer s will be on the stack. If you want a simple explanation then read Charles Petzold's excellent .NET Book Zero[^].

                                        T Offline
                                        T Offline
                                        Tridip Bhattacharjee
                                        wrote on last edited by
                                        #19

                                        i saw the index of this book .NET Book Zero but unfortunately i have not seen any memory management(heap and stack) related chapter there. are you sure that this books will discuss about memory management(heap and stack)? need suggestion.

                                        tbhattacharjee

                                        1 Reply Last reply
                                        0
                                        • L Lost User

                                          There's a legal PDF Book linked here: http://www.codeproject.com/Messages/4977275/free-introductory-book-on-Csharp-programming-from.aspx[^] I don't know if it covers memory management but it probably contains other useful stuff.

                                          Tridip Bhattacharjee wrote:

                                          share knowledge if you know what is high frequency heap?

                                          The CLR (Common Language Runtime) uses the high frequency heap to store frequently used data required to run a .Net program, e.g. method tables. It's nothing that's particularly important to know for writing a .Net application.

                                          T Offline
                                          T Offline
                                          Tridip Bhattacharjee
                                          wrote on last edited by
                                          #20

                                          thanks for your time and explanation. would you mind to discuss briefly what is high frequency heap and how it is different than a normal heap? i hard the static class related info stored in high frequency heap but not sure is it true or not. why static class related info stored in heap? looking for your guidance.

                                          tbhattacharjee

                                          P 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups