Copyright 2004 by M. Uli Kusterer Tue, 30 Dec 1969 07:58:58 GMT Comments on article howmemorymanagementworks at Zathras.de http://www.zathras.de/angelweb/howmemorymanagementworks.htm howmemorymanagementworks Comments witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) en-us Comment 27 by Uli Kusterer http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment27 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment27 Uli Kusterer writes:
@sudesh: Of course memory management in C is possible, why would it not? You use malloc(), free(), realloc() and the * and & operators.
Comment 26 by sudesh kumar http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment26 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment26 Memory management is possible in C lang?
Give the ans in true or false.plzzzzzzz
If yes, then how.
Comment 25 by Uli Kusterer http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment25 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment25 Uli Kusterer writes:
@nico: Thank you :-) There are also "runtime time" and "I'm not recycling, I collect garbage" postings in my blog that may clear up some additional things that may interest you in Java. I.e. the latter has a short explanation on how a garbage collector works, and the first explains how objects are laid out in memory in a typical OO programming language and also includes sample code.

Some of this goes into the nitty gritty details, though.
Comment 24 by nico http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment24 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment24 Wow, I google'd 'memory management' and 'stack heap' and read about 5 articles before yours. Very clear, useful, and to the point. Thanks so much!!! BTW, although I'm interested in C, I'm currently studying for a java certification and just wanted a high level understanding of the stack,heap, and memory management as much of this is automated in java. However, I was still able to read this article, including the pointers section, and get exactly what I need. Much praise!
Comment 23 by Uli Kusterer http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment23 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment23 Uli Kusterer writes:
@Jack: Thanks. Have you checked out "Masters of the Void"? Chapter 7 concerns itself with data structures, is there anything more you'd like to know than that?
Comment 22 by Jack http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment22 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment22 Hai,
Really Nice article.Understood the basic concept of memory management in our
computer systems.Also pointer concepts.superp..!Can you please provide another detail article in pointers and data structures.I am very much interested in learning data structures.Thankz...!!
Comment 21 by hachu http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment21 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment21 Kim and Uli, here's the reasons why memory is sold as a factor of 2.

In hardware, everything is in binary because each wire can only be on or off. To ask for a location, we need to specify the location by turning individual address lines (also called the address bus) on or off. (hence ones and zeros.)

Say for example we have a 16 bit addressing bus. This means we can specify a memory location from 0 to 2^16-1 because those are the numbers that be expressed with 16 bits. Hence 64k.

This also means, when making a memory chip, the most space-efficient design is also a grid of these 16 bits.

In the future, if we can make the memory chip denser, we add one more address line so now it's 17 bits. Therefore 128k. Adding one more past that, you get 18 bits. 256k. Eventually these keep getting bigger and we end up with the 512MB and 1024MB ones we have now.

Because hardware design is quite difficult, memory controllers will have limitations on what configurations of memory can be supported. But memory management units, in cooperation with the virtual memory support of the OS, make it possible to take the little bits and chunks of non-contiguous memory and make it look like one continuous piece to us programmers.

Another interesting fact. Because chips often don't come out perfect when manufacturing, the factory can take a chip, test it, and then disable an address line to permanently shut down one side of the chip. Then it's used and marked as if it were manufactured as a smaller chip. You might consider it quite wasteful to disable half a chip because one byte is broken, especially one of the big ones, but consider that the effort to design a way to support non-multiple-of-2 memory modules and memory controller and the OS support for this is so much more expensive. Taking one line and disabling it is the least complex, cheapest, and most reliable way to go.
Comment 20 by Uli Kusterer http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment20 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment20 Uli Kusterer writes:
@ Nikhil: I don't know what an 'u plz' is, but if you're looking for info on void* and the likes, have a look at my Masters of the Void tutorial (http://www.zathras.de/masters-of-the-void.htm), particularly chapters 5 and 6 delve a little deeper into the C side of things. If that still isn't enough, I'll add something when I next revise the tutorial, but I don't know when that will be.
Comment 19 by Nikhil http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment19 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment19 hey, great article.. can u plz 'point' to toward further material on memory mnagement? i am also a little confused about the type casting in c, especially the (void *).. i need something that will elucidate the topics mentioned above.. Thanx!
Comment 18 by ginni http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment18 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment18 good .. bt short ..
still .. every thin explained is in a gud n clear way !!
Comment 17 by F.Daouk http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment17 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment17 Excellent article.
This is the most useful information I've read about the stack and the heap.
Thanks
Comment 16 by Sunyana http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment16 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment16 Thanks a lot... this article was very much helpful to me.. it make me very clear on heap and stack concepts...

Sunyana
Comment 15 by Uli Kusterer http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment15 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment15 Uli Kusterer writes:
Well, I think "keeping the stack usage down" is a very good reason. Consider that you allocate some new stack space each time you call a function. That happens millions of times each second, so you'd better not leak even 10 bytes.

That said, the end of the stack is also used to locate some stack data (like the return address to jump back to after a function has been called, any registers you save...). This can't be done with absolute addresses hard-coded in the source, because a function could be called concurrently from different threads, or recursively etc., and in those cases you'd e.g. need two different return addresses. One could probably reserve a register for such a purpose, but really this approach is much easier.

In short: Yes, and there are other good reasons. :-)
Comment 14 by Magster http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment14 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment14 Really clear and unjargonised. Can I ask a question? When methods/subroutines/whatever are entered the variables declared there are pushed onto stack and popped off when the "function" is left. Is this simply to keep stack usage down or is there another reason? Thanks.
Comment 13 by Uli Kusterer http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment13 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment13 Uli Kusterer writes:
@Byomkesh: Thanks for the praise. And what diagram should I put where? And why explicitly? Can you maybe re-phrase that, I think this sentence is missing some part...?
Comment 12 by Byomkesh Das http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment12 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment12 Byomkesh Das writes:
Fantastic written , I understood clearly, Thanks. One thing Put some diagram explictely
Comment 11 by Uli Kusterer http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment11 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment11 Uli Kusterer writes:
Kim, I don't really know. It may have to do with the fact that, each time you add a digit to a binary number, its possible values double (so they might just double the size of each chip to make best use of the additional needed bit in the address), or it may be a physical reason, related to the manufacturing process of memory chips.
Comment 10 by Uli Kusterer http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment10 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment10 Uli Kusterer writes:
rakesh, I have no idea what you want from me, sorry. Maybe someone else understands his question?
Comment 9 by rakesh http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment9 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment9 1)how mangge convential memory,higer memory .etc. how it is work .pls

2)DDR RAM called DDR SDRAM?
Comment 8 by Kim http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment8 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment8 why is memory always sold as a factor of 2? EX: 128mb, 256mb, 512mb, etc.
Comment 7 by Ricky http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment7 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment7 Ricky writes:
Nice article Uli.

I thought I would post a link to Peter Hosey's guide on C pointers as well:

http://boredzo.org/pointers/

This might be good additional reference material.
Comment 6 by Terry Heaford http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment6 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment6 Have read this a few times and found it really useful. Thank you.

I would find more articles regarding Cocoa memory management and pointers regarding debugging (no pun intended) really useful for me and I'm sure others learning to programme objective-c with Cocoa would similarly benefit.
Comment 5 by Sid http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment5 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment5 Thank you for your clear description of an often misunderstood area of computer engineering!
Comment 4 by mozart http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment4 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment4 My head hurts after reading that.
I Don't even now my 8 times table.
Comment 3 by Uli Kusterer http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment3 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment3 Uli Kusterer writes:
Once I've finished Masters of the Void to cover the C basics, I'm thinking about going into more detail on Cocoa memory management (and especially on tracking down and avoiding memory bugs). Is that what you're looking for? "manage memory in practice" is a large topic, so if you're wondering about something in particular, it may be best to let me know in more detail.
Comment 2 by Ludwig http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment2 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment2 Ludwig writes:
I'd like to hear something about you can manage memory in practice! For example in Obj-C.
Comment 1 by Levi http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment1 http://www.zathras.de/angelweb/howmemorymanagementworks.htm#comment1 Hi,
Your article was really helpful!
Thanks!