From the CFITSIO manual:
All the data in a variable length field is written into an area called the heap which follows the main fixed-length FITS binary table. The size of the heap, in bytes, is specified by the PCOUNT keyword in the FITS header. When creating a new binary table, the initial value of PCOUNT should usually be set to zero. CFITSIO will recompute the size of the heap as the data is written and will automatically update the PCOUNT keyword value when the table is closed. When writing variable length data to a table, CFITSIO will automatically extend the size of the heap area if necessary, so that any following HDUs do not get overwritten.
Normally, when a vector in a variable length array column is rewritten, the previously written array remains in the heap as wasted unused space. This routine [fits_compress_heap] will repack the arrays that are still in use, thus eliminating any bytes in the heap that are no longer in use. Note that if several vectors point to the same bytes in the heap, then this routine will make duplicate copies of the bytes for each vector, which will actually expand the size of the heap.ftcmpheap may be useful after performing operations on variable-length columns in a FITS binary table. For example, the ftcopy task could be used to delete variable-length columns from a table, but currently this leaves the PCOUNT keyword and file size unchanged (leading to warnings from e.g. ftverify). Running ftcmpheap on the affected extension frees up the unused space and fixes the PCOUNT keyword.
1. Compress the heap in the third extension (named "EVENTS") of a FITS binary table:
ftcmpheap table.fits+3or
ftcmpheap 'table.fits[3]'or
ftcmpheap 'table.fits[EVENTS]'or, if there is only one table HDU in the file:
ftcmpheap table.fits