Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8227176

OOME with constructor reference but not with lambda

    XMLWordPrintable

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Any OS

      A DESCRIPTION OF THE PROBLEM :
      When I try create a new list with ::new function:

      public class NewFunctionTest {

      public static void main(String[] args) {
      int count = 100000;
      List<A> list = new ArrayList();
      for(int i = 0 ; i < count ; i++) {
      int key = ThreadLocalRandom.current().nextInt(100000);
      int value = ThreadLocalRandom.current().nextInt(1000000);
      list.add(new A(key, value));
      }
      System.out.println("init list done");
      Map<Integer, List<Integer>> map = new HashMap();
      for(A a : list) {
      map.computeIfAbsent(a.getKey(), ArrayList::new);
      }
      System.out.println("finish function");
      }

      @Getter
      @AllArgsConstructor
      public static class A {

      private int key;
      private int value;

      }
      }

      I got error:

      init list done
      Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
      at java.util.ArrayList.<init>(ArrayList.java:153)
      at com.tvd12.example.function.NewFunctionTest$$Lambda$1/1705736037.apply(Unknown Source)
      at java.util.HashMap.computeIfAbsent(HashMap.java:1127)
      at com.tvd12.example.function.NewFunctionTest.main(NewFunctionTest.java:25)

      But when i modify to k -> new ArrayList():

      public class NewFunctionTest {

      public static void main(String[] args) {
      int count = 100000;
      List<A> list = new ArrayList();
      for(int i = 0 ; i < count ; i++) {
      int key = ThreadLocalRandom.current().nextInt(100000);
      int value = ThreadLocalRandom.current().nextInt(1000000);
      list.add(new A(key, value));
      }
      System.out.println("init list done");
      Map<Integer, List<Integer>> map = new HashMap();
      for(A a : list) {
      map.computeIfAbsent(a.getKey(), k -> new ArrayList());
      }
      System.out.println("finish function");
      }

      @Getter
      @AllArgsConstructor
      public static class A {

      private int key;
      private int value;

      }

      }

      It's ok



      ACTUAL -
      init list done
      Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
      at java.util.ArrayList.<init>(ArrayList.java:153)
      at com.tvd12.example.function.NewFunctionTest$$Lambda$1/1705736037.apply(Unknown Source)
      at java.util.HashMap.computeIfAbsent(HashMap.java:1127)
      at com.tvd12.example.function.NewFunctionTest.main(NewFunctionTest.java:25)

      ---------- BEGIN SOURCE ----------
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      import java.util.concurrent.ThreadLocalRandom;

      import lombok.AllArgsConstructor;
      import lombok.Getter;

      public class NewFunctionTest {

      public static void main(String[] args) {
      int count = 100000;
      List<A> list = new ArrayList<>();
      for(int i = 0 ; i < count ; i++) {
      int key = ThreadLocalRandom.current().nextInt(100000);
      int value = ThreadLocalRandom.current().nextInt(1000000);
      list.add(new A(key, value));
      }
      System.out.println("init list done");
      Map<Integer, List<Integer>> map = new HashMap<>();
      for(A a : list) {
      map.computeIfAbsent(a.getKey(), ArrayList::new);
      }
      System.out.println("finish function");
      }

      @Getter
      @AllArgsConstructor
      public static class A {

      private int key;
      private int value;

      }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      import java.util.concurrent.ThreadLocalRandom;

      import lombok.AllArgsConstructor;
      import lombok.Getter;

      public class NewFunctionTest {

      public static void main(String[] args) {
      int count = 100000;
      List<A> list = new ArrayList<>();
      for(int i = 0 ; i < count ; i++) {
      int key = ThreadLocalRandom.current().nextInt(100000);
      int value = ThreadLocalRandom.current().nextInt(1000000);
      list.add(new A(key, value));
      }
      System.out.println("init list done");
      Map<Integer, List<Integer>> map = new HashMap<>();
      for(A a : list) {
      map.computeIfAbsent(a.getKey(), k -> new ArrayList<>());
      }
      System.out.println("finish function");
      }

      @Getter
      @AllArgsConstructor
      public static class A {

      private int key;
      private int value;

      }

      }

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            smarks Stuart Marks
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: