多线程并发查询百万数据的内存占用问题?-灵析社区

古典研发君

开启十个线程,每个线程都会去查询500W的数据。 @Test void testThread() throws InterruptedException { int size = 10; CountDownLatch countDownLatch = new CountDownLatch(size); for (int i = 0; i { testPage(); countDownLatch.countDown(); }); } countDownLatch.await(); } @Test void testPage(){ //查询出表中总记录数 Long total = orderMapper.selectCount(null); //每次分页读取的结果数 int fetchSize = 100000; // 分页优化参数,上次查询的最大ID int lastMaxId = 0; for (int i = 0; i orderLambdaQueryWrapper = new LambdaQueryWrapper(); orderLambdaQueryWrapper.gt(Order::getOrderId, lastMaxId); List records = orderMapper.selectPage(new Page(1, fetchSize), orderLambdaQueryWrapper).getRecords(); records.stream().forEach(System.out::println); //获取本次最大的Id lastMaxId = records.get(records.size() - 1).getOrderId(); } } 单独一个线程,堆内存占用500M。 ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20241125/5b2875f630d2ebbffaaa81ccc9f290db.png) 十个线程,堆内存占用最高也不过1400MB,为什么会这样呢?这些内存占用居然不会叠加的吗? ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20241125/b82a59f3062cf40dec875ec0fef86e2b.png)

阅读量:14

点赞量:0

问AI
题目说十个线程各查 500 万,代码里是十个现成分批查,一批 10 万,500 万和 10 万的差距还是挺大的 多线程环境下这么观察内存使用其实没法观察,不如把单线程查询需要多少内存算准了,多线程就做个乘法的事 另外如果想知道内存使用多少,直接看字段大小就能大概估算出来