apache/beam · error · IOException
heap_dump.hprof already existed and couldn't be deleted!
Error message
heap_dump.hprof already existed and couldn't be deleted!
What it means
MemoryMonitor.dumpHeap throws this IOException when a previous heap_dump.hprof exists in the target directory and cannot be deleted, blocking creation of a new heap dump. This guards the HotSpotDiagnosticMXBean dump call against writing to a stale or locked file.
Solutions
- Delete heap_dump.hprof manually or ensure nothing holds it open, then retry
- Point the memory monitor's dump directory at a writable, dedicated location
- Serialize dump triggers so only one monitor runs dumpHeap at a time
- Check filesystem permissions / mount read-only status of the dump directory
Example fix
// before: fixed filename reused each dump
File fileName = new File(directory, "heap_dump.hprof");
// after: unique per-dump name avoids stale-file conflicts
File fileName = new File(directory,
"heap_dump_" + System.currentTimeMillis() + ".hprof"); Defensive patterns
Strategy: try-catch
Validate before calling
File dump = new File(dumpDir, "heap_dump.hprof"); boolean writable = dumpDir.canWrite() && (!dump.exists() || dump.delete());
Try / catch
try {
memoryMonitor.dumpHeap();
} catch (IOException e) {
logger.warn("heap dump failed: {}", e.getMessage()); // continue without dump
} Prevention
- Use a dedicated, writable dump directory
- Clean old hprof files after upload
- Serialize dump triggers; don't run multiple monitors
When it happens
Trigger: Triggering a heap dump (e.g. from a memory alarm handler) when heap_dump.hprof already exists in the dump directory and File.delete() fails — typically because the file is locked by another process or the process lacks write permission on it/directory.
Common situations: A previous OOM dump still present and being read/uploaded by another tool; dumping on a read-only or container-mounted volume; two MemoryMonitor instances racing on the same directory; OS file locks (Windows).
Understand the failure class
Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.
Related errors
- Could not create a temporary directory for storing…
- Could not create a temporary directory for storing…
- Could not create the new requirements file
- Encountered exception creating directory for heap dumps…
- Error matching file spec
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/4a67c5defb3eb89d.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/java/harness/src/main/java/org/apache/beam/fn/harness/status/MemoryMonitor.java:649
}
/**
* Dump the current heap profile to a file in the given directory and return its name.
*
* <p>NOTE: We deliberately don't salt the heap dump filename so as to minimize disk impact of
* repeated dumps. These files can be of comparable size to the local disk.
*/
private static synchronized File dumpHeap(File directory)
throws MalformedObjectNameException,
InstanceNotFoundException,
ReflectionException,
MBeanException,
IOException {
boolean liveObjectsOnly = false;
File fileName = new File(directory, "heap_dump.hprof");
if (fileName.exists() && !fileName.delete()) {
throw new IOException("heap_dump.hprof already existed and couldn't be deleted!");
}
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName oname = new ObjectName("com.sun.management:type=HotSpotDiagnostic");
Object[] parameters = {fileName.getPath(), liveObjectsOnly};
String[] signatures = {String.class.getName(), boolean.class.getName()};
mbs.invoke(oname, "dumpHeap", parameters, signatures);
if (java.nio.file.FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
Files.setPosixFilePermissions(
fileName.toPath(),
ImmutableSet.of(
PosixFilePermission.OWNER_READ,
PosixFilePermission.GROUP_READ,
PosixFilePermission.OTHERS_READ));
} else {
fileName.setReadable(true, true);
}View on GitHub (pinned to 12126d8942)