I used Valgrind to check memory usage in a program I'm creating and found out that the problem is mostly in rosccp.
==21986==
==21986== 5,380,160 bytes in 3,440 blocks are definitely lost in loss record 161 of 161
==21986== at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21986== by 0x68A9637: getifaddrs_internal (ifaddrs.c:402)
==21986== by 0x68AA19F: getifaddrs (ifaddrs.c:825)
==21986== by 0x5328D15: ros::Transport::Transport() (in /opt/ros/indigo/lib/libroscpp.so)
==21986== by 0x53342BF: ros::TransportTCP::TransportTCP(ros::PollSet*, int) (in /opt/ros/indigo/lib/libroscpp.so)
==21986== by 0x5334716: ros::TransportTCP::accept() (in /opt/ros/indigo/lib/libroscpp.so)
==21986== by 0x5335B52: ros::TransportTCP::socketUpdate(int) (in /opt/ros/indigo/lib/libroscpp.so)
==21986== by 0x5364E4D: ros::PollSet::update(int) (in /opt/ros/indigo/lib/libroscpp.so)
==21986== by 0x53022A7: ros::PollManager::threadFunc() (in /opt/ros/indigo/lib/libroscpp.so)
==21986== by 0x739AA49: ??? (in /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.54.0)
==21986== by 0x75AD181: start_thread (pthread_create.c:312)
==21986== by 0x688FFBC: clone (clone.S:111)
==21986==
==21986== LEAK SUMMARY:
==21986== definitely lost: 5,422,612 bytes in 3,469 blocks
==21986== indirectly lost: 207,979 bytes in 4,227 blocks
==21986== possibly lost: 2,467 bytes in 18 blocks
==21986== still reachable: 3,376 bytes in 39 blocks
==21986== suppressed: 0 bytes in 0 blocks
==21986== Reachable blocks (those to which a pointer was found) are not shown.
==21986== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==21986==
==21986== For counts of detected and suppressed errors, rerun with: -v
==21986== ERROR SUMMARY: 30 errors from 30 contexts (suppressed: 0 from 0)
Is there anyway around it? This test was made with the program running for only 5 minutes, after two hours with my program running it slows down considerably because of this memory usage.
==== EDIT ====
I'm adding here an example code that shows this behavior in my laptop:
#include "ros/ros.h"
#include "custom_pkg_msgs/CustomAction.h"
#include "custom_pkg_msgs/CustomSrv.h"
#include <mcheck.h>
int main(int argc, char **argv)
{
// start ros
ros::init(argc, argv, "test_node");
ros::NodeHandle n;
ros::Rate loop_rate(1);
srand (time(NULL)); // start random fucntions
ros::Publisher chatter_pub = n.advertise<custom_pkg_msgs::CustomAction>("/random_topic", 1000);
// client to start game service and server for end game service
ros::ServiceClient start_game_client = n.serviceClient<custom_pkg_msgs::CustomSrv>("/random_service");
ros::service::waitForService("/random_service", -1);
// spin to answer services
ros::spin();
// shutdown ros node
ros::shutdown();
}
And in the terminal I run: valgrind --leak-check=full --trace-children=yes rosrun custom_package test_node
I'm running valgrind-3.10.0.SVN on a Ubuntu 14.04, 64bits and with ROS indigo.
Originally posted by t.pimentel on ROS Answers with karma: 385 on 2014-11-03
Post score: 4
Original comments
Comment by gvdhoorn on 2014-11-03:
It would probably help if you could post the code that you are running through Valgrind.
Comment by t.pimentel on 2014-11-03:
I edited my question with an example code.
Comment by gvdhoorn on 2014-11-04:
As in this question, Valgrind versions can differ greatly in their sophistication when it comes to detecting leaks. Could you add some version information to your OP? ROS, OS, valgrind?
Comment by t.pimentel on 2014-11-04:
I'm using valgrind-3.10.0.SVN on a Ubuntu 14.04, 64bits and ROS indigo.
A task manager, htop, also exposes this problem. The memory used by the ROS program increases indefinitely.
Comment by jbinney on 2014-11-04:
Could you describe what other nodes you have running? I'm not able to reproduce the memory leak by just running the code posted here - do you have subscribers on the chatter_pub topic?