当前位置:网站首页>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,影响客户正常使用,造成生产事故
边栏推荐
- MYSQL JDBC图书管理系统
- 【限时福利】21天学习挑战赛 - MySQL从入门到精通
- KingbaseESV8R6 snapshot too old的配置和测试
- canvas基础讲解加示例
- MySQL笔记2(函数,约束,多表查询,事务)
- MySQL 高级(进阶) SQL 语句 (一)
- chrome扩展:如何使对话框位于当前窗口的右侧?
- Android Studio 实现登录注册-源代码 (连接MySql数据库)
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上
- To the operation of the int variable assignment is atom?
猜你喜欢

转义字符笔记记录
![Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]](/img/bb/25b0493398901b52d40ff11a21e34c.png)
Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]

【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?

2.网络资源访问工具:requests

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

我是一名阿里在职9年软件测试工程师,我的经历也许能帮到处于迷茫期的你

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

Flex布局详解

MySQL60题作业

YOLO V3详解
随机推荐
[Typora] This beta version of Typora is expired, please download and install a newer version.
mysql死锁
服务器不稳定因素
【深度学习】对迁移学习中域适应的理解和3种技术的介绍
HJ85 最长回文子串
MySQL BIGINT 数据类型
明解C语言第五章习题
YOLO V3详解
How to make a deb package
MySQL60题作业
vlan简单实验
Difference Between Concurrency and Parallelism
MySQL笔记1(数据库的好处,数据库的概念,数据库的特点,MySQL的启动,数据模型,SQL)
ENS emoji domain name is on fire!Hype or opportunity?
MySQL_关于JSON数据的查询
Structured Streaming报错记录:Overloaded method foreachBatch with alternatives
awk notes
MySQL group_concat()详解
7、MySQL Workbench 导出导入数据库
外包干了三年,废了...