{"record":{"id":"0ddf7633a28c4d88","repo":"Tencent/matrix","slug":"free-space-freespace-less-than-clean-threshold","errorCode":null,"errorMessage":"free space($freeSpace) less than $CLEAN_THRESHOLD, skip dump hprof","messagePattern":"free space\\(\\$freeSpace\\) less than \\$CLEAN_THRESHOLD, skip dump hprof","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-resource-canary/matrix-resource-canary-android/src/main/java/com/tencent/matrix/resource/dumper/HprofFileManager.kt","lineNumber":80,"sourceCode":"    private fun File.reserve() {\n        if (!exists() && (!mkdirs() || !canWrite())) {\n            \"fialed to create new hprof file since path: $absolutePath is not writable\".let {\n                MatrixLog.e(TAG, it)\n                throw FileNotFoundException(it)\n            }\n        }\n    }\n\n    private fun File.makeSureEnoughSpace(deleteSoon: Boolean) {\n        if (!isDirectory) {\n            return\n        }\n        lru()\n        if (freeSpace < CLEAN_THRESHOLD) {\n            listFiles()?.forEach { it.delete() }\n        }\n        if (freeSpace < if (deleteSoon) MIN_FREE_SPACE else CLEAN_THRESHOLD) {\n            throw FileNotFoundException(\"free space($freeSpace) less than $CLEAN_THRESHOLD, skip dump hprof\")\n        }\n    }\n\n    private fun File.lru() {\n        if (!isDirectory) {\n            return\n        }\n        val files = listFiles() ?: return\n        files.sortBy { it.lastModified() }\n        files.forEach {\n            MatrixLog.d(TAG, \"==> list sorted: ${it.absolutePath}, last mod = ${it.lastModified()}\")\n        }\n        if (files.size >= MAX_FILE_COUNT) {\n            files.take(files.size - MAX_FILE_COUNT + 1).forEach {\n                it.delete()\n            }\n        }\n    }","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-resource-canary/matrix-resource-canary-android/src/main/java/com/tencent/matrix/resource/dumper/HprofFileManager.kt#L62-L98","documentation":"Matrix ResourceCanary's HprofFileManager.makeSureEnoughSpace() verifies the hprof output directory has enough free disk space before dumping the heap. If free space is below the required threshold (CLEAN_THRESHOLD, or MIN_FREE_SPACE when deleteSoon is set), it first LRU-cleans old hprof files and then aborts the dump by throwing FileNotFoundException with this message. It is a deliberate guard to avoid filling the device disk with a multi-hundred-MB hprof file.","triggerScenarios":"Calling prepare()/makeSureEnoughSpace() when File.getUsableSpace() on the hprof directory returns less than CLEAN_THRESHOLD (or less than MIN_FREE_SPACE when deleteSoon=true), so dumping hprof is skipped. Happens after LRU cleanup fails to reclaim enough space.","commonSituations":"Low-end devices or emulators with a nearly full data partition; app cache dir crowded by other data so even after deleting old hprofs the threshold is not met; CLEAN_THRESHOLD configured too high for small test devices; repeated memory-leak tests that accumulate hprof files faster than cleanup.","solutions":["Free device storage (uninstall apps, clear caches) so free space exceeds CLEAN_THRESHOLD, then retry the leak dump","Lower the CLEAN_THRESHOLD / MIN_FREE_SPACE configuration to match your test device's capacity","Clear Matrix's own hprof directory (the LRU cleanup target) before triggering the dump","Run the leak test on a device/emulator with a larger data partition or expand emulator disk size"],"exampleFix":"// before\nMatrix.Builder().plugin(ResourcePlugin(ResourceConfig.Builder().setHprofCleanThreshold(200 * 1024 * 1024).build()))\n// after\nMatrix.Builder().plugin(ResourcePlugin(ResourceConfig.Builder().setHprofCleanThreshold(50 * 1024 * 1024).build())) // fits small test device","handlingStrategy":"try-catch","validationCode":"// Kotlin: check free space before triggering a leak dump\nval dir = File(matrixHprofDir)\nval freeBytes = dir.usableSpace\nif (freeBytes < CLEAN_THRESHOLD) {\n    dir.listFiles()?.forEach { it.delete() } // LRU-clean old hprofs first\n    if (dir.usableSpace < MIN_FREE_SPACE) {\n        // skip dump / alert: not enough space\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    hprofFileManager.makeSureEnoughSpace(deleteSoon = false)\n    // proceed with dump\n} catch (e: FileNotFoundException) {\n    Log.w(\"Matrix\", \"Skipped hprof dump: insufficient free space\", e)\n    // report to monitoring, free storage, or retry later\n}","preventionTips":["Monitor device free storage and skip/defer leak tests when below threshold","Periodically purge Matrix's hprof directory of stale dumps","Configure CLEAN_THRESHOLD/MIN_FREE_SPACE appropriately for your smallest supported device","Test on low-storage devices before shipping leak-monitoring to production"],"tags":["android","disk-space","memory-leak","hprof"],"backgroundTag":"insufficient-disk-space","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}