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

  • SEARCH
  • Home
  • 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 8767469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:49:16+00:00 2026-06-13T16:49:16+00:00

I am using java to read data from file, copy the data to smaller

  • 0

I am using java to read data from file, copy the data to smaller arrays and put these arrays in Hashtables. I noticed that Hashmap consumes more memory (about double) than what is in the original file! Any idea why?

Here is my code:

public static void main(final String[] args) throws IOException {
    final PrintWriter writer = new PrintWriter(new FileWriter("test.txt",
            true));
    for(int i = 0; i < 1000000; i++)
        writer.println("This is just a dummy text!");
    writer.close();

    final BufferedReader reader = new BufferedReader(new FileReader(
            "test.txt"));
    final HashMap<Integer, String> testMap = new HashMap<Integer, String>();
    String line = reader.readLine();
    int k = 0;
    while(line != null) {
        testMap.put(k, line);
        k++;
        line = reader.readLine();
    }
}
  • 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-13T16:49:17+00:00Added an answer on June 13, 2026 at 4:49 pm

    A map is an “extendable” structure – when it reaches its capacity it gets resized. So it is possible that say 40% of the space used by your map is actually empty. If you know how many entries will be in your map, you can use the ad hoc constructors to size your map in an optimal way:

    Map<xx,yy> map = new HashMap<> (length, 1);
    

    Even if you do that, the map will still use more space than the actual size of the contained items.

    In more details: HashMap’s size gets doubled when it reaches (capacity * loadFactor). Default load factor for a HashMap is 0.75.

    Example:

    • Imagine your map has a capacity (size) of 10,000 entries
    • You then put 7,501 entries in the map. Capacity * loadFactor = 10,000 * 0.75 = 7,500
    • So your hashmap has reached its resize threshold and gets resized to (capacity * 2) = 20,000, although you are only holding 7,501 entries. That wastes a lot of space.

    EDIT

    This simple code gives you an idea of what happens in practice – the output is:

    threshold of empty map = 8192
    size of empty map = 35792
    threshold of filled map = 8192
    size of filled map = 1181712
    threshold with one more entry = 16384
    size with one more entry = 66640
    

    which shows that if the last item you add happens to force the map to resize, it can artificially increase the size of your map. Admittedly, that does not account for the whole effect that you are observing.

    public static void main(String[] args) throws java.lang.Exception {
        Field f = HashMap.class.getDeclaredField("threshold");
        f.setAccessible(true);
    
        long mem = Runtime.getRuntime().freeMemory();
        Map<String, String> map = new HashMap<>(2 << 12, 1); // 8,192
        System.out.println("threshold of empty map = " + f.get(map));
        System.out.println("size of empty map = " + (mem - Runtime.getRuntime().freeMemory()));
    
        mem = Runtime.getRuntime().freeMemory();
        for (int i = 0; i < 8192; i++) {
            map.put(String.valueOf(i), String.valueOf(i));
        }
        System.out.println("threshold of filled map = " + f.get(map));
        System.out.println("size of filled map = " + (mem - Runtime.getRuntime().freeMemory()));
    
        mem = Runtime.getRuntime().freeMemory();
        map.put("a", "a");
        System.out.println("threshold with one more entry = " + f.get(map));
        System.out.println("size with one more entry = " + (mem - Runtime.getRuntime().freeMemory()));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using java to read a TSV file that is 4gb in size
I managed to read from a .xlsx file from java using Apache POI. Only
I'm trying to read and write the data to the server using java program.
I am trying to read file headers using java, I want to get file
I'm new to using Java Enums and I've read that replace IF logic that
How can I read and write comments in a Zip file, using Java? I
i want to read the 2010 excel file in java using apache poi api
I want to read XML data using XPath in Java, so for the information
I have spring application and using property file want to read the values from
Possible Duplicate: How do I read a resource file from a Java jar file?

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.