Spring启动-H2 DB导入4GB data.sql OutOfMemory错误

Spring启动-H2 DB导入4GB data.sql OutOfMemory错误,spring,spring-boot,jvm,heap-memory,intellij-14,Spring,Spring Boot,Jvm,Heap Memory,Intellij 14,我想在data.sql中导入数据,大约4GB 但发生OutOfMemory-堆空间错误 我已经在IntelliJ中增加了堆空间,但仍然不起作用。 (-Xms16g-Xmx16g) 我的错误是: Caused by: java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3332) ~[na:1.8.0_211] at java.lang.AbstractStringBu

我想在data.sql中导入数据,大约4GB 但发生OutOfMemory-堆空间错误

我已经在IntelliJ中增加了堆空间,但仍然不起作用。 (-Xms16g-Xmx16g)

我的错误是:

Caused by: java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3332) ~[na:1.8.0_211]
    at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124) ~[na:1.8.0_211]
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448) ~[na:1.8.0_211]
    at java.lang.StringBuilder.append(StringBuilder.java:136) ~[na:1.8.0_211]
    at org.springframework.jdbc.datasource.init.ScriptUtils.readScript(ScriptUtils.java:388) ~[spring-jdbc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.jdbc.datasource.init.ScriptUtils.readScript(ScriptUtils.java:330) ~[spring-jdbc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:582) ~[spring-jdbc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:254) ~[spring-jdbc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:49) ~[spring-jdbc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:202) ~[spring-boot-autoconfigure-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.initSchema(DataSourceInitializer.java:119) ~[spring-boot-autoconfigure-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:91) ~[spring-boot-autoconfigure-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:38) ~[spring-boot-autoconfigure-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:99) ~[spring-boot-autoconfigure-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.postProcessAfterInitialization(DataSourceInitializedPublisher.java:90) ~[spring-boot-autoconfigure-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$151/1711185459.getObject(Unknown Source) ~[na:na]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
我的application.yml(h2配置)是:


如何解决此错误?

转到终端并使用此命令启动应用程序

java -jar your_app_name.jar -Xms16g -Xmx16g

转到终端并使用此命令启动应用程序

java -jar your_app_name.jar -Xms16g -Xmx16g

tl;dr:spring数据库sql初始化不适用于大文件

OutOfMemory来自于您对spring数据库sql初始化的使用

它不打算执行这么大的文件

Spring将首先将文件加载到内存中 在加载过程中,文件被逐行添加到StringBuilder中,该StringBuilder不断重新分配,导致内存分配出现临时峰值

根据您的jvm版本,在内存中存储文件所需的内存可能比原始文件大小大很多(因为它可能存储在unicode中)

然后,生成的StringBuilder将转换为字符串(临时内存分配)

然后,该字符串被sql语句拆分,并全部添加到arraylist中(将脚本所需的内存增加一倍)


tl;dr:spring数据库sql初始化不适用于大文件

OutOfMemory来自于您对spring数据库sql初始化的使用

它不打算执行这么大的文件

Spring将首先将文件加载到内存中 在加载过程中,文件被逐行添加到StringBuilder中,该StringBuilder不断重新分配,导致内存分配出现临时峰值

根据您的jvm版本,在内存中存储文件所需的内存可能比原始文件大小大很多(因为它可能存储在unicode中)

然后,生成的StringBuilder将转换为字符串(临时内存分配)

然后,该字符串被sql语句拆分,并全部添加到arraylist中(将脚本所需的内存增加一倍)


请参见

如何开始导入?从Intellij或通过命令行使用
Run
按钮?您是使用32位还是64位JVM运行@Gabbleg我使用Intellij中的
run
按钮和64位JVM@hanswsw那你应该很好。我会检查
java.util.Arrays.copyOf(Arrays.java:3332)
。即使使用16g,您也可能会获得
堆空间
,因为副本太多。您是否尝试在数据源初始化本身上使用H2 RunScript?正如@GabLeg指出的,即使是16G的分配也可能不够。您可能需要配置文件以观察实际需要。如何开始导入?从Intellij或通过命令行使用
Run
按钮?您是使用32位还是64位JVM运行@Gabbleg我使用Intellij中的
run
按钮和64位JVM@hanswsw那你应该很好。我会检查
java.util.Arrays.copyOf(Arrays.java:3332)
。即使使用16g,您也可能会获得
堆空间
,因为副本太多。您是否尝试在数据源初始化本身上使用H2 RunScript?正如@GabLeg指出的,即使是16G的分配也可能不够。您可能需要配置文件来观察实际需要。我想从intellij运行它,我还提供了-Xms16g-Xmx16g VM参数,但它不起作用。我想从intellij运行它,我还提供了-Xms16g-Xmx16g VM参数,但它不起作用。