Rosbag C++ API: how to avoid tremendous consumption of RAM

asked 2022-08-30 10:08:26 -0500

schizzz8 gravatar image

Hi all,

I wrote a simple class to read sensor_msgs::PointCloud2 messages from a rosbag follwing the cookbook:

#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <sensor_msgs/PointCloud2.h>

template <class M>
class MessageCollector
{
public:
  MessageCollector(const std::string& filename)
  {
    bag_.open(filename, rosbag::bagmode::Read);
  }

  void loadBag(const std::vector<std::string>& topics)
  {

    // create bag view
    rosbag::View view(bag_, rosbag::TopicQuery(topics));

    // Load all messages into the dataset
    BOOST_FOREACH (rosbag::MessageInstance const m, view)
    {
      boost::shared_ptr<M const> msg = m.instantiate<M>();
      if (msg != NULL)
      {
        msgCallback(msg);
      }
    }

    // close bag
    bag_.close();
  }

  void msgCallback(const boost::shared_ptr<M const>& msg)
  {
    dataset_.push_back(msg);
  }

  rosbag::Bag bag_;
  std::vector<boost::shared_ptr<M const>> dataset_;
};

int main()
{

  ros::Time::init();
  MessageCollector<sensor_msgs::PointCloud2> collector(
      "data.bag");
  std::vector<std::string> topics{ "point_cloud_1",
                                   "point_cloud_2",
                                   "point_cloud_3" };
  collector.loadBag(topics);

  return 0;
}

My problem is that the data.bag is ~50GB and contains ~50K lidars messages.

So, after few seconds this executable consumes all of my 32GB of RAM.

Please, can someone explain what is going on and/or what I'm doing wrong?

Thanks.

edit retag flag offensive close merge delete

Comments

Your code appears to deserialise all messages on three topics, all of which appear to carry point clouds, and store them in a std::vector. PointCloud2 messages can be very, very big. Storing all of them from a 50GB .bag in-memory in a std::vector will likely use a lot of memory, which I believe is what you're seeing.

Could you clarify what you expected to happen? Perhaps we can clarify something for you.

gvdhoorn gravatar image gvdhoorn  ( 2022-08-31 09:36:40 -0500 )edit