简体   繁体   中英

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM