9

This is a subset of a previous question.

As an exercise I am writing a memory manager - that is, the code which implements malloc, realloc and free (or new and delete.) The RTL for my language, Delphi, allows the RTL's memory manager to be replaced easily. For those of you using C++, this is similar to, but lower-level than, overriding new and delete (it hooks into the RTL itself rather than being a language feature.)

I'm looking for resources about high-quality approaches others have taken to the same problem and am trying to find out what algorithms other major compiler vendors use. While Delphi's is well documented, I cannot find any information about the implementations used by MS VC++, .Net, or Objective C. These vendors do not seem (?) to allow their RTL to be hooked into like Delphi does. All documentation seems to be higher-level, such as NSAutoReleasePool to pick a random example - far too high-level for this question.

What memory management algorithms do major vendors (Microsoft VC++ and .Net, and Apple Objective C) use in their runtime libraries?

An example of a great answer would be a document describing the memory manager implementation, such as this one, or a link to a published paper. An example of a useful answer would be the algorithm, 'The VC++ runtime uses the Hoard allocator'.

Community
  • 1
  • 1
David
  • 13,360
  • 7
  • 66
  • 130
  • Wow... but memory management is so complexe beast - it is prevasive on many-many-many levels. For example with Delphi: FastMM4 implements three algorithms depending on block size. But on top of it there is RTL, which implements copy-on-write + refcounting memory management for dyn-arrays and strings. Then there is TComponent and TObjectList contsiner classes implementing Owner-Items algorythm then there is TInterfaceObject comeing to ref-counting again... And all those ARE memory management algorythms. And all those algorythms together implement STRATEGY of given HMMgr+Language+RTL – Arioch 'The Apr 30 '13 at 10:10
  • 2
    +1, this is a quite interesting question, if only for academical purposes :-) – Guillem Vicens Apr 30 '13 at 10:15
  • 1
    With GC-based languages like .Net, JVM and again ObjC, there would be different expectations and different algorithms in memory manager, depending on which information can (or can not) Memory Manager reliably get from the language. Just read the contents of http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) – Arioch 'The Apr 30 '13 at 10:17
  • 2
    While this is a well-asked question, it's entirely too broad in scope to be answered here, and it would be virtually impossible to post a single answer that addresses everything you're asking. Sorry, but I have to vote to close this as not a real question because of that reason. – Ken White Apr 30 '13 at 10:58
  • Why don't you simply read the source code for each of these runtimes? – David Heffernan Apr 30 '13 at 12:35
  • @DavidHeffernan, the VC++, .Net and (I think) Objective-C runtimes are closed source. – David Apr 30 '13 at 12:48
  • I've got the source for MSVCRT sitting right here. I believe that ObjC runtime source is also available (http://www.opensource.apple.com/source/objc4/). And there's the reference implementation of the .net runtime. – David Heffernan Apr 30 '13 at 12:50
  • 1
    @KenWhite: why? Why can't someone reply 'VC++: uses Foo (link). .Net: uses Bar (link). ...'? At the very least, someone may know one of the three, and being a collaborative site other answers can be added to the reply. – David Apr 30 '13 at 12:51
  • @DavidM You'll want to ask a more focused question. As a question purely about VC++. Then ask one about another environment. Those tight focused questions are on topic. – David Heffernan Apr 30 '13 at 12:52
  • @DavidHeffernan: you do? Could you provide links to these, please? I didn't know the VC runtime was available, or that there was an open-source reference .Net implementation. (Is that the same as the normal implementation, though? A memory manager is the sort of thing that can have 'correct' and 'optimised' versions.) – David Apr 30 '13 at 12:52
  • MSVCRT source comes with VS. I added a link for ObjC. As for .net you are right, the reference implementation likely differs from the actual product. But for .net it's greatly complicated by GC. Now, GC stands on top of a heap. It's a huge topic. There is no one algorithm. I do believe you are asking too much. Concentrate on one environment. Pick the easiest! – David Heffernan Apr 30 '13 at 12:54
  • @DavidM: A link collection becomes what's known as a shopping list question (a list of links to off-site places), and shopping-list questions are not appropriate here. (See this [meta post](http://meta.stackexchange.com/q/158809/172661) for some of the reasons why that is the case.) – Ken White Apr 30 '13 at 12:59
  • 3
    Interesting question, I feel sorry when I see theoretical questions being closed for the bogus reasons. – OnTheFly Apr 30 '13 at 19:17
  • @DavidHeffernan Thanks for the link, looks like for Obj-C at least I can examine the source and answer that part with some time. Re MSVC, I don't own a copy, so probably can't examine the source. RE .Net - yes, the GC will complicate it, although under the hood there will still be an algorithm for partitioning memory, allocating from places, handling frees, etc... I don't want to 'pick the easiest', because I am interested in learning based not on ease but on what actually exists. I'm very interested in the actual implementations each vendor uses. – David May 02 '13 at 07:28
  • You "pick the easiest" to ask a single question. Asking a question about massively complex implementation details of 4 distinct platforms is not a good question for here. That's why it got closed. You just wait and see how many really good answers you get that cover all 4 platforms. – David Heffernan May 02 '13 at 09:36

1 Answers1

1

Objective-C uses automatic reference counting (ARC). It's enabled as of iOS5. Apple has US Patent 20030196063 "Transparent local and distributed memory management system".

Java uses a sophisticated garbage collection scheme which has evolved over the years. See "Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine" http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html

Richard Wеrеzaк
  • 1,551
  • 1
  • 13
  • 17
  • You're describing object lifetime management here, not memory allocation. Memory allocation is lower-level - it is the code that runs to allocate the memory when an object is created, for example. – David Oct 14 '15 at 20:36