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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:05:17+00:00 2026-06-15T23:05:17+00:00

For whatever reason, I wanted to see how many objects I could create and

  • 0

For whatever reason, I wanted to see how many objects I could create and populate a LinkedList with. I used Runtime.getRuntime().freeMemory() to get the approximation of free memory in my JVM. I wrote this:

public static void main(String[] arg) {
    Scanner kb = new Scanner(System.in);
    List<Long> mem = new LinkedList<Long>();
    while (true) {
        System.out.println("Max memory: " + Runtime.getRuntime().maxMemory() + ". Available memory: " + Runtime.getRuntime().freeMemory() + " bytes. Press enter to use more.");
        String s = kb.nextLine();
        if (s.equals("m"))
        for (int i = 0; i < 1000000; i++) {
            mem.add(new Long(Long.MAX_VALUE));
        }
    }
}

If I write in m, the app adds a million Long objects to the list. You would think the more objects (to which we have references, so can’t be gc’ed), the less free memory. Running the code:

Max memory: 1897725952. Available memory: 127257696 bytes.
m
Max memory: 1897725952. Available memory: 108426520 bytes.
m
Max memory: 1897725952. Available memory: 139873296 bytes.
m
Max memory: 1897725952. Available memory: 210632232 bytes.
m
Max memory: 1897725952. Available memory: 137268792 bytes.
m
Max memory: 1897725952. Available memory: 239504784 bytes.
m
Max memory: 1897725952. Available memory: 169507792 bytes.
m
Max memory: 1897725952. Available memory: 259686128 bytes.
m
Max memory: 1897725952. Available memory: 189293488 bytes.
m
Max memory: 1897725952. Available memory: 387686544 bytes.

The available memory fluctuates. How does this happen? Is the GC cleaning up other things (what other things are there on the heap to really clean up?), is the freeMemory() method returning an approximation that’s way off? Am I missing something or am I crazy?

  • 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-15T23:05:18+00:00Added an answer on June 15, 2026 at 11:05 pm

    As you consume more memory, the heap grows. Note that freeMemory() reports usage relative to the current heap size, which can grow. Check totalMemory() instead of maxMemory(). If I’m recalling this right.

    Or are you asking what is being allocated that can possibly be garbage collected?

    • The Random you allocate in the loop
    • Objects allocated during the process of reading from an OS stream, parsing into characters, then parsing into lines
    • Possibly some stuff allocated during the call to check free memory itself
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get this image uploading script right, but for whatever reason it
For whatever reason, I can't get XSLTProcessor() to work all the time. Here is
For whatever reason, I can't get this function repeat. Once it gets the setTimeout,
For whatever reason, I have been unable to get any table cells to have
For whatever reason, I can't seem to get the list of types in a
For whatever reason, I cannot get knockout to trigger an update on visible whenever
I've searched and searched and for whatever reason I just can't seem to get
For whatever reason, though this code does refresh the page, no fields get posted...
On whatever reason this is not working (says 'file not found'), set in=c:\myprogram\_save cd
For whatever reason, std::cout does not display anything with my application. The description of

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.