I'm having memory issues with Neo4j when I insert lots of data in neo4j. Things have been better after adding a session.clear()
after each big treatments.
I'm planning to make different threads for every insert. Do i have to end all threads with a session.clear()
? What will be the consequences ?
session.clear()
is recommended to free up memory during batch operations.
Analyzing a bit what the session is doing might help for understanding why it should be cleared.
Imagine that you load in total 1000 entities. On every save call on the session, it will have to check all the entities for possible changes and persist them if needed (so checking if properties changed and if any relationship reference in the object graph changed as well).
This is extremely expensive if you don't do anything with those entities.
Regarding your question though, if the session is not shared between the threads (aka one distinct session per thread) then it is safe to clear it after heavy operations in the thread.
You might want to check my answer on this question for further performance considerations during batch inserts : Why is neo4j's insert speed so low in this example?