Memory management clarification



Hi,

Trying to learn a bit of Ada I came across a statement that memory allocated from the pool will be implicitly reclaimed when the acces variable used to reference it goes out of scope.
That's nice, but I would like to learn a bit more about the exact mechanics of this and about the guarantees that it can provide.


Let's say that there is some MyType definition and this:

type MyTypeRef is access MyType;

1.

declare
    X : MyTypeRef;
begin
   loop
      X := new MyType;
   end loop;  -- infinite loop just for the sake of discussion
end;

Note that X does not goes out of scope when the loop is executing.
Will the memory be reclaimed? When? What can be said about the memory consumption of such program? Is it bounded and guaranteed? Is it necessarily larger than without the loop (just single allocation)?


2.

loop
   declare
      X : MyTypeRef;
   begin
      X := new MyType;
   end;
end loop;

What now? Is this any different from the memory management point of view?

3.

declare
   X : MyTypeRef;
begin
   X := new MyType;
   X := new MyType;
   X := new MyType;
   X := new MyType;
   -- ...
end;

When is the memory reclaimed for each allocated object? At each subsequent assignment? Or maybe at the end of the block? Or even "sometime later"? Or maybe all subsequent assignments are eliminated by compiler?

4.

Is it possible to associate some function with object allocated by new, which would be called at the time (or maybe after) the object is reclaimed?
Yes, I'm asking about destructors or finalizers.


5.

Is it possible to "overload" new for MyType so that the X := new MyType; statement will do whatever *I* want it to do, including actual memory allocation? If yes, is it possible to hook on memory reclamation as well?

6.

What about reference cycles between dynamically allocated objects?

declare
   type ListNode;
   type ListNodeRef is access ListNode;
   type ListNode is record
      SomeData : Integer;
      Other : ListNodeRef;
   end record;
   First, Second : ListNodeRef;
begin
   First := new ListNode;
   First.all.SomeData := 7;
   Second := new ListNode;
   Second.all.SomeData := 8;

   First.all.Other := Second;
   Second.all.Other := First; -- cycle
end;

Will the memory be reclaimed?


Regards,


-- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/ .



Relevant Pages

  • Re: How to release heap memory that is marked as free
    ... As I said, fragmentation is a very serious problem, and one of the most serious problems ... my allocator was accused of using massive amounts of memory. ... I'm going to have to re-think the memory allocation that I'm ... process's 'working set'. ...
    (microsoft.public.vc.mfc)
  • Re: [PATCH 00/28] Swap over NFS -v16
    ... memory they can consume. ... So we need the extra (skb) ... included in the reserve? ... if the allocation had to dip into emergency reserves, ...
    (Linux-Kernel)
  • Re: Memory leak with CAsyncSocket::Create
    ... read my essay on how storage allocators work. ... Create method is consuming system memory that is not released back to ... The memory consumption is either shown as "Mem Usage" on the Task ... many levels of allocation going ...
    (microsoft.public.vc.mfc)
  • Re: OT: C++ overloading operators
    ... dynamic allocation, no matter how many "clever tricks" are used... ... though there's enough memory in the system, ... all these "flexible data types" map into CPU command ... The computing environment is completely ...
    (comp.dsp)
  • Re: HLA Lib
    ... All memory allocation is freed up when the process quits. ... reduce need to resize blocks for 98% string operations. ... HLA strings already consume. ...
    (alt.lang.asm)