我正在联系这里的社区,了解是否有一种方法可以在 Java 映射结构中存储许多对象(> 500K)。我知道,如果我使用 Java Collections API,并且我正在寻找其他解决方案(分布式缓存之外),那么在某些时候我的应用程序将超出其内存占用量。我的意图是存储一些达到 Map> 效果的东西。
要求:
- 结构是不可变的(一次加载到地图中)。仅读取访问权限。
- 对地图的访问需要相当快,但不寻求低延迟检索,更关心在内存中存储和保留这些对象。
有谁知道我可以利用这样一个库来实现这一点,或者有人遇到过需要在 Java 内存中加载许多对象的解决方案吗?我很想听听您的反馈。
谢谢。
I'm reaching out to the community here to understand if there is a way to store many objects in a Java map structure (> 500K). I know at some point my application will exceed its memory footprint if I use the Java Collections API and I'm looking for other solutions (outside of distributed caching). My intention is to store something to the effect of Map<String, List<String>>.
Requirements:
- Structure will be immutable (one time load into the map). Only read access.
- Access to the map needs to be fairly quick but not looking for low latency retrievals, more concerned about storing and retaining these objects in memory.
Does anyone know of such a library I can utilize that can achieve this or has anyone ever come across a solution in which they were required to load many objects in memory in Java? I'd be very interested in hearing your feedback.
Thanks.
发布评论
评论(4)
EhCache 非常适合此目的。最基本的是,它提供了一个键值映射、可选的溢出到磁盘以及可选的 JVM 重新启动持久性。它将把最常用的元素保留在内存中。
EhCache would be perfect for this. At its most basic, it offers a key-value map, with optional overflow to disk, and optional persistence over JVM restarts. It will keep elements in memory that are most frequently used.
我和斯卡夫曼在一起。除了溢出到磁盘之外,EhCache 还允许您将缓存放置在进程内但堆外。如果您的缓存确实很大,这可能会对性能产生非常积极的影响,因为它减少了 GC 的压力。
然而,这个特定功能必须付费,否则 EhCache 是免费的。
除了 EhCache,Java 中还有一些其他缓存提供类似甚至有时甚至更高级的选项,例如 Infinispan 或 OsCache。
I'm with skaffman. In addition to the overflow to disk EhCache offers you to place the cache in-process but off-heap. If your cache is really big, this may have a very positive impact on performance since it reduces stress on the GC.
This specific feature however must be payed for, but otherwise EhCache is free.
Instead of EhCache, there are a couple of other caches in Java that offer similar or sometimes even more advanced options like Infinispan or OsCache.
您是否考虑过只读数据库,例如 java cdb: http://www.strangegizmo .com/products/sg-cdb/
Have you considered a read-only database, such as a java cdb: http://www.strangegizmo.com/products/sg-cdb/
H2 数据库引擎的 MVStore 功能
虽然我还没有尝试过,但 H2 数据库引擎 除了关系数据库引擎之外,还提供专门的键值存储。该商店名为 MVStore。虽然由 H2 内部使用,但文档建议您可以直接将其用于键值存储。
MVStore 允许您通过
java.util.Map
接口,不使用 JDBC 或 SQL。示例用法,取自文档:
阅读完整文档以了解其性能概况、并发性以及在内存中缓存同时持久保存到存储的能力。
MVStore feature of H2 Database Engine
While I have not tried it, the H2 Database Engine offers a specialized key-value store besides its relational database engine. This store is called MVStore. While used internally by H2, the documentations suggest you may use it directly for key-value storage.
MVStore lets you interact through the
java.util.Map
interface, without using JDBC or SQL.Example usage, taken from the doc:
Read the thorough documentation to learn about its performance profile, concurrency, and ability to cache in-memory while also persisting to storage.