当前位置:网站首页>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,影响客户正常使用,造成生产事故
边栏推荐
- 数字货币期货现货交易技巧,把握关键进场的买入点!(纯干货)
- [Nuxt 3] (十四) Nuxt 生命周期
- 2022年SQL经典面试题总结(带解析)
- vlookup函数匹配不出来的原因及解决方法
- 文字的选择与排版
- Use the map function to operate on each element in the list It seems that you don't need a map
- 肖特基二极管厂家ASEMI带你认识电路中的三大重要元器件
- ENS emoji domain name is on fire!Hype or opportunity?
- 明解C语言第六章习题
- 深度学习模型训练前的必做工作:总览模型信息
猜你喜欢
Mysql8创建用户以及赋权操作
Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]
Cookie中的JSESSIONID说明
Babbitt | Metaverse Daily Must Read: The shuffling is coming, will the digital Tibetan industry usher in a new batch of leaders in the second half?Will there be new ways to play?...
Deep Non-Local Kalman Network for VideoCompression Artifact Reduction
YOLO V3详解
KingbaseESV8R6 snapshot too old的配置和测试
Recommendation system: evaluation index [offline evaluation index: RMSE (root mean square error), AUC, precision, recall, F1] [online evaluation: A/B test] [generally required response time <0.5s]
Zabbix部署与练习
MySQL (2)
随机推荐
MySQL数据库字段超长问题
YOLO V3详解
excel数字下拉递增怎么设置?
[Nuxt 3] (十三) Nuxt 是如何工作的?
What is the common factor
excel数字显示e+17怎么恢复?excel数字变成了小数点+E+17的解决方法
PR视频剪辑软件教程
多线程获取官方汇率
Recommendation system: evaluation index [offline evaluation index: RMSE (root mean square error), AUC, precision, recall, F1] [online evaluation: A/B test] [generally required response time <0.5s]
HJ85 最长回文子串
在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块
IDEA2018.3.5取消双击Shift快捷键
C语言中指针没那么难~(2)【文章结尾有资料】
mysql 递归函数with recursive的用法
vlookup函数匹配不出来只显示公式的解决方法
网络安全实验环境搭建
awk笔记
明解C语言第六章习题
Oracle ADG状态查看与相关视图
c语言:操作符详解