FileChannel Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

36 Views Asked by At

I had an outofmemory error when launching the batch on the server: below are the details of the error as well as the piece of code written in java, can you help me please , the error is triggered at this line:

rowString = new String(barray, 0, endOfLinePos, charSet).split("\n");

knowing that my file contains 461500 lines and the number of bytes used is 230989152.

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.StringCoding.safeTrim(StringCoding.java:89)
at java.lang.StringCoding.decode(StringCoding.java:230)
at java.lang.String.<init>(String.java:463)
at com.test(Maclasse.java:215)
at com.test(Maclasse.java:181)
at com.test(Maclasse.java:144)
at com.test(Maclasse.java:119)



   try ( FileChannel inputChannel = new FileInputStream("/opt/input.txt").getChannel();)
 {
    MappedByteBuffer buffer = inputChannel.map( FileChannel.MapMode.READ_ONLY, 0L, inputChannel.size());
    buffer.position(0);
    long availableBytes = inputChannel.size();
    int endOfLinePos = (int)availableBytes;
    int get = Math.min(buffer.remaining(), endOfLinePos);
    byte[] barray = new byte[endOfLinePos];
    buffer.get(barray, 0, get);
    Charset charSet = Charset.forName("UTF-8");
    String[] rowString = new String[buffer.remaining()];
   
    rowString = new String(barray, 0, endOfLinePos, charSet).split("\n");   
    Arrays.asList(rowString).stream().map(line -> {
        try ...
0

There are 0 best solutions below