Re: Memory leak

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: fan yang <yangfangoto(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Memory leak
Date: 2017-07-29 14:06:27
Message-ID: 27758.1501337187@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

fan yang <yangfangoto(at)gmail(dot)com> writes:
> - src/port/quotes.c
> At line 38, at function "escape_single_quotes_ascii",
> here used "malloc" to get some memory and return the
> pointer returned by the "malloc".
> So, any caller used this function shoule free this memory.
> - /src/bin/initdb/initdb.c
> At line 327, at function "escape_quotes",
> which use function "escape_single_quotes_ascii"
> from above file.
> But at this file(/src/bin/initdb/initdb.c), there are many place
> used function "escape_quotes" but have not free the pointer returned
> by the function, thus cause memory leak.

By and large, we intentionally don't worry about memory leaks in initdb
(or any other program with a limited runtime). It's not worth the
maintenance effort to save a few bytes, at least not where it requires
code contortions like these.

Doing a quick check with valgrind says that a run of initdb, in HEAD,
leaks about 560KB over its lifespan. That's well below the threshold
of pain on any machine capable of running modern Postgres reasonably.

For fun, I tried to see whether your patch moved that number appreciably,
but patch(1) couldn't make any sense of it at all. I think that probably
your mail program munged the whitespace in the patch. Many of us have
found that the most reliable way to forward patches through email is to
add them as attachments rather than pasting them into the text in-line.

Poking at it a bit harder with valgrind, it seems that the vast majority
of what it reports as leaks is caused by replace_token(). If we wanted to
reduce memory wastage during initdb, that would be the place to work on.
But I'm dubious that it's worth any effort.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fan Yang 2017-07-29 16:34:33 Re: Memory leak
Previous Message fan yang 2017-07-29 09:50:52 Memory leak