Libpq memory leak

Started by Polyakov Vladimirover 15 years ago7 messagesgeneral
Jump to latest
#1Polyakov Vladimir
vvpolyakov@gmail.com

Program written in C using Libpq, which receives large files (BYTEA)
has a memory leak.
I need to free ALL of the used memory after each sql query.

after each call PQclear() I drop the buffer:
conn->inBuffer = realloc(conn->inBuffer, 8192);
conn->inBufSize = 8192;

It works, but ..
I noticed that in some cases PQclear() does not clear the memory.
This happens only when the program receives certain files...

Maybe there's some buffers that should make realloc()?
Or is it a bug?

versions tested 9.0.0 and 8.4.4

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Polyakov Vladimir (#1)
Re: Libpq memory leak

Polyakov Vladimir <vvpolyakov@gmail.com> writes:

Program written in C using Libpq, which receives large files (BYTEA)
has a memory leak.
I need to free ALL of the used memory after each sql query.

after each call PQclear() I drop the buffer:
conn->inBuffer = realloc(conn->inBuffer, 8192);
conn->inBufSize = 8192;

When you break it, you get to keep both pieces. Whatever gave you
the idea that the above would be considered a supported thing to do?

regards, tom lane

#3Dmitriy Igrishin
dmitigr@gmail.com
In reply to: Polyakov Vladimir (#1)
Re: Libpq memory leak

Hey Vladimir,

2010/9/24 Polyakov Vladimir <vvpolyakov@gmail.com>

Program written in C using Libpq, which receives large files (BYTEA)
has a memory leak.
I need to free ALL of the used memory after each sql query.

after each call PQclear() I drop the buffer:
conn->inBuffer = realloc(conn->inBuffer, 8192);
conn->inBufSize = 8192;

It works, but ..
I noticed that in some cases PQclear() does not clear the memory.
This happens only when the program receives certain files...

Why do you need realloc() after PQclear()?

--
Regards,
Dmitriy

#4Bret S. Lambert
bret.lambert@gmail.com
In reply to: Dmitriy Igrishin (#3)
Re: Libpq memory leak

On Fri, Sep 24, 2010 at 06:11:31PM +0400, Dmitriy Igrishin wrote:

Hey Vladimir,

2010/9/24 Polyakov Vladimir <vvpolyakov@gmail.com>

Program written in C using Libpq, which receives large files (BYTEA)
has a memory leak.
I need to free ALL of the used memory after each sql query.

after each call PQclear() I drop the buffer:
conn->inBuffer = realloc(conn->inBuffer, 8192);
conn->inBufSize = 8192;

This is a known unsafe use of the realloc() function. If if fails to
allocate memory, you just lost the conn->inBuffer, thus leaking memory
in your own code. Fix this first, and then see if you still have the
issue with memory leaks, because it's possible you're just leaking it
with a bad realloc() idiom.

Show quoted text

It works, but ..
I noticed that in some cases PQclear() does not clear the memory.
This happens only when the program receives certain files...

Why do you need realloc() after PQclear()?

--
Regards,
Dmitriy

#5Sergey Burladyan
eshkinkot@gmail.com
In reply to: Tom Lane (#2)
Re: Libpq memory leak

Tom Lane <tgl@sss.pgh.pa.us> writes:

Polyakov Vladimir <vvpolyakov@gmail.com> writes:

Program written in C using Libpq, which receives large files (BYTEA)
has a memory leak.
I need to free ALL of the used memory after each sql query.

I discussed this yesterday with Vladimir. I think Vladimir trying to
complain about behavior of libpq input buffer. It is never shrinking, if
i not mistaken, it is only grow.

So, for example, if you select long bytea field at start of the
connection and then select only small fields, you do not need this big
inBuffer in memory, but you are still with it during all lifetime of
this connection.

Here is example:

Attachments:

pg-test-bytea-free.ctext/x-csrcDownload
#6Dmitriy Igrishin
dmitigr@gmail.com
In reply to: Sergey Burladyan (#5)
Re: Libpq memory leak

Hey Sergey,

As i can understand Vladimir, he have many clients reading bytea in one

server like this and he is trying to reduce memory consumption.

Why not use large objects for this purpose?

--
Regards,
Dmitriy

#7Dmitriy Igrishin
dmitigr@gmail.com
In reply to: Sergey Burladyan (#5)
Re: Libpq memory leak

Hey Sergey,

As i can understand Vladimir, he have many clients reading bytea in one

server like this and he is trying to reduce memory consumption.

Why not use large objects for this purpose?

// Dmitriy.