Quick revision
The short answer
There are usually several causes at once. maxHistory counts periods, not files, so with size-and-time rolling and a small maxFileSize, one day can produce hundreds of files, and only totalSizeCap limits the total. Two processes or two appenders writing the same file pattern break rolling and cleanup; an external logrotate that moves or deletes files Logback still holds open leaves deleted-but-open files that keep using disk; and a changed fileNamePattern leaves old archives the cleaner no longer recognises. We fix each in turn, with a size cap, one writer per file, one rotation mechanism, a one-time manual cleanup, and disk monitoring, because a full disk can stop the application itself, not just its logging.
What to remember
In this answer, we traced a full disk to four causes. maxHistory counts periods, so small files on busy days multiply, and totalSizeCap is the real limit. Two writers on one file pattern break rolling, so each process needs its own file. External logrotate on files Logback holds open leaves deleted-but-open files, found with lsof +L1 and freed by truncation or restart, and fixed by using one rotation mechanism. A changed fileNamePattern leaves archives the cleaner cannot see. We finished with disk and directory monitoring and a separate volume for logs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
There are usually several causes at once. maxHistory counts periods, not files, so with size-and-time rolling and a small maxFileSize, one day can produce hundreds of files, and only totalSizeCap limits the total. Two processes or two appenders writing the same file pattern break rolling and cleanup; an external logrotate that moves or deletes files Logback still holds open leaves deleted-but-open files that keep using disk; and a changed fileNamePattern leaves old archives the cleaner no longer recognises. We fix each in turn, with a size cap, one writer per file, one rotation mechanism, a one-time manual cleanup, and disk monitoring, because a full disk can stop the application itself, not just its logging.
Why this question is asked
A full disk is one of the most frustrating outages, because the configuration looks correct: the rolling policy says keep 30, and yet there are thousands of files. Worse, deleting files may not free any space. Interviewers ask this debugging question because it rewards a clear model of how Logback rolls and cleans, and of how Unix handles deleted files. In this answer, we will recap how cleanup works, then go through each possible cause with how to recognise it and how to fix it, and finish with monitoring.
1A Quick Recap of How Logback Cleans Up
A rolling appender writes to an active file and, at each rollover, turns it into an archive named from the fileNamePattern. After the rollover, an archive remover looks for files that match the current pattern and deletes those older than maxHistory periods, and if totalSizeCap is set, it also deletes the oldest matching archives until the total is under the cap. Two words in that sentence matter for this problem: periods and matching. Cleanup only counts periods, and it only sees files that match the pattern as it is now.
On the operating system side, deleting a file in Linux only removes its name from the directory. The disk space is freed when the last process that has the file open closes it. That is why lsof can show files marked (deleted) that still take space.
2Cause 1: maxHistory Counts Periods, Not Files
Suppose the policy is daily with maxFileSize 10MB and maxHistory 30, and the service writes 4 GB on a busy day. That one day produces about 400 files of 10 MB, and maxHistory keeps 30 such days. That can mean 12,000 files and 120 GB, exactly within the policy as written.
The fix is totalSizeCap, which bounds the total whatever the daily volume, set below the free space we can spare. A larger maxFileSize also reduces the number of files. And we should ask why the service writes 4 GB a day; often a logger left at DEBUG is the real cause.
3Cause 2: Two Writers on the Same Files
If two processes, for example two instances of the service on one VM started from the same configuration, or two appenders in one process, write the same file and roll with the same pattern, rolling breaks. One process renames the file while the other keeps writing into the renamed file, archives get overwritten or skipped, and each process's cleaner sees a confusing set of files. Disk usage then grows in ways the policy does not predict.
Each process needs its own file name, for example by including an instance id in the path through a system property: logs/app-${INSTANCE}.log. Logback's prudent mode lets several processes write safely to one file using file locks, but it is slow and limits the rolling options (it does not support compression with size-and-time rolling, for instance), so separate files are the better fix.
4Cause 3: logrotate and Deleted-But-Open Files
This is the cause behind the lsof output. Someone added an external logrotate rule for the log directory as a safety net. logrotate renames or deletes app.log, but Logback still holds the file open and keeps writing to it. The name is gone, so the file does not appear in ls or du, but its blocks are still in use, and they grow. df shows the disk full, du shows much less, and the difference is deleted files that are still open.
| What we see | Likely cause | Fix |
|---|---|---|
| Hundreds of files per day | maxHistory counts periods | totalSizeCap, larger maxFileSize |
| Missing or overwritten archives | two writers on one pattern | one file name per process |
| df full, du small, lsof (deleted) | external rotation of an open file | one rotation mechanism; truncate or restart |
| Old names never deleted | fileNamePattern changed | remove once by hand |
Truncating through /proc frees the space immediately, and a restart of the service also does it. The real fix is to use one rotation mechanism per file: either Logback rolls and logrotate leaves the directory alone, or logrotate rotates and Logback uses a plain FileAppender. If logrotate must be used, its copytruncate option copies the file and then truncates it in place, which keeps the descriptor valid, but it has its own risk, described in the FAQ.
5Cause 4: A Changed fileNamePattern
The cleaner only deletes files that match the current pattern. If last year the pattern was logs/app.%d{yyyy-MM-dd}.log and now it is logs/app-%d{yyyy-MM-dd}.%i.log.gz, every archive from the old pattern is invisible to the cleaner and stays forever. The same happens when an application is renamed or moves directory. Logback also only looks back a limited number of periods when cleaning, so archives left behind after a long shutdown can survive too; cleanHistoryOnStart helps there (check your version).
The fix is a one-time manual cleanup of the old names, after confirming nothing needs them, and a note in the change process: whenever the pattern changes, clean up the old archives.
6Monitoring So It Does Not Happen Again
Finally, we should see the disk filling long before it is full. A disk usage alert on the volume at, say, 80 percent, and a separate metric for the size of the log directory, catch both runaway logging and broken cleanup. The difference between df and du is a useful extra signal for deleted-but-open files.
This matters beyond logging. When the disk is full, the application cannot write temporary files, databases on the same volume stop, and Logback's own writes fail. Logging should never be the reason a service goes down, so the log directory belongs on its own volume or partition where possible, so that a logging mistake fills only that volume.
7Follow-up questions
Why does logrotate with copytruncate risk losing lines?
Because copying and truncating are two separate steps. Lines that the application writes after the copy has finished but before the truncate happens are in neither the archive nor the new file, so they are lost. On a busy service, that window can hold hundreds of lines. It also wastes I/O, because the whole file is copied each time.
Why does restarting the service free the space?
Because the JVM closes all its file descriptors when it exits, and Linux frees the blocks of a deleted file once no process has it open. That is why the problem often disappears after a deployment and returns a few weeks later.
Could the async appender be involved?
Not in the disk usage itself, but an async appender hides the moment when writes start failing, because callers do not wait for the write. When the disk is full, events are lost silently unless we monitor the status messages or dropped events.
8Conclusion
In this answer, we traced a full disk to four causes. maxHistory counts periods, so small files on busy days multiply, and totalSizeCap is the real limit. Two writers on one file pattern break rolling, so each process needs its own file. External logrotate on files Logback holds open leaves deleted-but-open files, found with lsof +L1 and freed by truncation or restart, and fixed by using one rotation mechanism. A changed fileNamePattern leaves archives the cleaner cannot see. We finished with disk and directory monitoring and a separate volume for logs.
Open with
I would look for several causes, because they often combine. First, maxHistory counts periods, not files. With size-and-time rolling and a small maxFileSize, a busy day can produce hundreds of files, and maxHistory 30 keeps thirty such days, so the fix is totalSizeCap, a larger file size and probably lower log levels.
Then, if they want more
Second, two processes or two appenders writing the same file pattern break rolling and cleanup, so each instance needs its own file name; prudent mode exists but is slow and limits options.
Third, the lsof output points to external rotation: if logrotate renames or deletes a file that Logback still has open, the name disappears but the space is not freed, so df shows full while du shows little. lsof +L1 lists those files.
I can free the space immediately by truncating through /proc/pid/fd or by restarting, but the real fix is one rotation mechanism per file, either Logback's rolling or logrotate, not both. copytruncate keeps the descriptor valid but can lose lines written between the copy and the truncate.
Fourth, if the fileNamePattern was ever changed, the cleaner only matches the new pattern, so old archives stay forever and need one manual cleanup. Finally, I would add alerts on disk usage and log directory size, and put logs on their own volume, because a full disk can stop the application itself, not just its logging.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "On a VM, a service's disk filled up although the rolling policy says maxHistory 30. The...": https://scrivio-interview-library.netlify.app/java/java-logging/67c4c1c78d/