All Products
Search
Document Center

:What do I do if disk usage in the df command output is inconsistent with that in the du command output for a Linux instance?

Last Updated:Dec 26, 2022

This topic describes how to resolve the issue that disk usage in the df command output is inconsistent with that in the du command output for a Linux instance.

Problem description

  1. Run the following df command on a Linux instance to check file system usage:

    df -h

    A command output similar to the following one is displayed. In this command output, you can find that the/dev/xvdb1 partition takes up 27 GB of space and is mounted at /opt. image

  2. Run the following commands in sequence to switch to the /opt directory and then check file system usage. In the du command output, you can find the /dev/xvdb1 partition takes up 2.4 GB of space, which is inconsistent with the df command output.

    cd /opt
    du -sh

Cause

A large number of leftover files that have not been cleaned up properly (zombies) may exist in the instance, and some running processes are using the handles of these files.

The du command uses fstat system calls to collect data about files and can run across multiple partitions. The du command does not collect data about deleted files. The df command uses statfs system calls to directly read the superblock information of partitions and obtain partition usage. If running processes hold the handles of deleted files, the superblock information of partitions does not change and the df command still collects data about deleted files. As a result, data in du command output is inconsistent with that in the df command output.

Solutions

You can use one of the following methods to release the handles of files that are in the deleted state and clean up the files:

Method 1: Run the kill command

  1. Connect to the instance.

    For more information, see Connect to a Linux instance by using a password or key.

  2. Run the following commands in sequence to switch to the /opt directory and then query the files that are in the deleted state:

    cd /opt
    lsof |grep deleted
  3. Run the following command to terminate processes based on the process IDs (PIDs) displayed in the lsof command output.

    Warning

    The kill command terminates the processes that are running on the instance. Proceed with caution.

    kill <$PID>
    Note

    Set <$PID> to a PID displayed in the lsof command output.

Method 2: Restart the instance

When you restart the instance, all processes that are running on the instance exit, and the handles of deleted files in use are released.