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 6790175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:41:38+00:00 2026-05-26T17:41:38+00:00

I found out the memory my program is increasing is because of the code

  • 0

I found out the memory my program is increasing is because of the code below, currently I am reading a file that is about 7GB big, and I believe the one that would be stored in the hashset is lesson than 10M, but the memory my program keeps increasing to 300MB and then crashes because of OutofMemoryError. If it is the Hashset problem, which data structure shall I choose?

    if(tagsStr!=null) {
        if(tagsStr.contains("a")||tagsStr.contains("b")||tagsStr.contains("c")) {
            maTable.add(postId);
        }
    } else {
        if(maTable.contains(parentId)) {
            //do sth else, no memories added here
        }
    }
  • 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-05-26T17:41:39+00:00Added an answer on May 26, 2026 at 5:41 pm

    You’ve either got a memory leak or your understanding of the amount of string data that you are storing is incorrect. We can’t tell which without seeing more of your code.

    The scientific solution is to run your application using a memory profiler, and analyze the output to see which of your data structures is using an unexpectedly large amount of memory.


    If I was to guess, it would be that your application (at some level) is doing something like this:

    String line;
    while ((line = br.readLine()) != null) {
        // search for tag in line
        String tagStr = line.substring(pos1, pos2);
        // code as per your example
    }
    

    This uses a lot more memory than you’d expect. The substring(...) call creates a tagStr object that refers to the backing array of the original line string. Your tag strings that you expect to be short actually refer to a char[] object that holds all characters in the original line.

    The fix is to do this:

        String tagStr = new String(line.substring(pos1, pos2));
    

    This creates a String object that does not share the backing array of the argument String.

    UPDATE – this or something similar is an increasingly likely explanation … given your latest data.


    To expand on another of Jon Skeet’s point, the overheads of a small String are surprisingly high. For instance, on a typical 32 bit JVM, the memory usage of a one character String is:

    • String object header for String object: 2 words
    • String object fields: 3 words
    • Padding: 1 word (I think)
    • Backing array object header: 3 words
    • Backing array data: 1 word

    Total: 10 words – 40 bytes – to hold one char of data … or one byte of data if your input is in an 8-bit character set.

    (This is not sufficient to explain your problem, but you should be aware of it anyway.)

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

Sidebar

Related Questions

I have written several program and found out that when compiled in 64bit, the
I found out that build time of C# solution with many projects gets much
I found out how to generate an image in code-behind based on some input
I found out that I can use a different theme in an C# WPF
I found out that HTML form supports only two enctype types. They are application/x-www-form-urlencoded
I found out that the FormWizard only __init__ 's once, when the url is
I found out recently that JList (finally!) was generified in JDK7. Why JTree and
Just found out that the video output of the iPad is not a system
I found out that in order to open a Gmail compose screen you'd have
The example program below compiles two in-memory assemblies. The first compilation works fine. The

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.