comp.lang.ada
 help / color / mirror / Atom feed
* Problems freeing memory with Free
@ 2001-04-29 20:29 Preben Randhol
  2001-04-29 22:16 ` Florian Weimer
  0 siblings, 1 reply; 6+ messages in thread
From: Preben Randhol @ 2001-04-29 20:29 UTC (permalink / raw)


Hi

I need some help because now I have been looking at this problem for 3
days and I cannot find the solution.

I have made a double linked list which seems to preform correctly using
a debugger. The problem however is that if I delete all the Nodes except
the last and at the same time look at the programs memory usage with
"top" under Linux, it reports the same amount of memory all the time. If
it then delete the last node too, the memory suddenly drops down to the
expected value (that is same as before the nodes were made). 

If I also delete all the nodes starting from the second node until the
last, but not the first, the memory drops. I cannot figure out the
error, so if somebody could be so kind to look at it and help me spot
the error I would be very glad. I'm probably blind to the error after
staring at the source for so long.

The source can be downloaded and viewed here:

   http://www.pvv.org/~randhol/Ada95/Double/

Thanks in advance!

PS: The list is for an educational program I'm making and will release
under GPL as soon as I can get the list to work properly...

-- 
Preben Randhol ------------------- http://www.pvv.org/~randhol/ --
                 �For me, Ada95 puts back the joy in programming.�



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problems freeing memory with Free
  2001-04-29 20:29 Problems freeing memory with Free Preben Randhol
@ 2001-04-29 22:16 ` Florian Weimer
  2001-04-30  7:35   ` Preben Randhol
  0 siblings, 1 reply; 6+ messages in thread
From: Florian Weimer @ 2001-04-29 22:16 UTC (permalink / raw)


randhol+abuse@pvv.org (Preben Randhol) writes:

> I have made a double linked list which seems to preform correctly using
> a debugger. The problem however is that if I delete all the Nodes except
> the last and at the same time look at the programs memory usage with
> "top" under Linux, it reports the same amount of memory all the time. If
> it then delete the last node too, the memory suddenly drops down to the
> expected value (that is same as before the nodes were made). 

There's no error.  A typical implementation of a storage pool
retrieves memory in large chunks from the operating systems and
allocates several objects in each chunk.  When the objects are freed,
a chunk can only be returned to the operating system if all objects
which had been allocated in it before have been freed (unless you've
got a compacting garbage collector, but that's another story).

I think we discussed this phenomenon just a few months ago.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problems freeing memory with Free
  2001-04-29 22:16 ` Florian Weimer
@ 2001-04-30  7:35   ` Preben Randhol
  2001-04-30  8:01     ` Preben Randhol
                       ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Preben Randhol @ 2001-04-30  7:35 UTC (permalink / raw)


On 30 Apr 2001 00:16:09 +0200, Florian Weimer wrote:
> randhol+abuse@pvv.org (Preben Randhol) writes:
> 
> There's no error.  A typical implementation of a storage pool
> retrieves memory in large chunks from the operating systems and
> allocates several objects in each chunk.  When the objects are freed,
> a chunk can only be returned to the operating system if all objects
> which had been allocated in it before have been freed (unless you've
> got a compacting garbage collector, but that's another story).

I see. So that explaines why if I delete X of the last nodes it frees
the memory, while if I free the X first or X in the middle, it may be
50_000 nodes or more the size has no influence apparently, it is not
freed as the OS thinks I still need the memory because the last node is
still using the memory at a much higher position?

The reason why I thought there was an error was that if I excange the
Data_Type with a record I have:

type Wordgroup_type is 
   record
      Word        :  Unbounded_String := To_Unbounded_String("");
      Explanation :  Unbounded_String := To_Unbounded_String("");
   end record;

and use this in the same way as the integer in the testing program I put
up on the web, then the program start apparently to leak memory if I
delete X nodes and then add X nodes again. The size of the memory goes
perhaps from:

   8Mb -> 8Mb (after freeing I don't see a reduction in memory size) ->
   12Mb (some increase, but not as high as 16Mb) or something like this,
   this does not happen if I use Bounded_String. The problem with
   Bounded_String however is that the memory consumption is 27Mb, well
   not in a real situation, but for my testing now :-)

I don't use New Wordgroup_type, nor an access type, do I need to to that
and set the generic Data_Type to Worgroup_Access_type instead and then
use Free on the Wordgroup as well?

> I think we discussed this phenomenon just a few months ago.

Ah, I'll dig back in my archive...

Thanks!

-- 
Preben Randhol ------------------- http://www.pvv.org/~randhol/ --
                 �For me, Ada95 puts back the joy in programming.�



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problems freeing memory with Free
  2001-04-30  7:35   ` Preben Randhol
@ 2001-04-30  8:01     ` Preben Randhol
  2001-04-30 13:23     ` Larry Kilgallen
  2001-05-01 12:30     ` Florian Weimer
  2 siblings, 0 replies; 6+ messages in thread
From: Preben Randhol @ 2001-04-30  8:01 UTC (permalink / raw)


On Mon, 30 Apr 2001 07:35:48 +0000 (UTC), Preben Randhol wrote:
> and use this in the same way as the integer in the testing program I put
> up on the web, then the program start apparently to leak memory if I
> delete X nodes and then add X nodes again. The size of the memory goes
> perhaps from:
> 
>    8Mb -> 8Mb (after freeing I don't see a reduction in memory size) ->
>    12Mb (some increase, but not as high as 16Mb) or something like this,
>    this does not happen if I use Bounded_String. The problem with
>    Bounded_String however is that the memory consumption is 27Mb, well
>    not in a real situation, but for my testing now :-)

Correction:
If I use Bounded_String then it doesn't matter if I delete the first or
the Last Nodes, the memory pool is the same size, but if I reread the
nodes that I deleted then the memory does not increase. If I delete
all the nodes, the whole memory chunck is released.  

-- 
Preben Randhol ------------------- http://www.pvv.org/~randhol/ --
                 �For me, Ada95 puts back the joy in programming.�



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problems freeing memory with Free
  2001-04-30  7:35   ` Preben Randhol
  2001-04-30  8:01     ` Preben Randhol
@ 2001-04-30 13:23     ` Larry Kilgallen
  2001-05-01 12:30     ` Florian Weimer
  2 siblings, 0 replies; 6+ messages in thread
From: Larry Kilgallen @ 2001-04-30 13:23 UTC (permalink / raw)


In article <slrn9eq5ht.13j.randhol+abuse@kiuk0156.chembio.ntnu.no>, randhol+abuse@pvv.org (Preben Randhol) writes:
> On 30 Apr 2001 00:16:09 +0200, Florian Weimer wrote:
>> randhol+abuse@pvv.org (Preben Randhol) writes:
>> 
>> There's no error.  A typical implementation of a storage pool
>> retrieves memory in large chunks from the operating systems and
>> allocates several objects in each chunk.  When the objects are freed,
>> a chunk can only be returned to the operating system if all objects
>> which had been allocated in it before have been freed (unless you've
>> got a compacting garbage collector, but that's another story).
> 
> I see. So that explaines why if I delete X of the last nodes it frees
> the memory, while if I free the X first or X in the middle, it may be
> 50_000 nodes or more the size has no influence apparently, it is not
> freed as the OS thinks I still need the memory because the last node is
> still using the memory at a much higher position?

But that is just _typical_ of the way implementations are most often
built.  There is no guarantee regarding when or if memory will be
returned to an operating system by all Ada implementations.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problems freeing memory with Free
  2001-04-30  7:35   ` Preben Randhol
  2001-04-30  8:01     ` Preben Randhol
  2001-04-30 13:23     ` Larry Kilgallen
@ 2001-05-01 12:30     ` Florian Weimer
  2 siblings, 0 replies; 6+ messages in thread
From: Florian Weimer @ 2001-05-01 12:30 UTC (permalink / raw)


randhol+abuse@pvv.org (Preben Randhol) writes:

> I see. So that explaines why if I delete X of the last nodes it frees
> the memory, while if I free the X first or X in the middle, it may be
> 50_000 nodes or more the size has no influence apparently, it is not
> freed as the OS thinks I still need the memory because the last node is
> still using the memory at a much higher position?

This is the situation with more conventional allocator
implementations, yes.

> The reason why I thought there was an error was that if I excange the
> Data_Type with a record I have:
> 
> type Wordgroup_type is 
>    record
>       Word        :  Unbounded_String := To_Unbounded_String("");
>       Explanation :  Unbounded_String := To_Unbounded_String("");
>    end record;
> 
> and use this in the same way as the integer in the testing program I put
> up on the web, then the program start apparently to leak memory if I
> delete X nodes and then add X nodes again.

Strange.  However, if you create an Unbounded_String object, at least
GNAT invokes an allocated internally, and heap fragmentation might
explain the results you're observing.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2001-05-01 12:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-29 20:29 Problems freeing memory with Free Preben Randhol
2001-04-29 22:16 ` Florian Weimer
2001-04-30  7:35   ` Preben Randhol
2001-04-30  8:01     ` Preben Randhol
2001-04-30 13:23     ` Larry Kilgallen
2001-05-01 12:30     ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox