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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:26:36+00:00 2026-05-31T20:26:36+00:00

Is there any mechanism in Java to reduce the memory usage while reading large

  • 0

Is there any mechanism in Java to reduce the memory usage while reading large text files?

Almost every program I’ve come across uses String to read text files.But Java reserves space for each String literal.That’s why I think memory usage gets increased since all String objects are stored. All the classes of java.io deals with String. But if we’re not using StringBuilder then how can we reduce memory usage?

After all reducing memory usage is the primary concern of StringBuilder[since it’s not immutable like String]. Then how can we exploit its feature in Java I/O operation without using String i.e. without using something like this: sb.append([String object]);

  • 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-31T20:26:37+00:00Added an answer on May 31, 2026 at 8:26 pm

    Assume you have n strings, each of length 1 that you read from your input – for simplicity.

    Using operator+ on strigns while reading will create a String object each time you concatenate strings, so you get strings of length 1,2,3,…,n

    So the total memory usage of the strings combined is 1 + 2 + .. + n = O(n^2) in addition to the n strings you read from input

    while if you use StringBuilder to create the final string, you actually create n – for input [each of length 1] and one object for the final string – of size n, so total memory usage of 1 + 1 + .. + 1 + n = O(n)

    So, even if you use sb.append(String) – the space usage is asymptotcally beter then creating all intermediate strings – since you do not need to create intermediate String objects.

    In addition – the performance [time] should be better when using StringBuilder – both because you create less objects, and both because of lesser memory usage – the gc doesn’t need to work as hard as when concatenating strings naively.

    (*)Note that it is easy to see that the above still holds for any length of strings.

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

Sidebar

Related Questions

Is there any mechanism in C# that allows one to define and declare an
There is an open source desktop program which is written in Java (Swing) and
Is there any Java library to compute a Basic or Digest Http authentication? Jersey-Client
Is there any way to implement a URL mechanisim in asp.net like it has
Are there any future plans to replace selenium and watir with mechanize + johnson?
Is there any way to check whether a file is locked without using a
Is there any free or commercial component written in .NET (no COM interop) that
Is there any query which can return me the number of revisions made to
Is there any efficiency difference in an explicit vs implicit inner join? For example:
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox

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.