Robotics StackExchange | Archived questions

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

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):
    rclpy.init(args=args)

    minimal_subscriber = MinimalSubscriber()

    rclpy.spin(minimal_subscriber)

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

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.

So:

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?

Thanks!

Asked by renarded on 2020-06-04 08:18:10 UTC

Comments

Answers

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.

Asked by leonard on 2022-10-24 08:21:50 UTC

Comments