- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试一次性插入大约 190 万行作为批量插入到 MySQL 中。代码在 170 万行或更少的情况下工作得非常顺利,但对于超过 180 万行,我会遇到 GC 开销和/或有时出现 Java 堆空间错误。
代码:
try {
// triples is of type ArrayList<String>
String[] tripleToInsert= null;
for (int i = 0; i < triples.size(); i++) {
count++;
tripleToInsert= triples.get(i).split("\\s+");
/** Insert <s,p,o> into Triples table **/
preparedStmt.setString(1, tripleToInsert[0].trim());
preparedStmt.setString(2, tripleToInsert[1].trim());
preparedStmt.setString(3, tripleToInsert[2].trim());
preparedStmt.addBatch();
preparedStmt.clearParameters();
tripleToInsert=null;
}
}
catch(OutOfMemoryError e)
{
System.out.println("OOM error: IN LOADING TO DB function on loop count: " + count);
e.printStackTrace();
}
String preEndTime= new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println("Preprocessing Ended:" + preEndTime);
//Insert start time
String startTime= new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println("Insert Started:" + startTime);
// execute the prepared statement as a batch
long[] results = preparedStmt.executeLargeBatch();
System.out.println("Update Count size: "+ results.length);
//Insert end time
String endTime = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println("Insert Completed:" + endTime);
遇到错误:
OOM error: IN LOADING TO DB function on loop count: 1888076
java.lang.OutOfMemoryError: GC overhead limit exceeded
at com.mysql.jdbc.SingleByteCharsetConverter.toBytesWrapped(SingleByteCharsetConverter.java:230)
at com.mysql.jdbc.StringUtils.getBytesWrapped(StringUtils.java:652)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4005)
at LoadNTriplesByScript.loadTriplesByBatches(LoadNTriplesByScript.java:282)
at LoadNTriplesByScript.insertNTriplesToDB(LoadNTriplesByScript.java:137)
at LoadNTriplesByScript.main(LoadNTriplesByScript.java:77)
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.lang.Object.clone(Native Method)
at java.util.TimeZone.clone(TimeZone.java:738)
at sun.util.calendar.ZoneInfo.clone(ZoneInfo.java:647)
at java.util.TimeZone.getDefault(TimeZone.java:625)
at java.text.SimpleDateFormat.initializeCalendar(SimpleDateFormat.java:657)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:601)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:580)
at LoadNTriplesByScript.loadTriplesByBatches(LoadNTriplesByScript.java:330)
at LoadNTriplesByScript.insertNTriplesToDB(LoadNTriplesByScript.java:137)
at LoadNTriplesByScript.main(LoadNTriplesByScript.java:77)
Eclipse 版本:Oxygen.1a 版本 (4.7.1a)
Java 堆大小:-Xms2048m -Xmx3072m
我不确定这是否是由字符串操作(split() 方法)引起的,或者是否对PreparedStatement addBatch() 方法有限制。每批处理的行数。我设置了autoCommit(FALSE),然后在执行PreparedStatement后commit()。
注意:我读过很多关于 GC 开销和 Java 堆错误的文章和帖子。任何关于为什么会发生这种情况的建议或指示都值得赞赏。
最佳答案
很有可能,您刚刚耗尽了内存。该消息具有误导性,因为高 GC 开销通常很简单,因为没有任何可收集的内容(所有内存都在使用中)。
批处理的原因是避免每个命令的高开销。就性能而言,如果您使用 1000 行或 1000000 行的批处理,这并不重要。因此尝试小批量。
如果您的应用程序可以忍受一段时间不完整的数据,也许还可以尝试较小的提交。
请注意,您将在内存中保存 triples
中的原始行和 tripleToInsert
中的拆分行。由于有 180 万行和总共 3 GB,每行可以使用的空间少于大约 1500 字节。考虑到一个字符在 Java 中是两个字节,考虑到您将其存储两次并且总是存在一些开销,因此它可以在每行 < 300 个字符的情况下工作(只是猜测)。
你基本上做得对,只是这么大的批处理需要大量的内存。如果这只是一个导入,那么您可以通过在消耗未分割的三元组时清除它们来节省一些内存。
有时可以使用数据库提供的内置 CSV 导入。
关于java - 批量插入 MySQL 会导致 GC 开销和/或 Java 堆空间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49743152/
我想知道,通过数据 channel 发送数据时 WebRTC 会产生多少开销。 我知道 Websockets 每帧有 2 - 14 字节的开销。 WebRTC 是否使用更多开销?我在网上找不到一些有用
我想知道与创建新类而不是该类的新对象相关的开销是小还是大。我正在使用 dojo,但我将提供纯 JS 的示例。我将在启动时创建 10 到 100 个对象,我认为这不会是一个严重的问题,但我想涵盖所有基础
我有一个如下所示的表设置。 Table comment_flags user_id comment_id 我允许用户标记评论,然后给他们取消标记的选项,因为他们可能犯了一个错误。 问题
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: In MySQL what does “Overhead” mean, what is bad about it,
我正在制作一个非常简单的游戏,只是为了好玩/练习,但无论它现在有多简单,我仍然想很好地编写它,以防我想回到它并只是为了学习 因此,在这种情况下,我的问题是: 对象分配涉及多少开销?解释器对此的优化程度
我有一些资源敏感的东西要写。我想知道与仅将这些变量一起传递(例如作为函数参数)相比,在结构中将变量组合在一起是否真的会导致内存开销。 如果是这样,那么在不产生开销的情况下创建对惰性值进行操作的东西的好
我一直在开发一个实时应用程序,并注意到一些 OOP 设计模式在 Python 中引入了难以置信的开销(使用 2.7.5 进行了测试)。 直截了当,当字典被另一个对象封装时,为什么简单的字典值访问器方法
我正在从 ifstream 中读取随机 ascii 文本文件。我需要能够将整个消息放入字符串类型以进行字符解析。我当前的解决方案有效,但我认为我通过使用等效于此的方式来谋杀更冗长文件的处理时间: st
纯粹从软件工程的角度来看,getActivity() 有多少开销? 我在整个应用程序中经常多次使用此方法,并考虑使用一个引用 getActivity() 的全局变量。 如果为 Activity 设置一
我一直在研究 Riccardo Terrell 的 Akka.NET 分形演示 (https://github.com/rikace/akkafractal) 以尝试理解它。 (这很棒,顺便说一句)
我正在尝试使用高分辨率计时器查找我的代码运行时间,我注意到计时器的结果不一致,我想知道为什么会这样。 我找到了这篇文章 How do you test running time of VBA code
我正在学习WPF。我现在开始装订了。使用 INotifyPropertyChanged 时绑定(bind)是否依赖反射?是这样,价格是多少?我正在考虑使用 WPF 来显示通过 UDP 流式传输的数据,
我有某种模板化基类 template class Base { }; 并希望将其派生实例存储在列表中。为此,我使用 using derived_handle = std::unique_ptr v
使用GHC.TypeLits中的Sing有任何开销吗? ?以程序为例: {-# LANGUAGE DataKinds #-} module Test (test) where import GHC.T
我有某种模板化基类 template class Base { }; 并希望将其派生实例存储在列表中。为此,我使用 using derived_handle = std::unique_ptr v
我有一个 ORM sqlalchemy 模型,我需要构建一个查询(使用 ORM 类更容易构建),但这需要大量时间。当我直接像 SQL 一样向数据库执行相同的查询时,速度相当快。 使用 SQLAlche
我在 PHP 平台上有一家商店(开发不善),那里有很多不好的查询(没有索引的长查询、rand() 排序、动态计数,..) 我现在无法更改查询,但我必须调整服务器才能保持事件状态。 我尝试了我所知道的一
我有一个使用 JQuery mobile 构建的移动应用程序,响应时间对我来说非常重要,因为我希望为我的用户提供流畅的体验。 我刚刚将网站的安装移至本地服务器,以提高应用程序的性能,因为它连接到本地
关于数据库设计的问题。如果我有 28 个 bool 值并且能够将它们添加为每行 28 个 bool 值或一个整数,哪一个会更快?哪种方法将使磁盘上的表大小保持最低? 这是在假设我需要的可以通过查询中的
我有一个看起来像 Boost.Array 的简单类。有两个模板参数 T 和 N。Boost.Array 的一个缺点是,每个使用这种数组的方法都必须是带有参数 N 的模板(T 可以)。结果是整个程序往往
我是一名优秀的程序员,十分优秀!