我在我的项目中有一个要求,将 900 万条数据从 oracle 数据库缓存到 Hazelcast 。但显然 Hazelcast 消耗的堆空间比它应该消耗的更多。我已经为应用程序分配了 8bg 堆空间,但仍然出现内存不足错误。
下面是我的数据加载器类。
public class CustomerProfileLoader implements ApplicationContextAware, MapLoader<Long, CustomerProfile> {
private static CustomerProfileRepository customerProfileRepository;
@Override
public CustomerProfile load(Long key) {
log.info("load({})", key);
return customerProfileRepository.findById(key).get();
}
@Override
public Map<Long, CustomerProfile> loadAll(Collection<Long> keys) {
log.info("load all in loader executed");
Map<Long, CustomerProfile> result = new HashMap<>();
for (Long key : keys) {
CustomerProfile customerProfile = this.load(key);
if (customerProfile != null) {
result.put(key, customerProfile);
}
}
return result;
}
@Override
public Iterable<Long> loadAllKeys() {
log.info("Find all keys in loader executed");
return customerProfileRepository.findAllId();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
customerProfileRepository = applicationContext.getBean(CustomerProfileRepository.class);
}
}
Run Code Online (Sandbox Code Playgroud)
下面是存储库查询。如果我更改以下查询,使其限制为 200 万个数据,那么一切正常。
@Query("SELECT b.id FROM CustomerProfile b ")
Iterable<Long> findAllId();
Run Code Online (Sandbox Code Playgroud)
以下是我在hazelcast.xml文件中的地图配置。在这里,我给出了backup countas zero,在它是 1 之前,但这没有任何区别。
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast
xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-3.11.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- Use port 5701 and upwards on this machine one for cluster members -->
<network>
<port auto-increment="true">5701</port>
<join>
<multicast enabled="false"/>
<tcp-ip enabled="true">
<interface>127.0.0.1</interface>
</tcp-ip>
</join>
</network>
<map name="com.sample.hazelcast.domain.CustomerProfile">
<indexes>
<!-- custom attribute without an extraction parameter -->
<index ordered="false">postalCode</index>
</indexes>
<backup-count>0</backup-count>
<map-store enabled="true" initial-mode="EAGER">
<class-name>com.sample.hazelcast.CustomerProfileLoader</class-name>
</map-store>
</map>
</hazelcast>
Run Code Online (Sandbox Code Playgroud)
数据库表结构:
ID NOT NULL NUMBER(19)
LOGIN_ID NOT NULL VARCHAR2(32 CHAR)
FIRSTNAME VARCHAR2(50 CHAR)
LASTNAME VARCHAR2(50 CHAR)
ADDRESS_LINE1 VARCHAR2(50 CHAR)
ADDRESS_LINE2 VARCHAR2(50 CHAR)
CITY VARCHAR2(30 CHAR)
postal_code VARCHAR2(20 CHAR)
COUNTRY VARCHAR2(30 CHAR)
CREATION_DATE NOT NULL DATE
UPDATED_DATE NOT NULL DATE
REGISTER_NUM NOT NULL VARCHAR2(10 CHAR)
Run Code Online (Sandbox Code Playgroud)
其他要点:
JAVA_OPTS=-Xmx8192m。在它是 4gb 之前,但是当我遇到堆空间错误时,我增加到 8GB ,但没有运气。我现在面临的问题是:
当它获取所有数据并将其加载到映射时,我收到堆空间错误(java.lang.OutOfMemoryError: Java heap space)。现在表中有 900 万条数据。
加载数据也需要花费大量时间,可能我可以通过运行多个hazelcast服务器实例来解决这个问题。
我是榛子城的新手,因此将不胜感激任何帮助:)
在我看来,真正的问题是您在 8GB 堆中无法容纳太多数据。
你说你平均每行有 100 个字节的数据表示为字符串数据。
这里有一些估计1来表示一个9000000场的行的数据作为所需要的空间的HashMap。假设有 9 个字符串、2 个日期和一个int.
Date是 32 字节 x 2 -> 64 字节Integer)将是 24 个字节。尽可能多的实际数据超过 8GB。然后考虑到 Java 堆需要相当数量的工作空间这一事实;至少说 30%。
您收到 OOME 并不奇怪。我的猜测是您的堆需要大 50% ......并且假设您对每行 100 字节的估计是准确的。
这完全基于您的loadAll方法,该方法似乎将数据库中的所有行作为常规HashMap. 它不考虑 Hazelcast 用于缓存的堆空间或其他内存。
虽然您可以只扩展堆,但我认为更改您的代码使其不会像那样实现行会更有意义。目前还不清楚这是否有意义。这将取决于地图的使用方式。
1 - 我假设您使用的是 Java 8。