Java needed memory for new thread

淺唱寂寞╮ 提交于 2019-12-10 10:29:57

问题


I have an application which massively creates threads. As result I get an OutOfMemoryError. My idea is to wait until there is enough free space to create the next Thread. But therefore I need to know how many memory I need to create a thread and if this amount of memory is available. Is there a way to get the amount of memory a thread needs? And how can I determine if this amount of memory is available?

What I've already tried:

for (int i = 0; i < links.size(); i++) {
    while(Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory() +
        Runtime.getRuntime().freeMemory() < MEMORY_THRESHOLD) {
        synchronized (lock) {
            lock.wait(10);
        }
    }
    Thread t = new Thread(new Task(links.get(i)));
    t.setDaemon(true);
    t.start();
}

But even when I use 100MB as threshold I get an OutOfMemoryError.


回答1:


No, you go and delete this code and use a ThreadPool instead. What you trying to do is quite hard and you should be using the higher abstraction you have to do multithreading or you don't do it at all.

Check out Executors, currently they are the higher abstraction. If your tasks are compute bound then ForkJoin in Java 7 is your friend and you don't need to bother about the pool size as long as you know how to recursively divide your problem into subproblems.




回答2:


There is not enough information on the question in order to suggest an optimal solution.

Typically you may need to create many more threads than your actual CPU cores when you have expensive I/O operations. Judging from the links variable, this might be one of these cases, but as others suggested creating too many threads usually suggests a bad design. It might even get your system to run out of available resources.

Even when going over the network, your bandwidth is fixed, so unless you have direct access to an internet backbone it is highly unlikely that you'll get any performance benefit as most websites should respond with high speed.

Having said that, even if you slowed down thread creation you cannot control how much memory a thread will consume and at what time. i.e. All of your threads might start downloading data at the same time and even if you don't create any new ones, you will still run out of memory.

In conclusion, follow what others users suggested:

  • Use an ExecutorService
  • Read a bit more about Threads (Java Concurrency in practice, is a very good source)
  • Think why you are creating so many threads? Why do you expect that whatever you do will work faster?

PS. It would also help if you included why you need to create so many threads in your question



来源:https://stackoverflow.com/questions/26678072/java-needed-memory-for-new-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!