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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:39:17+00:00 2026-06-01T20:39:17+00:00

I know a char in java is 2bytes. but if load some chars in

  • 0

I know a char in java is 2bytes. but if load some chars in a list, they will cost 87B to save a char. the test is like the following:

There is a file “source” containing 995328 lines. every line is just a character:’a’. (so it will cost almost 2MB in java to save all chars).

There are two sleep method called in my source code and I use top command to check the memory usage at any moment.

The RSIZE value when running the first sleep(10000) method is 25M, and 108M when running the second sleep method. so per String(which is just an “a” ) cost:(108MB-25MB)/995328=87B. I don’t know why a String “a” cost so many memory!!! Could any one can tell my why?

public static void main(String[] args) throws Exception{
    File file = new File("source");
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line = null;
    List<String> list = new ArrayList<String>();
    Thread.sleep(10000); 
    while((line = br.readLine())!=null){
        list.add(line);
    }
    Thread.sleep(10000);

}
  • 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-01T20:39:20+00:00Added an answer on June 1, 2026 at 8:39 pm

    @Amir is right to say that there are better ways than top (for instance, hprof is included with the JDK) to measure memory usage, but there are some deeper issues that confound your memory numbers.

    1. You aren’t closing either file or br. This is the big one. Each one of these objects is a wrapper around a bunch of native code to interface with your operating system’s file I/O libraries. Those resources include file handles and cached buffers, so some of the data you’re reading from the file is counted twice in memory usage – once in the cache attached to br, and once in list.
    2. Each string isn’t really just a sequence of characters. The JRE maintains a pointer to a character array, a start index, and a length, along with other data. The pointer to the character array is 8 bytes, the start index is 4 bytes, and the length is 4 bytes. I’m sure that I’m leaving out some fields, but even this conservative estimate gives 16 bytes of overhead for a string, ignoring the actual characters in the string.
    3. The list variable has overhead as well. There is a backing array, where each slot is a pointer (8 more bytes), and there are plenty of empty slots. As the backing array grows to accommodate the lines, the ArrayList class leaves some extra space because the array resize (that is, create a new array and copy over all the elements from the old array) is expensive, and each of the empty slots is 8 bytes on a 64-bit system.
    4. The numbers returned by top include garbage. The garbage collector varies between JVM implementations and versions, but typically it collects new objects quickly and only collects old objects if there is memory pressure. So all the extra arrays left over from resizing the backing store of the ArrayList are quite likely still in memory, and counting towards the top numbers. Since those arrays are large to start with (most likely there is one that is at least 500K slots, each an 8-byte pointer), this boosts the program’s total memory usage.

    N.B. I talked about 8-byte pointers above, assuming a 64-bit system. On a 32-bit system, everything I said holds, except that pointers are only 4 bytes.

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

Sidebar

Related Questions

I'd like to know what is the last char that exists in java? I
i know java API function charAt for example String s=dato char r=s.charAt(0); r is
I tried to convert some C code to Java, but it is working slightly
I know in java, if you have a char variable, you could do the
in java regex,use [^x] to matching not with one char. i want to know,how
I know that a char and an int are calculated as being 8 bytes
I know that buffer overruns are one potential hazard to using C-style strings (char
Would anyone happen to know how to convert type LPTSTR to char * in
Does any one know how can I cut the 1st 3 char in a
I would like to know if there is any way to return an char

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.