Size() vs ls -la vs du -h which one is correct size?

They are all correct, they just show different sizes.

  • ls shows size of the file (when you open and read it, that's how many bytes you will get)
  • du shows actual disk usage which can be smaller than the file size due to holes
  • size shows the size of the runtime image of an object/executable which is not directly related to the size of the file (bss uses no bytes in the file no matter how large, the file may contain debugging information that is not part of the runtime image, etc.)

If you want to know how much RAM/ROM an executable will take excluding dynamic memory allocation, size gives you the information you need.