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 create 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.