1

I have a problem that needs to count the length of each ROS topic msg.

I modified rostopic(/opt/ros/smart-ros/lib/python2.7/dist-packages/rostopic / __ init__.py):

Class CallbackEcho(object) ->
Def callback (self, data, callback_args, current_time = None):
    Print('message length =',sys.getsizeof(data)) #statics msg length

But when the message contains a vector or struct list, the length is incorrect.

Please help solve this problem?

Thanks.

wei hu
  • 13
  • 3
  • rosmsg show XXXX/XXXX std_msgs/Header header uint32 seq time stamp string frame_id bool a float64 c float64 m float64 g but the message length is 96. – wei hu Feb 03 '19 at 03:16
  • I am not sure what you mean by the "length" of a message. Do you mean if the message has a vector on it, you want to know how many elements are in it? In any event, you shouldn't be monkeying with the ROS source code, there is probably a way to do what you want in some client code. – adamconkey Feb 07 '19 at 04:40

1 Answers1

0

The problem is the behaviour of sys.getsizeof:

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

This means only the size of the reference to a list is used to calculate the object size.

There are already a few questions dealing with this:

The solution is to use Pympler and its module asizeof which provides a function to the required calculation:

Function asizeof calculates the combined (approximate) size of one or several Python objects in bytes

After installing the package using pip

pip install pympler

and importing it to your code like

from pympler.asizeof import asizeof

you are able to print the correct object size in your callback like

print('Size: ' + str(asizeof(data)))
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • Thanks in advance, but my python version is 2.7. nvidia@p22:~$ pip install pympler Collecting pympler Could not find a version that satisfies the requirement pympler (from versions: ) No matching distribution found for pympler You are using pip version 8.1.1, however version 19.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. – wei hu Feb 11 '19 at 02:11
  • nvidia@p22:~$ pip install --upgrade pip Collecting pip Could not find a version that satisfies the requirement pip (from versions: ) No matching distribution found for pip You are using pip version 8.1.1, however version 19.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. – wei hu Feb 11 '19 at 04:07
  • My answer is compatible with Python 2.7. You need to upgrade pip and install Pympler. – Fruchtzwerg Feb 11 '19 at 05:32
  • Thank you in advance, pympler was installed successfully. pip upgrade failed,Use this command: pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org Pympler – wei hu Feb 11 '19 at 08:15
  • I have some doubts. I would like to ask some specific questions. Can you leave a mailbox? – wei hu Feb 11 '19 at 10:53
  • So what are your doubts? Extend your question, comment or think about asking a new question. – Fruchtzwerg Feb 11 '19 at 11:07
  • def callback(self, data, callback_args, current_time=None): print('asizeof(data)=', asizeof(data)) print ('data.__sizeof__()=', data.__sizeof__()) print ('len(str(data))=', len(str(data))) print ('sys.getsizeof(data)=', sys.getsizeof(data)) asizeof(data)= 114696 data.__sizeof__()= 64 len(str(data))= 72327 sys.getsizeof(data)= 96 – wei hu Feb 11 '19 at 11:12
  • In fact, asizeof (data) is bigger than len (str (data)), is it correct? – wei hu Feb 11 '19 at 12:07