0

At times, I get "java.io.IOException: error=12, Cannot allocate memory" in my java application.

I want to reproduce it on my machine. Is there a way to reproduce this error? When will Runtime.exec throw java.io.IOException?

FYI, [root@ara ~]# java -version java version "1.6.0_22" Java(TM) SE Runtime Environment (build 1.6.0_22-b04) Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03, mixed mode)

2 Answers2

1

You can get your answer from this:

  1. Try it on an old OpenJDK version. It seems to be a JVM bug.
  2. Set overcommit_memory to 2 (don't overcommit)

    echo 2 > /proc/sys/vm/overcommit_memory

  3. Run the following program:

    public class ReproduceException {
    
       public static void main(String[] args) throws IOException {
            Runtime.getRuntime().exec("ls");
       }
    
    }
    
Community
  • 1
  • 1
0

I've linked a question that explains what causes this problem and gives at least 2 ways to fix it. From the explanation you should be able to figure out a strategy for causing it ... if that is still of interest after you've read the Q&A.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216