c++ - 如何使用 boost 日志防止内存增长?

标签 c++ memory boost boost-log boost-logging

在这个link中运行boost日志的示例代码会继续增加内存占用。我认为这是内存泄漏。

boost 库版本:1.64.0

This is the piece of code I'm checking more carefully
/*
 *          Copyright Andrey Semashev 2007 - 2013.
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE_1_0.txt or copy at
 *          http://www.boost.org/LICENSE_1_0.txt)
 */
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <iostream>


#include <fstream>
#include <boost/ref.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>

#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sinks.hpp>
//#include <boost/log/sinks/sync_frontend.hpp>
//#include <boost/log/sinks/text_file_backend.hpp>

namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;

using boost::shared_ptr;

enum
{
    LOG_RECORDS_TO_WRITE = 10000,
    THREAD_COUNT = 19
};


//BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(tmp_logger, src::logger_mt)
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::logger_mt)

//! This function is executed in multiple threads
void thread_fun(boost::barrier& bar)
{
    // Wait until all threads are created
    bar.wait();

    // Now, do some logging
    // modify code start
    while(1) {
        BOOST_LOG(test_lg::get()) << "Log record Log record Log record Log record Log record Log record Log record Log record ";
    }
    // modify code end
}

int main(int, char*[])
{
    try
    {
        // Open a rotating text file
        shared_ptr< std::ostream > strm(new std::ofstream("test.log"));

        if (!strm->good()) {
            throw std::runtime_error("Failed to open a text log file");
        }

        // Create a text file sink
        shared_ptr< sinks::synchronous_sink< sinks::text_ostream_backend > > sink(
                new sinks::synchronous_sink< sinks::text_ostream_backend >
                );

        sink->locked_backend()->add_stream(strm);

        sink->set_formatter(
             expr::format("%1%: [%2%] [%3%] - %4%")
             % expr::attr< unsigned int >("RecordID")
             % expr::attr< boost::posix_time::ptime >("TimeStamp")
             % expr::attr< attrs::current_thread_id::value_type >("ThreadID")
             % expr::smessage
            );

        // Add it to the core
        logging::core::get()->add_sink(sink);

        // Add some attributes too
        logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
        logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
        logging::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());

        // Create logging threads
        boost::barrier bar(THREAD_COUNT);
        boost::thread_group threads;

        for (unsigned int i = 0; i < THREAD_COUNT; ++i)
            threads.create_thread(boost::bind(&thread_fun, boost::ref(bar)));

        // Wait until all action ends
        threads.join_all();
    }
    catch (std::exception& e)
    {
        std::cout << "FAILURE: " << e.what() << std::endl;
        return 1;
    }
    return 0;
}

当 while 循环继续时,可用大小的数量继续减少。 开始时间(18:50:56)的可用容量为 28,331KB,55 分钟后(19:45:14)的可用容量为 13,449KB,相差约 15MB

编辑:“MemFree”行的/proc/meminfo 结果

它继续缩小,导致 oom,最终导致内核崩溃,并重新启动。

如何解决这个问题?我期待听到您的意见和建议

最佳答案

Memory verification was done using "MemFree" value in "cat /proc/meminfo"

这不是衡量进程内存使用情况的可靠方法。

相反,在内存分析器下运行您的代码,例如 valgrind --tool=massif。

我运行了您修改后的示例,但是有 20 个线程,在 Massif 下运行了 10 分钟,它清楚地表明没有增长:

enter image description here

注意事项:

  1. 你可能有另一个进程泄漏内存
  2. 您可能将日志文件本身(隐含地)放在 tmpfs 中,从而减少可用内存
  3. 旧版本的 boost 中可能存在错误(我使用 boost 1.67 进行了测试)

关于c++ - 如何使用 boost 日志防止内存增长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50409739/

相关文章:

c++ - Eclipse 编译调试与 native 调试(C++ 代码)

c++ - 在 vector 中使用共享指针来访问类对象

c++ - 在 CUDA 中传递内核参数?

c++ - 为什么在代码段中从 char* 转换为 std::string 比转换为 const char* 更受欢迎?

c++ - Visual Studio 2013 : linker errors with custom enumerations?

c++ - 运算符 == 和 boost::detail::atomic_count 中的显式构造函数?

可以从类中设置的 C++ 只读结构?

c++ - 使用 fstream 关​​闭文件

iphone - 非 8 字节对齐内存访问导致运行 iOS 4.0 的 iPhone 3GS 上的内存访问冲突

django - 我可以在不使用 RAM 的情况下通过 HTML 表单将大图像上传到我的服务器吗?