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

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

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

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!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

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

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

4 followers

Stats

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

Seen: 3,228 times

Last updated: Jun 04 '20