(String[] args)
| 8 | // 随机数; 记得这里可以设置随机数种子; |
| 9 | private static Random random = new Random(); |
| 10 | public static void main(String[] args) { |
| 11 | // 当前毫秒时间戳 |
| 12 | long startMillis = System.currentTimeMillis(); |
| 13 | // 持续运行毫秒数; 可根据需要进行修改 |
| 14 | long timeoutMillis = TimeUnit.SECONDS.toMillis(1); |
| 15 | // 结束时间戳 |
| 16 | long endMillis = startMillis + timeoutMillis; |
| 17 | LongAdder counter = new LongAdder(); |
| 18 | System.out.println("正在执行..."); |
| 19 | // 缓存一部分对象; 进入老年代 |
| 20 | int cacheSize = 2000; |
| 21 | Object[] cachedGarbage = new Object[cacheSize]; |
| 22 | // 在此时间范围内,持续循环 |
| 23 | while (System.currentTimeMillis() < endMillis) { |
| 24 | // 生成垃圾对象 |
| 25 | Object garbage = generateGarbage(100*1024); |
| 26 | counter.increment(); |
| 27 | int randomIndex = random.nextInt(2 * cacheSize); |
| 28 | if (randomIndex < cacheSize) { |
| 29 | cachedGarbage[randomIndex] = garbage; |
| 30 | } |
| 31 | } |
| 32 | System.out.println("执行结束!共生成对象次数:" + counter.longValue()); |
| 33 | } |
| 34 | |
| 35 | // 生成对象 |
| 36 | private static Object generateGarbage(int max) { |
nothing calls this directly
no test coverage detected