首页 > 解决方案 > 如何解决 Azure for PostgreSQL 中的内存不足错误

问题描述

我正在一个项目中工作,该项目从数据库中提取一百万条数据,并将这些数据转换为 csv 文件以供下载。我使用的数据库是 PostgreSQL,我将它部署到 AKS (Azure kubernetes)。该代码在我本地运行良好。我正在从 AKS 使用的本地连接相同的数据库。但是当我试图在 AKS 中执行代码时,我总是得到

 java.lang.OutOfMemoryError: Java heap space] with root cause
  java.lang.OutOfMemoryError: Java heap space
at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor.getExtractor(VarcharTypeDescriptor.java:59) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:249) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.custom.ScalarResultColumnProcessor.extract(ScalarResultColumnProcessor.java:54) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.custom.ResultRowProcessor.buildResultRow(ResultRowProcessor.java:83) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.custom.ResultRowProcessor.buildResultRow(ResultRowProcessor.java:60) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:412) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:775) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.Loader.getRowsFromResultSet(Loader.java:1044) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.Loader.processResultSet(Loader.java:995) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:964) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:350) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2887) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2869) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2701) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.Loader.list(Loader.java:2696) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338) ~[hibernate-core-5.4.21.Final.jar!/:5.4.21.Final]
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java

我正在发布执行的代码

Query query = entityManager.createNativeQuery(sqlQuery);
List<Object[]> objectList = query.getResultList();
objectList.forEach(glObject -> {
 myEntity entity = myEntity.builder()
 .supl_num(glObject[0])
 .detail(glObject[1])
 ...............
  .build();
myEntityList.add(entity);   
});

AKS 部署配置

...........
ports:
     -containerPort: 80
resources:
  requests:
    cpu : 75m
    memory:256Mi
  limits:
     cpu:1
   memory: 2Gi

我不明白为什么这会导致一次又一次

标签: postgresqlmemory-managementout-of-memoryazure-aks

解决方案


在 JDBC URL 中使用defaultRowFetchSize 连接模式,以便驱动程序以块的形式获取数据,而不是一次全部获取。


推荐阅读