0

I have a ramdrive created through fstab as

tmpfs       /mnt/ram tmpfs   defaults,size=2048M   0 0

This space filled up to 86% so I deleted some large files.

Now the python view and the linux view differ on this disk usage. It looks like the python view does not account for the deleted files.

>>> from os import system
>>> import psutil
>>> import shutil
>>> cmd = 'du -s /mnt/ram/'
>>> system(cmd)
632 /mnt/ram/
0
>>> psutil.disk_usage('/mnt/ram/')
sdiskusage(total=2147483648, used=1862307840, free=285175808, percent=86.7)
>>> shutil.disk_usage('/mnt/ram/')
usage(total=2147483648, used=1862500352, free=284983296)
Stephen Boston
  • 971
  • 1
  • 12
  • 23
  • 1
    Were the large files possibly still in use by some program? The disk space won't actually be reclaimed as long as the files are open, but `du` won't show that because it's only looking at files that still have a directory entry. – jasonharper Jul 16 '23 at 14:08
  • 1
    I think `psutil.disk_usage()` corresponds more to `df`, not `du`. The difference is due to deleted files that still take space because a process has them open. – Barmar Jul 16 '23 at 14:21
  • See https://unix.stackexchange.com/questions/9612/why-is-there-a-discrepancy-in-disk-usage-reported-by-df-and-du – Barmar Jul 16 '23 at 14:22
  • @jasonharper They were log files that logrotate had gzipped. – Stephen Boston Jul 16 '23 at 15:48
  • `du` lists total size of accessible files, `psutil.disk_usage` lists block statistics. They're not _supposed_ to be the same thing. If you want block device statistics from a command line, use `df`. – Charles Duffy Jul 16 '23 at 16:29
  • (pretend I said "filesystem statistics"; the filesystem is working at the block layer, and generally managing statistics that way -- so f/e, `du` might double-count two files that share the same blocks due to reflinks, or fail to count files that are deleted but not yet unreferenced; whereas `df` and `disk_usage()` ask the filesystem for its accounting instead of totaling up files, and so have numbers that reflect the corner cases) – Charles Duffy Jul 16 '23 at 16:36

0 Answers0