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 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
    #21

    And in response to the message you sent and then deleted: unless you actually read the book you are not likely to learn anything.

    1 Reply Last reply
    0
    • T Tridip Bhattacharjee

      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 Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #22

      Seriously, we aren't here to do your thinking and research for you. I take it you have an interview soon. You cannot possibly cram in enough to bluff your way through based on random questions. You have been posting questions here long enough to have figured out that you need to learn the basics first. Richard has given you a great book to get started with. Beyond this, if you need to know things like the high frequency heap, then you need to learn how to use google and apply some critical thinking. Learning is your responsibility.

      1 Reply Last reply
      0
      • T Tridip Bhattacharjee

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

        tbhattacharjee

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #23

        Have a search through the MSDN magazine back issues. They have a lot of great information there.

        T 1 Reply Last reply
        0
        • P Pete OHanlon

          Have a search through the MSDN magazine back issues. They have a lot of great information there.

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

          if possible give me some good link from MSDN magazine which relevant for my question.

          tbhattacharjee

          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:

            S Offline
            S Offline
            Santosh K Tripathi
            wrote on last edited by
            #25

            5+ from me also. :)

            1 Reply 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[^].

              S Offline
              S Offline
              Santosh K Tripathi
              wrote on last edited by
              #26

              Nice Book.

              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