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

Why should we use destroy_node() for Python nodes in ROS2?

asked 2020-06-04 08:18:10 -0500

Hi everyone,

In the ROS2 examples using rclpy (for example the subscriber example), in the main function I always see the use of the rclpy destroy_node() method.

def main(args=None):

    minimal_subscriber = MinimalSubscriber()


    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)

To me the explanation in the comment does not give much info.

I've tried to run the code without this line to see if something would happen, and I got no error so far.


Why should we use destroy_node() before calling rclpy.shutdown()?

Can I expect any kind of issue if I don't explicitely destroy nodes (if I just shutdown ROS and exit after)?

Also, is this practice temporary, and will this line be removed from examples and tutorials in the future?


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-10-24 08:21:50 -0500

leonard gravatar image

Not having garbage collection is a bit of a relic from times when memory and processing power where scarce. When you run stuff on your PC and the RAM doesn't instantly get freed up you don't necessarily run into problems, but in my opinion it starts to matter when you run a lot of nodes on a RaspberryPi or something equivalent with not much RAM.

edit flag offensive delete link more

Question Tools



Asked: 2020-06-04 08:18:10 -0500

Seen: 2,662 times

Last updated: Jun 04 '20