gpt4 book ai didi

out-of-memory - Raven DB DocumentStore - 抛出内存不足异常

转载 作者:行者123 更新时间:2023-12-05 00:34:59 25 4
gpt4 key购买 nike

我有这样的代码:

public bool Set(IEnumerable<WhiteForest.Common.Entities.Projections.RequestProjection> requests)
{
var documentSession = _documentStore.OpenSession();
//{
try
{
foreach (var request in requests)
{
documentSession.Store(request);
}
//requests.AsParallel().ForAll(x => documentSession.Store(x));
documentSession.SaveChanges();
documentSession.Dispose();
return true;
}
catch (Exception e)
{
_log.LogDebug("Exception in RavenRequstRepository - Set. Exception is [{0}]", e.ToString());
return false;
}
//}
}

这段代码被多次调用。在我获得通过它的大约 50,000 个文档后,我得到一个 OutOfMemoryException。
知道为什么吗?也许过一段时间我需要声明一个新的 DocumentStore ?

谢谢你

**
  • 更新:

  • **

    我最终使用 Batch/Patch API 来执行我需要的更新。
    你可以在这里看到讨论: https://groups.google.com/d/topic/ravendb/3wRT9c8Y-YE/discussion

    基本上,因为我只需要更新我的对象的 1 个属性,并且在考虑了关于将所有对象重新序列化回 JSON 的 ayendes 评论之后,我做了这样的事情:
    internal void Patch()
    {
    List<string> docIds = new List<string>() { "596548a7-61ef-4465-95bc-b651079f4888", "cbbca8d5-be45-4e0d-91cf-f4129e13e65e" };
    using (var session = _documentStore.OpenSession())
    {
    session.Advanced.DatabaseCommands.Batch(GenerateCommands(docIds));
    }
    }

    private List<ICommandData> GenerateCommands(List<string> docIds )
    {
    List<ICommandData> retList = new List<ICommandData>();

    foreach (var item in docIds)
    {
    retList.Add(new PatchCommandData()
    {
    Key = item,
    Patches = new[] { new Raven.Abstractions.Data.PatchRequest () {
    Name = "Processed",
    Type = Raven.Abstractions.Data.PatchCommandType.Set,
    Value = new RavenJValue(true)
    }}});
    }

    return retList;
    }

    希望这可以帮助 ...

    非常感谢。

    最佳答案

    我只是为我当前的项目做了这个。我将数据分块并将每个块保存在新 session 中。这也可能对你有用。

    请注意,此示例显示一次按 1024 个文档进行分块,但在我们决定值得分块之前至少需要 2000 个。到目前为止,我的插入获得了最好的性能,块大小为 4096。我认为这是因为我的文档相对较小。

    internal static void WriteObjectList<T>(List<T> objectList)
    {
    int numberOfObjectsThatWarrantChunking = 2000; // Don't bother chunking unless we have at least this many objects.

    if (objectList.Count < numberOfObjectsThatWarrantChunking)
    {
    // Just write them all at once.
    using (IDocumentSession ravenSession = GetRavenSession())
    {
    objectList.ForEach(x => ravenSession.Store(x));
    ravenSession.SaveChanges();
    }

    return;
    }

    int numberOfDocumentsPerSession = 1024; // Chunk size

    List<List<T>> objectListInChunks = new List<List<T>>();

    for (int i = 0; i < objectList.Count; i += numberOfDocumentsPerSession)
    {
    objectListInChunks.Add(objectList.Skip(i).Take(numberOfDocumentsPerSession).ToList());
    }

    Parallel.ForEach(objectListInChunks, listOfObjects =>
    {
    using (IDocumentSession ravenSession = GetRavenSession())
    {
    listOfObjects.ForEach(x => ravenSession.Store(x));
    ravenSession.SaveChanges();
    }
    });
    }

    private static IDocumentSession GetRavenSession()
    {
    return _ravenDatabase.OpenSession();
    }

    关于out-of-memory - Raven DB DocumentStore - 抛出内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10030704/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com