I have a list of 100 strings. For each string I need to perform 3 tasks. So I introduced Concurrent threads
Class A implements Runnable
{
String str;
A(String s)
{
str = s;
}
public static void main(String[] args)
{
Runnable runnable = null;
//For Each of my strings
for(String s:MyList)
{
runnable = new A(s);
new Thread(runnable).start();
}
}
@Override
public void run() {
task1();
task2();
task3();
}
}
This Runs into an Exception.
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
How do I overcome this and restrict the Thread size?
I tried Using this.
ExecutorService pool = Executors.newFixedThreadPool(10);
pool.execute(runnable);
It doesnt work.
"java.lang.OutOfMemoryError: Java heap space" means pretty much exactly what it says - the heap (the place you allocate new Java objects from) is out of space. Not sure what you are doing in those tasks but it must be using a lot of memory.
You can either figure out what is taking up so much memory and reduce it. Or you can - as you mention - reduce the number of concurrent operations, which would presumably use less memory. Or you can use something like:
java -Xmx1024M A
To manually specify the amount of memory made available to the JVM. But note that just increasing the memory limit without understanding why it's using so much memory is not a good idea. I would suggest looking for a bug in these taskN() calls first.
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.