comp.lang.ada
 help / color / mirror / Atom feed
* How do I free unbounded strings?
@ 2001-03-05 17:03 Erik Sigra
  2001-03-05 20:14 ` Florian Weimer
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Erik Sigra @ 2001-03-05 17:03 UTC (permalink / raw)
  To: comp.lang.ada

Hi,

This is my first post to this mailinglist/newsgroup. I use Gnat 3.13p 
downloaded from <http://www.gnuada.org/rpms313p.html>. In the rationale at 
<http://www.adapower.com/rationale/rat95-p3-a.html> i read:

  For unbounded strings the ":=" operation does the necessary storage
  management through Adjust and Finalize operations to allocate needed space
  for the new value of the target and to reclaim the space previously
  occupied by the object.

Then I tried this simple program:

  with Ada.Strings.Unbounded;         use Ada.Strings.Unbounded;
  with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;
  with Ada.Text_IO;                   use Ada.Text_IO;

  procedure Prov is
     Unbounded_String_1 : Unbounded_String;
  begin
     Put ("Enter an unbounded string: ");
     Unbounded_String_1 := Get_Line; --  First Pause
     Put ("Check how much memory the program uses and press return: ");
     while not End_Of_Line loop null; -- Second Pause
     end loop;
     Skip_Line;
     Unbounded_String_1 := Null_Unbounded_String;
     Put ("The unbounded string has been freed. Check how much memory the"
          & " program uses and press return: ");
     while not End_Of_Line loop null; -- Third Pause
     end loop;
     Skip_Line;
  end Prov;

At First Pause the program always uses (880, 880, 776) memory. At Second 
Pause the program uses at least (888, 888, 780) memory. If I enter a long 
string this can be for example (900, 900, 784). At Third Pause the program 
uses exactly as much memory as at Second Pause.

The memory values are labelled ("Virtual image size of process in Kbytes", 
"Resitent set size; Kbytes of program in memory", "Shared memory in Kbytes").

So it appears to me as if freeing doesn't work as the rationale says. Could 
someone please help me with this?




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

* Re: How do I free unbounded strings?
  2001-03-05 17:03 How do I free unbounded strings? Erik Sigra
@ 2001-03-05 20:14 ` Florian Weimer
  2001-03-05 21:10   ` Erik Sigra
  2001-03-05 20:48 ` Jeffrey Carter
  2001-03-06  2:52 ` Mark Biggar
  2 siblings, 1 reply; 8+ messages in thread
From: Florian Weimer @ 2001-03-05 20:14 UTC (permalink / raw)


Erik Sigra <sigra@home.se> writes:

> At First Pause the program always uses (880, 880, 776) memory. At Second 
> Pause the program uses at least (888, 888, 780) memory. If I enter a long 
> string this can be for example (900, 900, 784). At Third Pause the program 
> uses exactly as much memory as at Second Pause.
> 
> The memory values are labelled ("Virtual image size of process in Kbytes", 
> "Resitent set size; Kbytes of program in memory", "Shared memory in Kbytes").
> 
> So it appears to me as if freeing doesn't work as the rationale says. Could 
> someone please help me with this?

Well, you can't expect that a program will use operating system
interfaces to allocate individual, small strings.  Usually, memory is
obtained from the operating systems in large chunks which are filled
with smaller objects, and only large objects are directly allocated
via the operating system.

GNAT uses the C malloc()/free() memory management interface, which
might or might not return deallocated memory to the operating system
(of course, a large chunk can only be given back if all the objects in
it have been deallocated).  GNU/Linux systems feature a
malloc()/free() implementation which does return completely unused
chunks.



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

* Re: How do I free unbounded strings?
  2001-03-05 17:03 How do I free unbounded strings? Erik Sigra
  2001-03-05 20:14 ` Florian Weimer
@ 2001-03-05 20:48 ` Jeffrey Carter
  2001-03-05 20:55   ` Erik Sigra
  2001-03-06  2:52 ` Mark Biggar
  2 siblings, 1 reply; 8+ messages in thread
From: Jeffrey Carter @ 2001-03-05 20:48 UTC (permalink / raw)


Erik Sigra wrote:
> 
>      while not End_Of_Line loop null; -- Second Pause
>      end loop;
>      Skip_Line;
...
>      while not End_Of_Line loop null; -- Third Pause
>      end loop;
>      Skip_Line;

Why do you have these null loops in your program? Skip_Line will block
the task until the user enters a line terminator.

-- 
Jeff Carter
"We burst our pimples at you."
Monty Python & the Holy Grail



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

* Re: How do I free unbounded strings?
  2001-03-05 20:48 ` Jeffrey Carter
@ 2001-03-05 20:55   ` Erik Sigra
  0 siblings, 0 replies; 8+ messages in thread
From: Erik Sigra @ 2001-03-05 20:55 UTC (permalink / raw)
  To: comp.lang.ada, Jeffrey Carter

m�ndagen den  5 mars 2001 21:48 skrev Jeffrey Carter:
> Erik Sigra wrote:
> >      while not End_Of_Line loop null; -- Second Pause
> >      end loop;
> >      Skip_Line;
>
> ...
>
> >      while not End_Of_Line loop null; -- Third Pause
> >      end loop;
> >      Skip_Line;
>
> Why do you have these null loops in your program? Skip_Line will block
> the task until the user enters a line terminator.

Thanks for the remark. I haven't used Ada for many months now so I'm a little 
rusty. I saw that the probram used 0% CPU in the null loops and my desktop 
environment was as resoposive as ever so I thought it was acceptable.




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

* Re: How do I free unbounded strings?
  2001-03-05 20:14 ` Florian Weimer
@ 2001-03-05 21:10   ` Erik Sigra
  2001-03-06  1:49     ` Robert A Duff
  0 siblings, 1 reply; 8+ messages in thread
From: Erik Sigra @ 2001-03-05 21:10 UTC (permalink / raw)
  To: comp.lang.ada

m�ndagen den  5 mars 2001 21:14 skrev Florian Weimer:
> Well, you can't expect that a program will use operating system
> interfaces to allocate individual, small strings.  Usually, memory is
> obtained from the operating systems in large chunks which are filled
> with smaller objects, and only large objects are directly allocated
> via the operating system.
>
> GNAT uses the C malloc()/free() memory management interface, which
> might or might not return deallocated memory to the operating system
> (of course, a large chunk can only be given back if all the objects in
> it have been deallocated).  GNU/Linux systems feature a
> malloc()/free() implementation which does return completely unused
> chunks.

Thanks. Then I will assume that things are allright. Now to a related 
question. Consider this situation:

  type Post_Node;
  type Post_List is access Post_Node;
  type Post_Node is record
     Name : Unbounded_string;
     Next : Post_List;
  end record;

I allocate a Post_Node and store an unbounded string in it. Then I deallocate 
it with an instance of Ada.Unchecked_Deallocation. Is the memory that the 
unbounded string used lost?

If so, does it help to assign Null_Unbounded_String to it before freeing the 
record?

Or is there something else I have to do, some pitfalls?

If the answer can be found in some document, can you provide a link?




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

* Re: How do I free unbounded strings?
  2001-03-05 21:10   ` Erik Sigra
@ 2001-03-06  1:49     ` Robert A Duff
  0 siblings, 0 replies; 8+ messages in thread
From: Robert A Duff @ 2001-03-06  1:49 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 695 bytes --]

Erik Sigra <sigra@home.se> writes:

> m�ndagen den  5 mars 2001 21:14 skrev Florian Weimer:

>   type Post_Node;
>   type Post_List is access Post_Node;
>   type Post_Node is record
>      Name : Unbounded_string;
>      Next : Post_List;
>   end record;
> 
> I allocate a Post_Node and store an unbounded string in it. Then I deallocate 
> it with an instance of Ada.Unchecked_Deallocation. Is the memory that the 
> unbounded string used lost?

No.  It is reclaimed.

As Erik Sigra implied, "reclaimed" does not mean "given back to the
operating system, so your OS tools notice".  It just means that that
memory can be reused for other Unbounded_Strings, or perhaps something
else.

- Bob



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

* Re: How do I free unbounded strings?
  2001-03-05 17:03 How do I free unbounded strings? Erik Sigra
  2001-03-05 20:14 ` Florian Weimer
  2001-03-05 20:48 ` Jeffrey Carter
@ 2001-03-06  2:52 ` Mark Biggar
  2001-03-06 14:10   ` Larry Kilgallen
  2 siblings, 1 reply; 8+ messages in thread
From: Mark Biggar @ 2001-03-06  2:52 UTC (permalink / raw)


Erik Sigra wrote:
>   For unbounded strings the ":=" operation does the necessary storage
>   management through Adjust and Finalize operations to allocate needed space
>   for the new value of the target and to reclaim the space previously
>   occupied by the object.
> ...example program deleted. 
> So it appears to me as if freeing doesn't work as the rationale says. Could
> someone please help me with this?

Sure, first you must understand that there is a difference between
freeing memory for a data structure in a program and a program
actually giving back memory to the operating system.  On most operating
systems when a program needs more memory it asks the Opsys for a large
block and then allocated chunks of that block to various dymanmic
data stuctures (like Unbounded_String), but when the program frees
one of those data structures, it is only part fo the large block
and so is not given back to the Opsys, it just becomes available
to be allocated for some other data structure in that program.
In fact, most programs never given back the large blocks until
the program finishes execution.  So that mean that the memory allocate
display you used likely only shows the high water mark for memory
allocation for your program.  Now what the rational was talking about
that when you free a data structure (like an Unbounded_String)
that memory become available for reallocation in the 
current program.

--
Mark Biggar
mark.biggar@trustedsyslabs.com



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

* Re: How do I free unbounded strings?
  2001-03-06  2:52 ` Mark Biggar
@ 2001-03-06 14:10   ` Larry Kilgallen
  0 siblings, 0 replies; 8+ messages in thread
From: Larry Kilgallen @ 2001-03-06 14:10 UTC (permalink / raw)


In article <3AA3DEC1.24B781CA@home.com>, Mark Biggar <mark.a.biggar@home.com> writes:
> Erik Sigra wrote:
>>   For unbounded strings the ":=" operation does the necessary storage
>>   management through Adjust and Finalize operations to allocate needed space
>>   for the new value of the target and to reclaim the space previously
>>   occupied by the object.
>> ...example program deleted. 
>> So it appears to me as if freeing doesn't work as the rationale says. Could
>> someone please help me with this?
> 
> Sure, first you must understand that there is a difference between
> freeing memory for a data structure in a program and a program
> actually giving back memory to the operating system.  On most operating
> systems when a program needs more memory it asks the Opsys for a large
> block and then allocated chunks of that block to various dymanmic
> data stuctures (like Unbounded_String), but when the program frees
> one of those data structures, it is only part fo the large block
> and so is not given back to the Opsys, it just becomes available
> to be allocated for some other data structure in that program.

While such a caching strategy can provide significant performance
benefits, there are situations where it is done by the "operating
system".  In VMS that is the LIB$*_VM runtime library calls, which
are delivered as part of the operating system although they are not
part of the kernel.

The same precautions you mention about measuring free memory still
apply.  Few programs bypass the LIB$*_VM runtime library calls.



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

end of thread, other threads:[~2001-03-06 14:10 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-05 17:03 How do I free unbounded strings? Erik Sigra
2001-03-05 20:14 ` Florian Weimer
2001-03-05 21:10   ` Erik Sigra
2001-03-06  1:49     ` Robert A Duff
2001-03-05 20:48 ` Jeffrey Carter
2001-03-05 20:55   ` Erik Sigra
2001-03-06  2:52 ` Mark Biggar
2001-03-06 14:10   ` Larry Kilgallen

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