简体   繁体   English

在sqlite3中增加内存使用量?

[英]Increasing memory usage in sqlite3?

I've written a console app which receives events via boost::interprocess memory and dumps the info into an sqlite3 database. 我编写了一个控制台应用程序,该应用程序通过boost :: interprocess内存接收事件并将信息转储到sqlite3数据库中。 While running the app I've noticed that, in the Windows task manager, the memory usage was cyclically increasing every... 30s-1min. 运行该应用程序时,我注意到,在Windows任务管理器中,内存使用量每隔30s-1min周期性增加。 This led me to believe that the problem lies within the main loop in which I execute my SQL. 这使我相信问题出在执行SQL的主循环内。 I've added some monitoring and apparently the sqlite3_memory_usage returns increasing results every couple loop iterations. 我添加了一些监视功能,显然sqlite3_memory_usage在每两次循环迭代中都会返回增加的结果。

Can somebody tell me what am I doing wrong? 有人可以告诉我我在做什么错吗? Am I missing something I should de-allocate ? 我是否缺少我应该取消分配的内容?

Here are 2 strings I use to generate SQL 这是我用来生成SQL的2个字符串

    const std::string sql_insert =
      "INSERT INTO EventsLog "
      "(Sec, uSec, DeviceId, PmuId, EventId, Error, Msg) "
      "VALUES (%ld, %ld, %ld, %d, %ld, %d, %Q)";

    const std::string sql_create =
      "CREATE TABLE IF NOT EXISTS EventsLog("
      "Id INTEGER PRIMARY KEY AUTOINCREMENT, "
      "Sec INTEGER NOT NULL, "
      "uSec INTEGER NOT NULL, "
      "DeviceId INTEGER NOT NULL, "
      "PmuId INTEGER NOT NULL, "
      "EventId INTEGER NOT NULL, "
      "Error INTEGER NOT NULL, "
      "Msg TEXT"
      ")";

In here, I generate the SQL INSERT command 在这里,我生成SQL INSERT命令

std::string construct_sql_query
    (const ELMessageData & data)
    {
      std::string query = "";

      ptime jan1st1970 = ptime(date(1970,1,1));
      ptime now = boost::posix_time::microsec_clock::universal_time();
      time_duration delta = now - jan1st1970;
      TimeVal time((uint32)delta.total_seconds(),
                   (uint32)now.time_of_day().fractional_seconds());

      char * const sql = sqlite3_mprintf(sql_insert.c_str(),
                                         time.tv_sec,
                                         time.tv_usec,
                                         data.getDeviceId(),
                                         data.getPmuId(),
                                         data.getEventId(),
                                         (data.getIsError() ? 1 : 0),
                                          data.getExMsg().c_str());
      if(sql == NULL)
        post_event(EvIOError("Failed to create the SQL command",
                             "StLoggingEvents::_construct_sql_query"));

      query = std::string(sql);
      sqlite3_free(sql);

      return query;
    } // construct_sql_query

Here's the main loop in which I execute the INSERT commands 这是我执行INSERT命令的主循环

 while(true)
    {
      m_exchange_obj->wait(); // wait for the semaphore to be raised
  const std::string sql = construct_sql_query
    (m_exchange_obj->receive());

  char ** err = NULL;

  const int rc = sqlite3_exec(m_db_handle,
                              sql.c_str(),
                              NULL,
                              NULL,
                              err);
  sqlite3_free(err);

  if(rc != SQLITE_OK)
  {
    LERR_ << "Error while inserting into the database";
    LERR_ << "Last SQL Query : ";
    LERR_ << sql;
  }
  else
  {
    LDBG_ << "Event logged...";
    LDBG_ << "Sqlite3 memory usage : "
          << sqlite3_memory_used();
  }
}

I second the suggestion of trying this under valgrind. 我赞同在valgrind下尝试此操作的建议。 You may also want to look at google's tcmalloc replacement... It can print pretty graphs showing you all your leaks... That said, I hope you get the answer for this... I plan on using SQLite in an upcoming project... 您可能还想看看google的tcmalloc替换...它可以打印出漂亮的图形来显示所有泄漏...也就是说,我希望您能得到答案...我计划在即将到来的项目中使用SQLite。 ..

How are you determining your memory usage? 您如何确定内存使用情况? You may not have a real leak. 您可能没有真正的泄漏。

If you are on a windows system and using visual studio, compile in debug mode and use the memory debugging macros to find your leaks. 如果您使用的是Windows系统并使用Visual Studio,请在调试模式下进行编译,然后使用内存调试宏查找泄漏。

If you are on a unix based system, try valgrind / memcheck. 如果您使用的是基于Unix的系统,请尝试valgrind / memcheck。

I think OS X's xcode also detects leaks too. 我认为OS X的xcode也可以检测到泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM