Results 1 to 9 of 9

Thread: Sqlite Memory Use

  1. #1
    Join Date
    Dec 2009
    Posts
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Sqlite Memory Use

    I have a simple app that uses a database as a log and just repeatedly INSERT's.
    Originally with Postgresql everything was OK, fine.

    Changed to SQLITE by changing the QSqlDatabase::addDatabase
    Now the app just chews memory, approx 200K per INSERT. It is currently using 1.5G and increasing

    IS this a SQLITE bug or a QT database bug or Normal behaviour or what ??

    thanks

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sqlite Memory Use

    For what it's worth, I just ran the following test and it only chews up a couple of megabytes of memory. Could you give more details on your project?
    Qt Code:
    1. void doSqLiteTest()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    4. db.setDatabaseName("CatchyName");
    5. db.open();
    6.  
    7. QString sql = QString("CREATE TABLE T1(F1, F2, F3)");
    8. QSqlQuery query(db);
    9. query.exec(sql);
    10.  
    11. query.exec("BEGIN TRANSACTION;");
    12.  
    13. for (int i = 0; i < 500000; i++)
    14. {
    15. query.prepare("INSERT INTO T1 (F1, F2, F3) "
    16. "VALUES (:id, :forename, :surname)");
    17. query.bindValue(":id", 1001);
    18. query.bindValue(":forename", "Bart");
    19. query.bindValue(":surname", "Simpson");
    20. query.exec();
    21. }
    22.  
    23. query.exec("END TRANSACTION;");
    24. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sqlite Memory Use

    If you want to keep in the spirit of Qt, you can use db.transaction() and db.commit() instead of query.exec("BEGIN TRANSACTION;"); and end transaction

  4. The following user says thank you to tbscope for this useful post:

    numbat (3rd June 2010)

  5. #4
    Join Date
    Dec 2009
    Posts
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite Memory Use

    Hmm my code looks more like (see below).
    Thats artificial by the way, in fact the inserts happen randomely from a few per second to a few per minute
    and the basic logic is
    Open
    INSERT
    Close

    Could it be the TRANSACTION that frees the memory ??
    Do INSERTS have to be wrapped by TRANSACTIONS ?



    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(DBNAME);

    for (int i = 0; i < 40000; i++)
    {
    bOk = db.open();
    if ( !bOk )
    { qDebug() << "db.Open() FAILED"; break; }

    QSqlQuery query(db);
    QString qStr = QString ("INSERT INTO socketlog ([ip],[outlet],[watts],[volts],[amps],[temp],[freq],[powerfactor]) "
    "VALUES(%1,0,10,240,2,15,50,1);").arg(19216811 + (i % 100));
    bOk = query.exec(qStr);
    db.close();
    if ( !bOk )
    qDebug() << "DB insert_sockdata FAILED :: " << query.lastError();
    else
    qDebug() << "DB insert_sockdata OK " << i;
    }
    Last edited by drave; 4th June 2010 at 10:22.

  6. #5
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sqlite Memory Use

    Do INSERTS have to be wrapped by TRANSACTIONS ?
    No, but doing so gives a 1000 fold improvement in speed. See the SQLite FAQ.

    I reallly don't think you want to be calling db.open 40000 times. Probably best to do it once for the whole application.

  7. #6
    Join Date
    Dec 2009
    Posts
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite Memory Use

    thats for speed . any ideas on memory chewing ?

  8. #7
    Join Date
    Dec 2009
    Posts
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite Memory Use

    bizarre behaviour.
    to further test that it wasnt all the opening and closing that was leaking memory i wrapped the query like so

    db.open
    if ( RUNQUERY == true)
    {
    QSqlQuery query(db);
    QString qStr = QString ("INSERT INTO socketlog ([ip],[outlet],[watts],[volts],[amps],[temp],[freq],[powerfactor]) "
    "VALUES(%1,0,10,240,2,15,50,1);").arg(19216811 + (i % 100));
    bOk = query.exec(qStr);
    }
    else bOk = true;
    db.close

    when RUNQUERY is true this arrangement does not leak memory. duh!! how is that possible?
    so is it the query or querystring that is not being destroyed properly or ... confusion rains

  9. #8
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite Memory Use

    When is the memory leak being measured?

    In the first case you have potentially 40000 sets of objects being created in your 'for' loop which may not be deleted until the loop unwinds/exits.
    Opening and closing the DB connection does not have any effect on the life of the SQL query objects.

    In the second everything is created and destroyed with in the scope of the 'if' statement.

    By the way please use [CODE] tags, it makes the source easier to read.
    Got to keep the loonies on the path ...

  10. #9
    Join Date
    Dec 2009
    Posts
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite Memory Use

    i'm measuring by watching the working set in Task Manager

    when the loop finish's the working set does not go back down, it remains at the high level and if you run the loop again the memory will begin to climb again

    dont those objects go in and out of scope, and get created/destroyed with each loop? at least i thought they did

    plus as i said this behaviour is with SQLITE it didnt do that with Postgresql
    this is just test code that i wrote after noticing the climbing memory in the real app, that has an OPEN,INSERT,CLOSE logic but not in a loop. i just put it in a loop for test purposes

Similar Threads

  1. Loading and saving in-memory SQLITE databases
    By miraks in forum Qt Programming
    Replies: 10
    Last Post: 27th April 2010, 21:24
  2. [Qt][SQLite] Two problems with SQLite.
    By Xandareva in forum Newbie
    Replies: 6
    Last Post: 6th April 2010, 23:06
  3. OUT OF MEMORY - Error while working with sqlite
    By Askar in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2009, 23:54
  4. How can I send a SQLite :memory: database connection ?
    By georgep in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2009, 12:07
  5. SQLite in QT
    By sophister in forum Qt Programming
    Replies: 11
    Last Post: 8th April 2009, 16:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.