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

Is object NodeHandle::serviceClient<theService> small?

asked 2014-10-15 07:21:52 -0500

arennuit gravatar image

updated 2014-10-16 07:19:11 -0500

Dear all,

I'd like to call a service in my main loop. This led me using


At the moment I create a new NodeHandle::serviceClient for each service call (ie at each step ~ very often), is that correct practise?

My question relates to how big object NodeHandle::serviceClient is really, because if this is small it is fine to instantiate on the stack at each loop. This question assumes objects theService are small as well.

--- EDIT ---

Although I have an alternative (simply not creating the object at each loop: easy!), I am still interested in knowing whether the class is heavy or not... From reading the source I would say it only contains 3 pointers (so not a big deal to instantiate it in a loop) but maybe there are subtleties I do not understand... Anyone knows about it?



edit retag flag offensive close merge delete


What does 'heavy' mean, and what would be the negative consequences of a 'heavy' object on your application?.

On your source reference: The implementation follows the opaque pointer (PIMPL) idiom. The pointed-to instance containing the implementation details is allocated on the heap.

Adolfo Rodriguez T gravatar image Adolfo Rodriguez T  ( 2014-10-16 07:56:34 -0500 )edit

I admit deciding what is heavy and what is not is up to the specific app. Having a PIMPL (which I had not noticed) makes it heavier though. Things are now clear to me. Thanks for your help!

arennuit gravatar image arennuit  ( 2014-10-16 08:47:09 -0500 )edit

So what action do you take when you have a 'heavy' object?. I'm just curiously wondering what kind of problems you're running into, or trying to avoid.

Adolfo Rodriguez T gravatar image Adolfo Rodriguez T  ( 2014-10-16 10:04:58 -0500 )edit

I have a vector of services to call in an order changing over time, and I was wondering whether to sort the vector directly or to switch to something like an STL view or maybe a list (I initially wanted to avoid these last two).

arennuit gravatar image arennuit  ( 2014-10-16 16:24:23 -0500 )edit

Ack. I'd still benchmark the naive implementation just to make sure no premature optimization is taking place.

Adolfo Rodriguez T gravatar image Adolfo Rodriguez T  ( 2014-10-17 02:35:57 -0500 )edit

After this discussion it really appears too risky to move the service objects around (because of potential things happening under the hood, in the PIMPL or in premature optim as you mentioned), so I'll keep it safe and go for a clean STL view or something close... Thanks a lot!

arennuit gravatar image arennuit  ( 2014-10-17 03:24:21 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2014-10-15 07:56:09 -0500

Adolfo Rodriguez T gravatar image

You can still create the service client outside your loop as an automatic variable (ie. on the stack), and prevent the constant and unnecessary creation/destruction of multiple clients within the loop.

The template parameter is used to determine the type of messages to be used by the client, that is, the request and response types. Its type should not influence whether you create client instances on the stack or heap, or inside/outside a loop.

edit flag offensive delete link more


Hello Adolfo, sure this is the preferred way to create objects outside the loop. Thanks for your answer.

arennuit gravatar image arennuit  ( 2014-10-15 08:21:15 -0500 )edit

Please mark the question as answered if you consider it resolved.

Adolfo Rodriguez T gravatar image Adolfo Rodriguez T  ( 2014-10-16 02:19:42 -0500 )edit

Not quite yet, but will do when ready. Thanks Adolfo ;)

arennuit gravatar image arennuit  ( 2014-10-16 07:15:31 -0500 )edit

Anyone interested in the answer should look at the first comment of the question. (the one comment by Adolfo).

arennuit gravatar image arennuit  ( 2014-10-16 08:48:30 -0500 )edit

answered 2014-10-16 07:26:30 -0500

paulbovbel gravatar image

updated 2014-10-16 07:27:30 -0500

You can use the sizeof() operator to determine the memory footprint of an object.

The accepted pattern would be to create the Service object, and do the .waitForExistence call once, outside the loop, to avoid the overhead. The overhead will be communication related - the nodehandle has to establish a connection with the service server through the ROS master.

edit flag offensive delete link more


Thanks Paul, that helps.

arennuit gravatar image arennuit  ( 2014-10-16 08:48:57 -0500 )edit

Question Tools

1 follower


Asked: 2014-10-15 07:21:52 -0500

Seen: 430 times

Last updated: Oct 16 '14