内存有效映射,用于字节数组值

时间:2018-07-07 19:28:26

标签: java dictionary hashmap

我正在尝试将大量数据加载到HashMap中。目前,我正在尝试推送2000万个条目。当将大量条目加载到该映射中时,我注意到考虑到我要加载到该映射中的所有内容都是字节数组,因此消耗了大量内存(我从500mb文件生成此数据,平均每个字节数组都有大小5,最多11):

Map<Key, byte[]> result = new HashMap<>(entryCount, 1);
for (int i = 0; i < entryCount; i++) {
    do {
        // Read data from file, store it into result and increment count.
    } while (count < MAX_COUNT);   
}

还有Key类:

public final class Key {

    private final byte[] value;

    Key(byte[] value) {
        this.value = value;
    }

    // equals, hashCode, toString
}

使用jProfiler查找消耗了这么多内存的内容,我注意到HashMap$Node是图表顶部的类之一: enter image description here

我很好奇,对于这种确切类型的数据,是否有更高效的内存Map实现?

1 个答案:

答案 0 :(得分:0)

您可以使用特定类型的映射,例如在fastutil或trove等中。例如,可以在满足您要求的fastutil中使用Object2ByteArrayMap。

The fastuil api

相关问题