Skip to content

Commit b9b0cd6

Browse files
committed
thread arena
1 parent 4a108dd commit b9b0cd6

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,62 @@
11
package jvm.oom;
22

3+
import jvm.MallocBench;
4+
import lombok.extern.slf4j.Slf4j;
5+
6+
import java.nio.ByteBuffer;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.concurrent.ExecutorService;
10+
import java.util.concurrent.Executors;
11+
import java.util.concurrent.ThreadLocalRandom;
12+
import java.util.concurrent.TimeUnit;
13+
314
/**
415
* https://github.com/Kuangcp/Note/blob/master/Linux/Base/LinuxBase.md#glibc-ptmalloc2
516
*
617
* @author Kuangcp
718
* 2025-05-15 21:59
19+
* @see MallocBench
820
*/
21+
@Slf4j
922
public class DirectMemoryGlibcGrow {
1023
//TODO 模拟出Glibc thread arena 占用大的情况
1124
//TODO 替换 jemalloc 后查看是否有问题
25+
26+
27+
// https://medium.com/@daniyal.hass/how-glibc-memory-handling-affects-java-applications-the-hidden-cost-of-fragmentation-8e666ee6e000
28+
public static void fragment() throws InterruptedException {
29+
List<ByteBuffer> buffers = new ArrayList<>();
30+
// Allocate large buffers and release them to see memory impact
31+
for (int i = 0; i < 50; i++) {
32+
int delta = ThreadLocalRandom.current().nextInt(100) + 33;
33+
System.out.print(delta + ", ");
34+
buffers.add(ByteBuffer.allocateDirect(delta * 1024 * 1024)); // 100 MB each
35+
36+
buffers.remove(0);
37+
// if (i % 2 == 0) buffers.remove(0); // Simulate variable memory usage
38+
}
39+
40+
System.out.println("Memory allocations complete. Check OS memory usage.");
41+
}
42+
43+
public static void loopFragment() throws InterruptedException {
44+
TimeUnit.SECONDS.sleep(10);
45+
ExecutorService xo = Executors.newFixedThreadPool(3);
46+
Runnable run = () -> {
47+
try {
48+
for (int i = 0; i < 1000; i++) {
49+
TimeUnit.SECONDS.sleep(3);
50+
fragment();
51+
System.out.println("loop " + i);
52+
}
53+
} catch (Exception e) {
54+
log.error("", e);
55+
}
56+
};
57+
xo.submit(run);
58+
xo.submit(run);
59+
60+
}
61+
1262
}

class/src/main/java/jvm/oom/DirectMemoryOOM.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class DirectMemoryOOM {
2727
// 注意: -XX:MaxDirectMemorySize参数只对由DirectByteBuffer分配的内存有效限制,对Unsafe直接分配的内存无效
2828

2929
/**
30-
* C语言malloc申请的也是虚拟内存,没有设置值的话操作系统不会分配物理内存
30+
* C语言malloc申请的是虚拟内存,没有设置值的话操作系统不会分配物理内存
3131
*
3232
* @see java.nio.DirectByteBuffer#DirectByteBuffer(int)
3333
* <p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package jvm.oom;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author Kuangcp
7+
* 2025-05-19 21:36
8+
*/
9+
public class DirectMemoryGlibcGrowTest {
10+
11+
@Test
12+
public void testFragment() throws Exception {
13+
DirectMemoryGlibcGrow.fragment();
14+
Thread.currentThread().join();
15+
}
16+
17+
/**
18+
* pmap -x 259296 | jvm-tool -s 查看块数量
19+
*/
20+
@Test
21+
public void testLoopFragment() throws Exception {
22+
DirectMemoryGlibcGrow.loopFragment();
23+
Thread.currentThread().join();
24+
}
25+
}

0 commit comments

Comments
 (0)