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

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

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
  MessageCollector(const std::string& 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)

    // close bag

  void msgCallback(const boost::shared_ptr<M const>& msg)

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

int main()

  MessageCollector<sensor_msgs::PointCloud2> collector(
  std::vector<std::string> topics{ "point_cloud_1",
                                   "point_cloud_3" };

  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?


edit retag flag offensive close merge delete


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 -0600 )edit