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

HashMap consumes too much memory

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_112"
      Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      HashMap consumes a lot of memory for no apparent reason.


      import java.util.HashMap;
      import java.util.Map;
      import java.util.Random;

      public class HashMapTest {

      public static void main(String[] args) throws InterruptedException {
      // TODO Auto-generated method stub
      Map<String, String> hashMapTest = new HashMap<>(16, 0.9f);


      Random random = new Random();

      String empty = "";

      for(int i = 0; i< 3_500_000; i++)
      hashMapTest.put(String.valueOf(random.nextInt()), "test");

      System.out.println("completed java7 loop");
      Thread.sleep(1000000l);

      }

      }

      run the above code in Java 7 and Java 8 and watch how much memory is consumed by both after requesting GC. Java 8 hovers around 400 M while Java 7 hovers around 3 M.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just execute the above code Java 8 runtime and Java 7 runtime. You can use visual VM to monitor the amount of memory consumed. After the loop completes request GC and compare how much memory each runtime is consuming

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Lower memory consumption
      ACTUAL -
      Too much memory being held. When I did rough calculations to see how much it should consume it was definitely not 400 MB

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import java.util.HashMap;
      import java.util.Map;
      import java.util.Random;

      public class HashMapTest {

      public static void main(String[] args) throws InterruptedException {
      // TODO Auto-generated method stub
      Map<String, String> hashMapTest = new HashMap<>(16, 0.9f);


      Random random = new Random();

      String empty = "";

      for(int i = 0; i< 3_500_000; i++)
      hashMapTest.put(String.valueOf(random.nextInt()), "test");

      System.out.println("completed java7 loop");
      Thread.sleep(1000000l);

      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      No workaround, our app is build on Java 8 using JavaFX 8 and we have no workaround

      SUPPORT :
      YES

        1. 7u45.PNG
          49 kB
          Fairoz Matte
        2. 8u112.PNG
          47 kB
          Fairoz Matte
        3. 9eab149.PNG
          49 kB
          Fairoz Matte
        4. HashMapTest.java
          0.5 kB
          Abhijit Roy

            chegar Chris Hegarty
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: