我正在創建100萬個節點。爲了避免內存不足,我必須以較小的批次創建這些內容。現在我試圖刪除現有的節點,但是我又遇到了內存不足異常。我應該真的能夠刪除這個級別的數據,而不會耗盡內存,也不必編碼這個限制。我在這裏做錯了什麼?爲什麼Neo4j(客戶端)內存不足?
我知道我可以增加Java堆的大小,但我覺得也只是推遲的真正問題到以後的時間點,當我有更多的數據,以創建/刪除。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Neo4jClient;
namespace Neo4JWontDelete
{
class Program
{
private const int MaximumNumberOfWordsToCreate = 1000*1000;
private const int BatchSize = 5 * 1000;
static void Main(string[] args)
{
var client = new GraphClient(new Uri("http://neo4j:[email protected]:7474/db/data"));
client.Connect();
Console.WriteLine("Starting with a clean database");
DeleteAllObjects(client);
Console.WriteLine("Creating data");
int currentWordNumber = 1;
while (currentWordNumber < MaximumNumberOfWordsToCreate)
{
int numberOfWordsToCreate = MaximumNumberOfWordsToCreate - currentWordNumber;
if (numberOfWordsToCreate > BatchSize)
numberOfWordsToCreate = BatchSize;
var words = Enumerable.Range(currentWordNumber, BatchSize).Select(x => new Word {Value = x.ToString()});
client.Cypher
.Create("(w:Word {words})")
.WithParam("words", words)
.ExecuteWithoutResults();
currentWordNumber += numberOfWordsToCreate;
Console.WriteLine(currentWordNumber - 1);
}
Console.WriteLine("Deleting data");
DeleteAllObjects(client);
}
private static void DeleteAllObjects(GraphClient client)
{
client.Cypher
.Match("(w :Word)")
.Delete("w")
.ExecuteWithoutResults();
}
}
class Word
{
public string Value { get; set; }
}
}
爲什麼設置BATCHSIZE和MaximumNumberOfWordsToCreate乘法的結果,而不僅僅是1000000及5000? – holaSenor
嗨,彼得,只是我注意到了 - 你想要一個乾淨的數據庫或只是刪除'話'? Clean db將是'.Match(「n」)。OptionalMatch(「n- [r] - ()」)。Delete(n,r).ExecuteWithoutResults()' –