ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Does rosbag write cause a memory leak?

asked 2015-08-25 17:34:22 -0500

sergiom gravatar image

I have the following code running on x86-64 Linux with ROS indigo:

#include <string>
#include <sys/sysinfo.h>
#include <ros/ros.h>
#include <rosbag/bag.h>
#include <std_msgs/String.h>

#define TWO_MiB (2*1024*1024)

int main(int argc, char **argv)
{
  std::string name("testwrite");
  ros::init(argc, argv, name, 0);
  struct sysinfo info;
  ros::Time time = ros::Time::now();
  ros::Rate loop(1); // 1 Hz

  while(ros::ok()) {
    std_msgs::String str;
    rosbag::Bag *bag = new rosbag::Bag();

    // Using bagmode::Write mode does not cause a memory leak.
    // but it also does not write continuously to a file, as you would expect.
    bag->open("test.bag", rosbag::bagmode::Append);

    sysinfo(&info);
    ROS_INFO("TR0. Mem: Free:%.1f MB\n", info.freeram / (float) 1e6);
    void *mem = malloc(TWO_MiB);
    if (!mem) goto loop_free;

    memset(mem, 'a', TWO_MiB);
    str.data = (char *) mem;

    // Remove the following line and no memory is leaked.
    bag->write("strings", time, str);

    sysinfo(&info);
    ROS_INFO("TR1. Mem: Free:%.1f MB\n", info.freeram / (float) 1e6);

    // Ensure the memory used by the bag is **really** free
    bag->close();
    delete bag;

    loop_free:
    free(mem);
    sysinfo(&info);
    ROS_INFO("TR2. Mem: Free:%.1f MB\n", info.freeram / (float) 1e6);
    loop.sleep();
  }
  return 0;
}

And the output is as follows:

[ INFO] [1440540265.784606302]: TR0. Mem: Free:3250.6 MB
[ INFO] [1440540265.811953308]: TR1. Mem: Free:3229.3 MB
[ INFO] [1440540265.814442553]: TR2. Mem: Free:3241.2 MB

[ INFO] [1440540266.784350396]: TR0. Mem: Free:3241.3 MB
[ INFO] [1440540266.808669181]: TR1. Mem: Free:3225.1 MB
[ INFO] [1440540266.811110143]: TR2. Mem: Free:3236.9 MB

[ INFO] [1440540267.784398301]: TR0. Mem: Free:3237.0 MB
[ INFO] [1440540267.808999283]: TR1. Mem: Free:3220.7 MB
[ INFO] [1440540267.811474048]: TR2. Mem: Free:3232.6 MB

[ INFO] [1440540268.784463342]: TR0. Mem: Free:3232.7 MB
[ INFO] [1440540268.808201640]: TR1. Mem: Free:3216.5 MB
[ INFO] [1440540268.810761252]: TR2. Mem: Free:3228.3 MB

[ INFO] [1440540269.784520258]: TR0. Mem: Free:3228.4 MB
[ INFO] [1440540269.809175512]: TR1. Mem: Free:3212.2 MB
[ INFO] [1440540269.811680539]: TR2. Mem: Free:3224.0 MB

[ INFO] [1440540270.789515610]: TR0. Mem: Free:3224.1 MB
[ INFO] [1440540270.814318890]: TR1. Mem: Free:3207.8 MB
[ INFO] [1440540270.816899544]: TR2. Mem: Free:3219.8 MB

[ INFO] [1440540271.784680288]: TR0. Mem: Free:3219.7 MB
[ INFO] [1440540271.809407003]: TR1. Mem: Free:3203.2 MB
[ INFO] [1440540271.811989272]: TR2. Mem: Free:3215.2 MB

If I comment out bag.write the memory on my system stays stable.

Am I using rosbag incorrectly or is the call to rosbag::Write leaking memory?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2015-08-26 02:45:36 -0500

ahendrix gravatar image

Measuring free memory is not an appropriate way to check for a memory leak, for a couple of reasons:

  • If there is lots of free memory, the OS may use it as a disk cache whenever it wants to (this is probably what you're seeing)
  • There are many other processes running on your system, and their memory consumption may vary.

If you want to check for memory leaks, use a memory check tool such as valgrind; these can track that each memory allocation call has a matching deallocation call, and can even give you hints about which allocations may be the source of memory leaks.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-08-25 17:34:22 -0500

Seen: 507 times

Last updated: Aug 26 '15