From 9d638d54643eb2e0593a4480f78a939fbd56ce00 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Sat, 11 Apr 2026 10:51:01 +0700 Subject: [PATCH] feat(content): add post about fixing Gradle test OOM with maxHeapSize --- content/post/2026/04/08/index.md | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 content/post/2026/04/08/index.md diff --git a/content/post/2026/04/08/index.md b/content/post/2026/04/08/index.md new file mode 100644 index 0000000..cbb4f7a --- /dev/null +++ b/content/post/2026/04/08/index.md @@ -0,0 +1,36 @@ +--- +title: "Fix Gradle test crash do hết RAM: config maxHeapSize" +date: 2026-04-08 +tags: ["AI-Assisted", "Gradle", "Java", "Testing"] +categories: ["Sharing"] +--- + +Một mẹo nhỏ mình tìm ra lâu rồi nhưng giờ mới có dịp chia sẻ lại. + +## Vấn đề + +Khi chạy `./gradlew test`, thỉnh thoảng test bị crash giữa chừng với `OutOfMemoryError` hoặc worker process "disappeared unexpectedly". Chạy lại thì khi pass khi fail, rất flaky. + +## Nguyên nhân + +Gradle chạy test trong một JVM worker riêng, và heap size mặc định khá nhỏ (thường chỉ 512MB). Test suite nặng (Spring context, H2, nhiều test data...) sẽ dễ đụng trần. + +## Cách fix + +Thêm vào `build.gradle`: + +```groovy +test { + maxHeapSize = "1024m" +} +``` + +Hoặc `build.gradle.kts`: + +```kotlin +tasks.test { + maxHeapSize = "1024m" +} +``` + +Xong. Nếu vẫn crash thì tăng lên `"2048m"`.