fish
fish

Reputation: 451

Java Heap Multithreading Optimization

I am writing a Java program that runs many simulations simultaneously in different threads and averages the results together. I want to run a lot of simulations, more than can be running simultaneously, so I want to run as many as I can simultaneously without running out of memory and “queue” the rest.

Is there an easy way to determine the maximum heap memory a thread uses?

Is there an easy way to check how much heap memory is being used at run time so I can only start new threads when memory opens up?

PS: I’m new to optimizing multithreaded applications.

Upvotes: 0

Views: 1257

Answers (3)

William Morrison
William Morrison

Reputation: 11006

Profile your threads to determine how much heap is occupied. I know of no way to do this programmatically.

Don't use more threads than you have cores. Creating new threads is a relatively expensive operation, and creating too many threads can actually cause your application to run more slowly. If you're processing huge data, or need very low latency, you'll really want to avoid creating many threads.

I suggest looking into ExecutorService. Create a fixed thread pool equal to the number of cores on your simulation machine.

Edit:

Since Java 1.4 we've had this available:

int cores = Runtime.getRuntime().availableProcessors();

Try this, create a fixed thread pool with cores threads. This would allow your application to scale between machines with a difference in cores.

Note this is the number of logical cores, so with Intel's hyperthreading feature it would count 2 "cores" for every processor. Still, a good measurement.

Upvotes: 1

jtahlborn
jtahlborn

Reputation: 53694

This is not an "easy" task to do, but there are some ways to do it. The jvm exposes the current memory usage through the management apis. You can use the MemoryMXBean and the MemoryPoolMXBean to get access to the current jvm memory status.

Upvotes: 0

Joni
Joni

Reputation: 111389

You can run the program with a single thread while monitoring it in VisualVM, and then with two threads, then with three. The difference in memory usage and garbage collection frequency can give you an idea of how much heap memory a single thread uses and how much of the memory is taken by objects shared between different threads.

Upvotes: 0

Related Questions