Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nK0lk-0001Lm-Vg for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Feb 2022 16:33:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nK0lj-0002jY-1d for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Feb 2022 16:33:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nK0li-0002jP-Ps for pgsql-bugs@lists.postgresql.org; Tue, 15 Feb 2022 16:33:34 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nK0le-0006aM-Pj for pgsql-bugs@lists.postgresql.org; Tue, 15 Feb 2022 16:33:34 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 21FGXQfe3052164; Tue, 15 Feb 2022 11:33:27 -0500 From: Tom Lane To: Daniel Gustafsson cc: wliang@stu.xidian.edu.cn, pgsql-bugs Subject: Re: Report a potential memory leak in setup_config() In-reply-to: References: <64ee7543.20d.17efb1270ca.Coremail.wliang@stu.xidian.edu.cn> Comments: In-reply-to Daniel Gustafsson message dated "Tue, 15 Feb 2022 13:10:45 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3052162.1644942806.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 15 Feb 2022 11:33:26 -0500 Message-ID: <3052163.1644942806@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Daniel Gustafsson writes: > On 15 Feb 2022, at 02:49, wliang@stu.xidian.edu.cn wrote: >> Specifically, at line 1095, function pretty_wal_size() is called, which= allocates a chunk of memory by using pg_malloc and returns it. However, t= he return chunk is directly passed to snprintf as its 3rd parameter. As a = result, there is a memory leak. > PostgreSQL isn't all too concerned about small static leaks in short liv= ed > programs, like initdb. Memory will be freed shortly when the program ex= its. > Complicating the code to save 28 bytes seems hardly worth it, but if you= feel > strongly about it I suggest proposing a patch to fix it. Yeah. A quick run of initdb under valgrind reports =3D=3D3051337=3D=3D LEAK SUMMARY: =3D=3D3051337=3D=3D definitely lost: 893,446 bytes in 47 blocks =3D=3D3051337=3D=3D indirectly lost: 4,490 bytes in 51 blocks =3D=3D3051337=3D=3D possibly lost: 0 bytes in 0 blocks =3D=3D3051337=3D=3D still reachable: 2,403 bytes in 22 blocks =3D=3D3051337=3D=3D suppressed: 0 bytes in 0 blocks It might be worth trying to knock that down a bit, but I wouldn't start with a one-time leak of 28 bytes. It looks like the biggest offender is that we don't bother trying to reclaim the lines malloc'd by readfile() and replace_token(). Fixing that is *maybe* worth the trouble, but TBH no one has complained about initdb's memory consumption. regards, tom lane