Java Threads for Memory Management
I have an application that performs a very sequential set of discrete tasks.
My problem is that one of the first tasks consumes a large amount of memory, and despite eliminating object references and invoking the garbage collector, only about half the memory is essentially freed. This impacts later tasks. The problem is also that I want to temporarily grant the JVM a large heap to efficiently manage the first task but I don't want this to stick around till the GC decides it's efficient to free the rest.
I had the idea of executing the memory-intensive task inside thread; the new child thread uses the parent JVM (no surprise here), but there appears to be no change in the memory management.
How does Java handle Thread memory? Is there a simple way to crea开发者_Go百科te a child heap for the subthread that can be dumped after the thread has finished?
As an addendum, here's what I actually want to do:
- Setup a Neo4j graph database (I'm creating several million nodes, properties and relationships, along with numerous indexes) [memory intensive]
- Perform queries on the graph database
No, heap is shared between threads and there isn't a way to reserve memory for a given thread or allow a thread to break limits. Threads are not processes (despite they are implemented this way in some jvm).
You could run this thread in a separate procss (different JVM) and pass data to it via files or sockets, but while it would solve memory problems, it could kill performances ... but depends on how much data you need to pass.
Use a memory profiler to find out which GC root is keeping the objects alive that you expected to be garbage-collected.
I expect, however, Neo4j is keeping those objects alive and there might be little you can do about it. After all, your graph and its indexes do need to be there for you to be able to perform queries on them.
You might be able to find some Neo4j API call to tell it to clean out some caches or something similar.
精彩评论