question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak on rclpy subscriber

See original GitHub issue

During the work, we noticed that memory consumption of our docker container(with ROS2 Foxy) has been increased with time. We did research and found that this memory is allocated for the node, written in python.

We tried to reproduce the issue with a simple application and we reproduced it on clean foxy docker with a demo application.

Bug report

Required Info:

  • Operating System:
    • Ubuntu 18.04(doesn’t matter in this case, as in docker other OS is used)
  • Installation type:
    • binaries
  • Version or commit hash:
    • Foxy
  • DDS implementation:
    • default
  • Client library (if applicable):
    • rclpy

Steps to reproduce issue

  1. Download docker file from the repository dockerfile
  2. Go to the folder where you’ve saved dockerfile in the previous step Build docker with a command like docker build -t ros2-foxy .
  3. Run docker with the listener from demo py package docker run -it --rm ros2-foxy ros2 run demo_nodes_py listener
  4. Run rqt tool on your environment or in the other docker
  5. Check if /chatter topic is available. It should be created by the listener node
  6. In rqt, configure message publisher to send messages to the chatter with high frequency, e.g. 5000Hz. The frequency doesn’t matter, but with such frequency, we will see issue sooner
  7. Check docker(where the listener is run) memory consumption. In my case it was ~60Mb
  8. Wait for some time, in my case about 30 min

Expected behavior

Memory consumption is on the same level

Actual behavior

Memory consumption is increased to 330mB

Additional information

Memory consumption is not decreased when we stop message publishing.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
iuhilnehc-ynoscommented, Dec 8, 2020

@fujitatomoya

with _WaitSet() to call __enter__ and __exit__, it seems there is no memory leak. https://github.com/ros2/rclpy/blob/d7b4688743a99063e444f216f30c462fb03369b3/rclpy/rclpy/executors.py#L63-L71

Maybe it’s related to

diff --git a/rclpy/src/rclpy/_rclpy.c b/rclpy/src/rclpy/_rclpy.c
index a09b91a..b1bcefa 100644
--- a/rclpy/src/rclpy/_rclpy.c
+++ b/rclpy/src/rclpy/_rclpy.c
@@ -3446,10 +3446,12 @@ rclpy_message_info_to_dict(rmw_message_info_t * message_info)
   PyObject * source_timestamp = PyLong_FromLongLong(message_info->source_timestamp);
   if (source_timestamp != NULL) {
     PyDict_SetItemString(dict, "source_timestamp", source_timestamp);
+    Py_DecRef(source_timestamp);
   }
-  PyObject * received_timestamp = PyLong_FromLongLong(message_info->source_timestamp);
+  PyObject * received_timestamp = PyLong_FromLongLong(message_info->received_timestamp);
   if (received_timestamp != NULL) {
-    PyDict_SetItemString(dict, "received_timestamp", source_timestamp);
+    PyDict_SetItemString(dict, "received_timestamp", received_timestamp);
+    Py_DecRef(received_timestamp);
   }
 
   // check for errors
0reactions
yfedicommented, Dec 16, 2020

Yes, as the issue has been addressed we can close it

Read more comments on GitHub >

github_iconTop Results From Across the Web

ROS2 serious memory leak when using timer? - ROS Answers
Trying memory profiler (mprof) shows that in 4 min, memory consumption is increased by at least 10MB. import sys import rclpy import time ......
Read more >
Google Cloud PubSub python subscriber leaks memory
It looks like the subscriber is leaking memory and the amount of memory leaked is directly proportional to the underlying callback memory usage ......
Read more >
Topics — rclpy 0.6.1 documentation - ROS 2 Docs
topic ( str ) – The name of the topic the subscription will subscribe to. callback ( Callable ) – A user-defined callback...
Read more >
Preventing Angular Subscription Memory Leaks | Jake Cyr
In my early Angular days, I would subscribe to observables without unsubscribing before the app was destroyed, possibly leading to a build up...
Read more >
Debug a memory leak in .NET Core - Microsoft Learn
This tutorial demonstrates the tools to analyze a memory leak in a . ... Here you can see that most objects are either...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found