Exception java.lang.OutOfMemoryError: GC overhead limit exceeded with tree map

Exception java.lang.OutOfMemoryError: GC overhead limit exceeded with tree map

我在从 Hibernate 查询加载行时不断收到 java.lang.OutOfMemoryError: GC overhead limit exceeded

我试过几次增加内存,但它一直在发生。我在我的日志中注意到它似乎指向我正在使用 TreeMap 的方法。我想知道我是否使用不当导致内存不足问题。

public List<Item> getProducts() {
    List<ProductListing> productListings = session.createCriteria(ProductListing.class)
    .createAlias("productConfiguration", "productConfiguration")
    .add(Restrictions.eq("productConfiguration.category", category))
    .add(Restrictions.eq("active", true))
    .add(Restrictions.eq("purchased", true)).list();

    Map<String, Item> items = new TreeMap<>();

    productListings.stream().forEach((productListing) -> {
        Item item = productListing.getItem();
        items.put(item.getName(), item);
    });

    return new ArrayList<>(items.values());
}

我只是想知道我是否做错了什么。它看起来是正确的,但内存异常另有说明。

我称之为 "loading the world" - 名称 getProducts()(没有参数)有点代码味道 - 因为你没有限制结果集的大小,据我们所知你的 Item 对象可能很大,有很多急切加载的依赖项(更不用说堆上的所有后备 Hibernate 对象)。

另一个大问题是,您将脱水实体添加到 TreeMap,调用 hashCode() 并可能调用 equals(),只是为了丢弃键并复制值进入新分配的 ArrayList.

撇开 ArrayList 缺乏预调整(正确,这并不理想,虽然它应该只是慢),为什么 TreeMap 阶段?如果需要进行聚合,为什么不让数据库更有效地执行此操作(例如通过 GROUP BY name)并使用索引,而不是将其全部拉入地图以进行更慢的重新处理?至少,通过仅返回唯一 Items,您可以跳过地图阶段并直接复制到您的列表中(根据您的需要,甚至还有更轻量级的可能性)。

我强烈建议使用合适的 Profiler 进行测试。具体来说,它可以帮助您确定预加载对堆大小的影响。可能只是关闭它可能会使问题更易于管理。

但是,您还需要考虑代码的客户:谁真正需要 所有Item?很可能没有人。