{"record":{"id":"4a67c5defb3eb89d","repo":"apache/beam","slug":"heap-dump-hprof-already-existed-and-couldn-t-be-deleted","errorCode":null,"errorMessage":"heap_dump.hprof already existed and couldn't be deleted!","messagePattern":"heap_dump\\.hprof already existed and couldn't be deleted!","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"sdks/java/harness/src/main/java/org/apache/beam/fn/harness/status/MemoryMonitor.java","lineNumber":649,"sourceCode":"  }\n\n  /**\n   * Dump the current heap profile to a file in the given directory and return its name.\n   *\n   * <p>NOTE: We deliberately don't salt the heap dump filename so as to minimize disk impact of\n   * repeated dumps. These files can be of comparable size to the local disk.\n   */\n  private static synchronized File dumpHeap(File directory)\n      throws MalformedObjectNameException,\n          InstanceNotFoundException,\n          ReflectionException,\n          MBeanException,\n          IOException {\n\n    boolean liveObjectsOnly = false;\n    File fileName = new File(directory, \"heap_dump.hprof\");\n    if (fileName.exists() && !fileName.delete()) {\n      throw new IOException(\"heap_dump.hprof already existed and couldn't be deleted!\");\n    }\n\n    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();\n    ObjectName oname = new ObjectName(\"com.sun.management:type=HotSpotDiagnostic\");\n    Object[] parameters = {fileName.getPath(), liveObjectsOnly};\n    String[] signatures = {String.class.getName(), boolean.class.getName()};\n    mbs.invoke(oname, \"dumpHeap\", parameters, signatures);\n\n    if (java.nio.file.FileSystems.getDefault().supportedFileAttributeViews().contains(\"posix\")) {\n      Files.setPosixFilePermissions(\n          fileName.toPath(),\n          ImmutableSet.of(\n              PosixFilePermission.OWNER_READ,\n              PosixFilePermission.GROUP_READ,\n              PosixFilePermission.OTHERS_READ));\n    } else {\n      fileName.setReadable(true, true);\n    }","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/status/MemoryMonitor.java#L631-L667","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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"],"exampleFix":"// before: fixed filename reused each dump\nFile fileName = new File(directory, \"heap_dump.hprof\");\n// after: unique per-dump name avoids stale-file conflicts\nFile fileName = new File(directory,\n    \"heap_dump_\" + System.currentTimeMillis() + \".hprof\");","handlingStrategy":"try-catch","validationCode":"File dump = new File(dumpDir, \"heap_dump.hprof\");\nboolean writable = dumpDir.canWrite() && (!dump.exists() || dump.delete());","typeGuard":null,"tryCatchPattern":"try {\n  memoryMonitor.dumpHeap();\n} catch (IOException e) {\n  logger.warn(\"heap dump failed: {}\", e.getMessage()); // continue without dump\n}","preventionTips":["Use a dedicated, writable dump directory","Clean old hprof files after upload","Serialize dump triggers; don't run multiple monitors"],"tags":["java","io","heap-dump","filesystem"],"backgroundTag":"file-write-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}