当前位置:网站首页>Generate OOM records in a production environment. Conclusion: Don't be lazy to query useless fields unless you are completely sure.
Generate OOM records in a production environment. Conclusion: Don't be lazy to query useless fields unless you are completely sure.
2022-07-30 20:51:00 【Bright de Xuan rice 61】
1、背景
When I got off work today, I saw a message from the technical director in the company's WeChat group,The content is that the company website is down due to code execution,Then he asked the boss of the website department to immediately find the corresponding one in the background logbug来源,I have been involved in this project before seeing it,This is the log message I wrote,Then the cause of the problem is that the collection of objects queried from the database is too large,Directly burst the memory,It is said to have been reached20G,Then the website went down
2、伪代码
This time, the following pseudo code is used to illustrate the code logic
public class Test {
// Mock information database
private static List<A> dataList = new ArrayList<>();
static {
imitateMysql();
}
/** * Simulate pagination of output data */
public static void main(String[] args) {
// 总数据量
int total = dataList.size();
System.out.println("》》》》》》The total amount of data to be processed:" + total + ";当前时间:" + DateUtil.now(DateUtil.DEFAULT_DATE_FORMAT));
// 页面容量
int pageSize = 100;
// 总页码
int totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
// Paginate data
for (int currentPage = 1; currentPage <= totalPage; currentPage++) {
List<A> pageList = page(currentPage, pageSize);
for (A entity : pageList) {
indexData(entity.getId());
}
// 回收资源
pageList = null;
}
System.out.println("》》》》》》All data has been processed");
}
/** * Generate mock database data */
private static void imitateMysql() {
for (int i = 0; i < 210000; i++) {
A a = new A();
a.setId(UUIDUtil.create().toString().replace("-", ""));
a.setContent("Imagine for yourself that there is a lot of text here");
dataList.add(a);
}
}
/** * 根据数据idindex data toElasticsearch,No more simulation here * @param id 数据id */
private static void indexData(String id) {
// 根据数据id查询数据
// index data toElasticsearch
System.out.println("index data toElasticsearch完成,id = " + id);
}
/** * Simulate paging to get data * @param currentPage 当前页码 * @param pageSize 页面容量 * @return */
private static List<A> page(int currentPage, int pageSize) {
List<A> pageList = new ArrayList<>(pageSize);
int start = (currentPage - 1) * pageSize;
int end = start + pageSize - 1;
for (int i = 0; i < dataList.size(); i++) {
if (i >= start && i <= end) {
pageList.add(dataList.get(i));
}
}
return pageList;
}
}
3、分析
我们来分析一下上面的代码,Paging to obtain data and recycle resources are all to reduce memory space usage,但是我们忽略了一个事实,That is, in a real environmentcontent内容巨大,因此可能造成pageList很大,In severe cases even lead toOOM,Although we empty the paginated data collection,But the garbage collector does not complete garbage collection so quickly
For operations that paginate data from the database,It is completely possible to get data only by pagingid集合,without getting other fields,The benefit of this is that paginated queries are faster,占用内存空间更小,And I didn't think about it that much at the time,Then lazily used the company's default pagination method to get all the field data,导致了OOM
4、结论
when fetching data from the database,Get only the data you need as much as possible,Don't be lazy and get useless data,Smaller will slow down the efficiency of the program,Larger causes the programOOM,影响客户正常使用,造成生产事故
边栏推荐
猜你喜欢

Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上

数字货币期货现货交易技巧,把握关键进场的买入点!(纯干货)

KingbaseESV8R6 snapshot too old的配置和测试

第03章 用户和权限管理【1.MySQL架构篇】【MySQL高级】

Oblique document scanning and character recognition (opencv, coordinate transformation analysis)

365天挑战LeetCode1000题——Day 044 按公因数计算最大组件大小 并查集

flowable工作流所有业务概念

IDEA2018.3.5取消双击Shift快捷键

WeChat reading, export notes

WPS怎么独立窗口显示?wps单独窗口显示怎么操作?
随机推荐
CDH集群spark-shell执行过程分析
【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码
Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]
【考研词汇训练营】Day18 —— amount,max,consider,account,actual,eliminate,letter,significant,embarrass,collapse
文字的选择与排版
【深度学习】对迁移学习中域适应的理解和3种技术的介绍
Mysql——字符串函数
微信读书,导出笔记
MySQL数据库字段超长问题
Mysql索引特性(重要)
弹性盒子模型
第03章 用户和权限管理【1.MySQL架构篇】【MySQL高级】
如何制作deb包
2.网络资源访问工具:requests
vlookup函数匹配不出来只显示公式的解决方法
明解C语言第五章习题
Oracle ADG状态查看与相关视图
MySQL 视图(详解)
How to make a deb package
深度学习模型训练前的必做工作:总览模型信息