gpt4 book ai didi

java - 如何从 Java 生成大型(1GB)示例 gc 日志文件

转载 作者:行者123 更新时间:2023-12-02 02:16:03 25 4
gpt4 key购买 nike

我需要大型 GC 日志文件来对我的日志分析器应用程序进行基准测试。请建议生成示例 GC 日志文件的最佳方法。

我发现下面这段小代码,每 100 毫秒分配 2MB 内存

class MyTask extends TimerTask{

static final int MB = 1024 * 1024; //GB= 1024 * 1024 * 1024
@Override
public void run() {
byte[] a1 = new byte[2 * MB];
a1[1] = 1;
Runtime runtime = Runtime.getRuntime();
System.out.print("total :" + (runtime.totalMemory() / 1024)+ " KB\n");
long free = runtime.freeMemory() / 1024;
System.out.print("free:" + free+ " KB\n");
}
}

public class MemoryAllocationTest {
static public void main(String[] arg){
Timer timer = new Timer();
timer.schedule(new MyTask(), 100, 100);
}
}

现在我可以使用 VM 选项生成 gc 日志文件

-XX:+DisableExplicitGC
-XX:+PrintGCDetails
-XX:+PrintGCApplicationStoppedTime
-XX:+PrintGCApplicationConcurrentTime
-XX:+PrintGCDateStamps
-Xloggc:gclog.log

生成 200-300 Mb 的文件需要几天时间。但我需要 >= 1 GB 文件大小以及不同类型/品种的 gc 事件。

我怎样才能做到这一点?

最佳答案

您可以强制运行java垃圾收集器Runtime

删除-XX:+DisableExplicitGC选项

/**
* Use less memory, for example jvm options -Xms64m -Xmx64m
*/
public class GarbageGenerator {

private static String generateGarbage() {
StringBuilder b = new StringBuilder();
for (long i = 0; i < 100_000; i++) {
b.append(Long.valueOf(i));
}
return b.toString();
}

private static final long MAX = 100_000_000L;
private static final long TP = 10_000_000L;

public static void main(String[] args) {
System.out.println("Garbage generation started");
// we need more garbage, so not an ArrayList
final List<String> list = new LinkedList<>();
long done = 0L;
long i = 0L;
final Lock lock = new ReentrantLock();
int cpuLogicalCores = Runtime.getRuntime().availableProcessors();
ExecutorService exec = Executors.newFixedThreadPool(cpuLogicalCores);
do {
final CountDownLatch countDown = new CountDownLatch(cpuLogicalCores);
for (int k = 0; k < cpuLogicalCores; k++) {
exec.execute(() -> {
String garbage = generateGarbage();
lock.lock();
try {
list.add(garbage);
} finally {
lock.unlock();
}
countDown.countDown();
});
}
try {
countDown.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
list.clear();
++i;
if (0 == (i % TP))
System.out.println("%" + Long.valueOf(done += 10L));
// force run gc, check that not disabled by jvm command line options
Runtime.getRuntime().gc();
} while (i < MAX);
exec.shutdown();
try {
exec.awaitTermination(365, TimeUnit.DAYS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Garbage generation end");
}

}

关于java - 如何从 Java 生成大型(1GB)示例 gc 日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49279813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com