Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2007-03

Array of Ansistring

8 messages · 6 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jason P Sage

Hello All.

I saw a great example of using:

MyArray:  Array of ansistring;

There is code I saw that seems to work great using:

SetLength(MyArray,100);
Which allows MyArray[100]:='Some ansiString';

What I don't know is how to clean this up.

Making all the 100 ansistrings in this example set to empty doesn't
Cut it, nor does trying to perform SetLength(MyArray,0);

At least not when I run tests and look at memory in use in the Win32 Task Manager in XP.

Does anyone know how to cleanly deallocate memory used in this way?

Or is this simply not a good way to code. I was drawn to it because it seems a rather fast way to have a list of strings and to search them. I'm counting milliseconds in my current project - so speed matters.

Thank you in advance.
Jason P Sage

Reply by email

Marco van de Voort
to Jason P Sage
Making all the 100 ansistrings in this example set to empty doesn't Cut it, nor does trying to perform SetLength(MyArray,0);

Both are ok.

At least not when I run tests and look at memory in use in the Win32 Task Manager in XP.

That is your problem. To optimize performance, nearly all memory allocators allocate memory from the OS in larger blocks, and then suballocate them.

Or is this simply not a good way to code.

It might not be good if you are going to stuff hundred thousands of them in an array. A few thousand till 30k-100k is no problem.

Reply by email

Bram Kuijvenhoven
to Jason P Sage: Hello All. I saw a great example of using: MyArray: Array of ansistring; There is code I…
Jason P Sage wrote 7 lines
MyArray: Array of ansistring; There is code I saw that seems to work great using: SetLength(MyArray,100); Which allows MyArray[100]:='Some ansiString';

Dynamic arrays start at index 0 up to Length-1, so actually this code would corrupt memory ;) (but I assume this is a typo in your mail)

What I don't know is how to clean this up.

It is cleaned automatically when the array goes out of scope (taking into account the refcounting of the dyn array itself), but of course also when you explicitly set the strings to '' or the array length to 0, as you proposed already. As Marco says, memory is usually not directly released back to the OS. BTW, memleaks can also be checked for with the heaptrc unit.

Regards,

Bram

Reply by email

Flávio Etrusco
to Bram Kuijvenhoven

A slightly related question: does FPC memory manager release unused memory back to the OS?

Regards,
Flávio

Bram Kuijvenhoven wrote 24 lines
Jason P Sage wrote: > MyArray: Array of ansistring; > > There is code I saw that seems to work great using: > > SetLength(MyArray,100); > > Which allows MyArray[100]:='Some ansiString'; Dynamic arrays start at index 0 up to Length-1, so actually this code would corrupt memory ;) (but I assume this is a typo in your mail) > What I don't know is how to clean this up. It is cleaned automatically when the array goes out of scope (taking into account the refcounting of the dyn array itself), but of course also when you explicitly set the strings to '' or the array length to 0, as you proposed already. As Marco says, memory is usually not directly released back to the OS. BTW, memleaks can also be checked for with the heaptrc unit. Regards, Bram _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Florian Klaempfl
to Flávio Etrusco
A slightly related question: does FPC memory manager release unused memory back to the OS?

Yes, but it keeps always one block to avoid repeated allocating/releasing.

Show quoted text 38 lines
Regards, Flávio On 3/12/07, Bram Kuijvenhoven wrote: > Jason P Sage wrote: > > MyArray: Array of ansistring; > > > > There is code I saw that seems to work great using: > > > > SetLength(MyArray,100); > > > > Which allows MyArray[100]:='Some ansiString'; > > Dynamic arrays start at index 0 up to Length-1, so actually this code > would corrupt memory ;) (but I assume this is a typo in your mail) > > > What I don't know is how to clean this up. > > It is cleaned automatically when the array goes out of scope (taking > into account the refcounting of the dyn array itself), but of course > also when you explicitly set the strings to '' or the array length to > 0, as you proposed already. As Marco says, memory is usually not > directly released back to the OS. BTW, memleaks can also be checked > for with the heaptrc unit. > > Regards, > > Bram > > > _______________________________________________ > fpc-devel maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-devel > _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Flávio Etrusco
to Jonas Maebe

Thanks for your replies, Florian and Jonas.
This is very nice! I was shocked and puzzled when I found the GNU libc allocator didn't release VM back to the OS, it's great to confirm you guys are (IMHO ;-) more sane than the gcc and libc folks :-)

Cheers,
Flávio

Jonas Maebe wrote 14 lines
On 12 mrt 2007, at 16:25, Flávio Etrusco wrote: > A slightly related question: does FPC memory manager release unused > memory back to the OS? Yes, but not all of it since that causes extreme slowdowns in certain cases (due to constant freeing and reallocating memory to/from the OS). Jonas_______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Jonas Maebe
to Flávio Etrusco
Flávio Etrusco wrote
Thanks for your replies, Florian and Jonas. This is very nice! I was shocked and puzzled when I found the GNU libc allocator didn't release VM back to the OS, it's great to confirm you guys are (IMHO ;-) more sane than the gcc and libc folks :-)

GNU libc does release memory back to the OS as well. In fact, before FPC's memory manager did this too, you had use the cmem unit (which maps Pascal memory manager calls to libc's malloc/free/...) to get this functionality.

Jonas

Reply by email