I recently wrote an application that accesses Postgres DB via libpqxx, and it does not tolerate memory well. Even this simple test program, based on the example of http://pqxx.org/devprojects/libpqxx/doc/4.0/html/Reference/a00001.html , runs like no tomorrow.
(Edit: I added the commit () and clear () calls in response to the suggestions. The same leak.)
#include <iostream> #include <pqxx/pqxx> #include <string> #include <stdio.h> int main() { try { pqxx::connection c("user=postgres"); int i = 0; while(true) { pqxx::work w(c); pqxx::result r = w.exec("SELECT 1"); w.commit(); i++; if ( i % 1000 == 0 ) printf( "Cycle %d\n", i ); r.clear(); } //while } //try catch (const std::exception &e) { std::cerr << e.what() << std::endl; return 1; } //catch } //main
After approximately 75,000 loop cycles, the top one shows 206 MB of virtual memory usage, and it continues to rise. I ran a similar test program with 5000 cycles through valgrind and got the following:
==1647== 13,732,155 (219,868 direct, 13,512,287 indirect) bytes in 4,997 blocks are definitely lost in loss record 12 of 12 ==1647== at 0x40060D5: operator new(unsigned int) (vg_replace_malloc.c:214) ==1647== by 0x404C0A9: pqxx::result::result(pg_result*, int, std::string const&, int) (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x40309EF: pqxx::connection_base::make_result(pg_result*, std::string const&) (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x4036D65: ??? (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x405EFD6: pqxx::transaction_base::DirectExec(char const*, int) (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x40416EA: pqxx::dbtransaction::do_exec(char const*) (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x40618FA: pqxx::transaction_base::exec(std::string const&, std::string const&) (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x80498F8: main (dbtest.cpp:21)
Any idea what is going on? It is very difficult to accept that a widely used library like libpqxx will have such a serious error, so what can I do wrong here?
Configuration Details:
- OS: Linux 2.6.18-238.el5
- gcc version 4.4.0
- libpqxx 4.0
- postgres 9.2
( Final edit: it was easier for me to replace libpqxx with libpq than to continue investigating a memory leak. )