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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:42:07+00:00 2026-05-16T02:42:07+00:00

Below is the code snippet to examine the memory public class TestFreeMemory { public

  • 0

Below is the code snippet to examine the memory

public class TestFreeMemory {

    public static void main(String ... args){

        Runtime rt = Runtime.getRuntime();
        System.out.println("Free Memory (Before GC): " + rt.freeMemory());

        rt.gc();
        System.out.println("Free Memory (After GC1): " + rt.freeMemory());

        rt.gc(); // Second time to ensure results are consistent 
                 // MAY BE has collected all non-reachable objects
        System.out.println("Free Memory (After GC2): " + rt.freeMemory());

        String s = new String("abcd");
        Integer i = new Integer(12345);
        System.out.println("Free Memory (After String Creation): " + rt.freeMemory());
        // Why is freeMemory not reflecting the memory consumed by two objects
    }

}

and the output is

Free Memory (Before GC): 1859672
Free Memory (After GC1): 1911768
Free Memory (After GC2): 1911768
Free Memory (After String Creation): 1911768

Why is freeMemory not reflecting the memory consumed by two objects ?


To be more clear, the question is about getMemory() call not showing up expected result even though two objects are created and not related to GC. Two GC calls are made just to try to make sure the numbers of getMemory() call are correct.. and BTW, there’s no GC call after objects are created.. so pls Note that i am not trying for a GC after objects are created ..

  • 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-16T02:42:07+00:00Added an answer on May 16, 2026 at 2:42 am

    Memory Management on HotSpot JVM:

    Another desirable garbage collector
    characteristic is the limitation of
    fragmentation. When the memory for
    garbage objects is freed, the free
    space may appear in small chunks in
    various areas such that there might
    not be enough space in any one
    contiguous area to be used for
    allocation of a large object.
    One
    approach to eliminating fragmentation
    is called compaction, discussed among
    the various garbage collector design
    choices below.

    Memory Management in HotSpot JVM (PDF Format).

    This behavior can be very dependent on the particular implementation of the garbage collection. For example:

    Parallel Mark Compact

    • Stop-the-world
    • Heap divided into fixed-size chunks (> 2kb now, will likely increase or be
      subject to ergonomics)
    • Chunk is unit of live data summarization
    • Parallel mark
    • Record live data addresses in external bitmap
    • Find per chunk live data size
    • Find dense chunks, i.e., ones that are (almost) full of live objects

    I’ve made this sample (with abusive String concatenation to use up more memory):

    public class TestFreeMemory {
    
     static void allocateSomeMemory(){
      long[][] array = new long[400][400];
     }
    
        public static void main(String ... args){
    
            Runtime rt = Runtime.getRuntime();
    
            allocateSomeMemory(); // once we leave, our array is not reachable anymore 
            System.out.println("Free Memory (Before GC): " + rt.freeMemory());     
            rt.gc();
            System.out.println("Free Memory (After GC): " + rt.freeMemory());
    
            String a = new String("A");
            for(int i = 0; i < 100; i++){
             a+="B";
            }
    
            System.out.println("Free Memory (After String Creation): " + rt.freeMemory());
            // Less free memory expected.
        }
    
    }
    

    Output:

    Free Memory (Before GC): 3751800

    Free Memory (After GC): 5036104

    Free Memory (After String Creation):
    5012048


    If I use a relatively small number of iterations in the loop (say 10), the extra space does not show up in freeMemory(), and I’d get something like this:

    Free Memory (Before GC): 3751800

    Free Memory (After GC): 5036040

    Free Memory (After String Creation):
    5036040

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

Sidebar

Related Questions

I have below code snippet SimpleDateFormat dateFormat = new SimpleDateFormat( yyyy-MM-dd hh:mm:ss.SSS); String processedContentDate=2012-04-10
i have below code snippet in my legacy project DetachedCriteria subCriteria = DetachedCriteria.forClass(Employee.class); subCriteria.setProjection(Projections.distinct(Property.forName
The below code snippet best explains what I am trying to achieve: [Test] public
In the below code snippet, template<typename T1> void func(T1& t) { cout << all
The below code snippet shows the implementation of memmove() . void my_memmove(void* dest, const
For the below code snippet, how do I initialize instances of class Enemy with
Below is code snippet in instance method String x = new StringBuffer().append(a).append(b).append(c).toString() i am
I am using the below code snippet to get the html string of a
i am not understanding how below code snippet can be made thread safe. class
I am using below code snippet to validate my input string with: only capital

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.