Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8951933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:45:58+00:00 2026-06-15T13:45:58+00:00

The Java code is as follows: Random r = new Random(1234697890); HashMap<Integer, List<Integer>> map

  • 0

The Java code is as follows:

Random r = new Random(1234697890);
HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
List<Integer> list = new ArrayList<Integer>();

for(int i=0;i<100000;i++){
    for(int j=0;j<1000;j++){
        list.add(r.nextInt(100000));
    }
    map.put(i, list);
    map.remove(i);
}

when i reaches 37553 , java.lang.OutOfMemoryError: Java heap space happens.
It seems that garbage collection does not happen in the loop.
Now I wonder how to fix the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:45:59+00:00Added an answer on June 15, 2026 at 1:45 pm

    Try rewriting the code as follows and you should not get OOME’s …

    Random r = new Random(1234697890);
    HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
    
    for(int i=0;i<100000;i++){
        List<Integer> list = new ArrayList<Integer>();
        for(int j=0;j<1000;j++){
            list.add(r.nextInt(100000));
        }
        map.put(i, list);
        map.remove(i);
    }
    

    The problem with your original code is that:

    • you only create one list,
    • you keep adding more and more elements to it, and
    • that list only becomes garbage when the code completes … because it is “in scope” the whole time.

    Moving the list declaration inside the loop means that a new ArrayList is created and filled in each loop iteration, and becomes garbage when you start the next iteration.


    Someone suggested calling System.gc(). It won’t help at all in your case because there is minimal1 garbage to be collected. And in general it is a bad idea because:

    • the GC is guaranteed to have run immediately before an OOME is thrown,
    • the JVM can figure out better than you can when is the best (i.e. most efficient) time to run the GC,
    • your call to System.gc() may be totally ignored anyway. The JVM can be configured so that calls to System.gc() are ignored.

    1 – The pedant in me would like to point out that map.put(i, list); map.remove(i); is most likely generating an Integer object that most likely becomes garbage. However, this is “chicken feed” compared to your indefinitely growing ArrayList object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Java code is as follows: String s = 0.01; int i = Integer.parseInt(s);
Code for SQL class as follows: import java.text.SimpleDateFormat; import java.util.Date; import android.content.ContentValues; import android.content.Context;
I am generating decision trees in Weka in Java code as follows: J48 j48DecisionTree
I have some java code as follows: try { String outString =java -jar C:\\ami\\bin\\ImmediateSubmit.jar
The very simple Java code as follows has the weird output, but the same
I tried the java.io.Console API using eclipse. My sample code follows. package app; import
This Java code compiles fine, but when I try to run it I get:
Will Java code built and compiled against a 32-bit JDK into 32-bit byte code
IN JAVA CODE IN JSP as below i m getting null value for fieldnoOfRecords.
My Java code must get string HH:MM from console and needs to operate with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.