Re: Memory Management in pqlib, Garbage Collection support

Поиск
Список
Период
Сортировка
От Peter Bex
Тема Re: Memory Management in pqlib, Garbage Collection support
Дата
Msg-id 20120503081327.GB23432@frohike.homeunix.org
обсуждение исходный текст
Ответ на Re: Memory Management in pqlib, Garbage Collection support  (Alexander Reichstadt <lxr@mac.com>)
Список pgsql-general
On Thu, May 03, 2012 at 09:39:29AM +0200, Alexander Reichstadt wrote:
> Thanks, that's answering my question. In Objective-C as well as many other languages there is the feature to turn on
GarbageCollection. It's a separate thread that scans memory for strong pointers, their source and origin and "vacuums"
memoryso to not have any leaks. Anything unreferenced and no longer needed is cleaned up automatically. There are some
bordercases where GC can fail, but for most it works. 

OK, so you're specifically talking about Objective C.  I don't know much
about that language, but unless there are specific ObjC-bindings for
libpq (like libpq++ for C++), you'll need to perform manual memory
management and call PQClear yourself.

> As GC is an evolutionary stage across languages [...]

Thank you for your explanation of GC but that really wasn't neccessary.
The confusion was mostly due to your mail omitting the fact that you
were using ObjC rather than C, which would be the default assumption
when talking about libpq.

Actually, my language of choice (Chicken Scheme) is also GCed and the
postgresql bindings I wrote for it integrate with its GC in such a way
as to automatically call PQClear when an object gets collected.  This
is easy to do in any language that supports "finalizers", and with a
little more effort it could even be integrated more tightly with the GC.
The user can also manually decide to clear up the memory used by a
result set when it's known in advance that this will no longer be needed
and the memory use is prohibitive.

I think if you want something similar you'll either need to write your
own ObjC class to wrap the C functions or look for something pre-existing.
Perhaps you can use libpq++.  I've also found a "PostgreSQL Cocoa Framework"
project (but it appears to be abandoned): http://pgsqlcocoa.sourceforge.net/

> In general there are libs that provide garbage collection for C as well, like here:
> <http://www.hpl.hp.com/personal/Hans_Boehm/gc/>

The Boehm GC is a good prototyping solution but not very suitable for
general use.  It's an especially bad idea to force use of such a garbage
collector on the user whenever using libpq.  This would also prohibit
the integration of libpq with other languages and their GCs.

AFAIK libpq currently does not offer specifying callback functions to
use for allocating and freeing objects (it uses malloc/free directly).
If this was available, it would be even easier to integrate deeply with
a custom GC.

> For example, it'd help avoid leaks like those caused by a result not being PQclear'ed.

C programmers are generally comfortable with manual memory management, or
they'd use another language.

Cheers,
Peter
--
http://sjamaan.ath.cx
--
"The process of preparing programs for a digital computer
 is especially attractive, not only because it can be economically
 and scientifically rewarding, but also because it can be an aesthetic
 experience much like composing poetry or music."
                            -- Donald Knuth

В списке pgsql-general по дате отправления:

Предыдущее
От: Magnus Hagander
Дата:
Сообщение: Re: Memory Management in pqlib, Garbage Collection support
Следующее
От: Simon Riggs
Дата:
Сообщение: Re: Memory Management in pqlib, Garbage Collection support