当前位置:网站首页>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,影响客户正常使用,造成生产事故
边栏推荐
- 明解C语言第六章习题
- 【深度学习】对迁移学习中域适应的理解和3种技术的介绍
- bebel系列- 插件开发
- Mysql 回表
- Common Expression Recognition Based on Face (1) - Basic Knowledge of Deep Learning
- Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
- Running the evict task with compensationTime
- Recommender systems: overview of the characteristics of architecture: user/item engineering -- -- -- -- -- -- -- -- > recall layer > sort layer - > test/evaluation 】 【 cold start problems, real-time 】
- Activiti 工作流引擎 详解
- HarmonyOS Notes ------------- (3)
猜你喜欢
肖特基二极管厂家ASEMI带你认识电路中的三大重要元器件
C语言中指针没那么难~ (1)【文章结尾有资料】
MySQL的on duplicate key update 的使用
WPS没有在任务栏显示所有窗口选项怎么回事?
Zabbix部署与练习
Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
深入浅出边缘云 | 3. 资源配置
Office365无法打开word文档怎么办?Office365无法打开word文档的解决方法
MySQL Workbench 安装及使用
在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块
随机推荐
【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]
Multi-threaded mutex application RAII mechanism
MySQL笔记2(函数,约束,多表查询,事务)
Running the evict task with compensationTime
[Nuxt 3] (十四) Nuxt 生命周期
转义字符笔记记录
chrome扩展:如何使对话框位于当前窗口的右侧?
WPS没有在任务栏显示所有窗口选项怎么回事?
idea plugins搜索不到插件
To the operation of the int variable assignment is atom?
MVC模式和三层架构
树形结构:二叉树的递归非递归遍历、BST
2.网络资源访问工具:requests
mpls简介
微信读书,导出笔记
6.3有定型性 第七章
ENS 表情包域名火了!是炒作还是机遇?
vookloop函数怎么用?vlookup函数的使用方法介绍
【luogu P8031】Kućice(计算几何)