Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Memory Usage

 
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written below piece of code to check the memory usage but there is no difference in the memory after creating the objects. Why this is so?


Output is :


254741016
254741016


I am creating multiple objects, so, should it decrease the free memory available? What is the reason for this behaviour?
 
Marshal
Posts: 74645
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:. . . I am creating multiple objects . . .

How many objects are you creating after the execution starts? Not during the class loading process.
 
Sheriff
Posts: 26950
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:I am creating multiple objects



No, you aren't. I only see one object being created. If you wanted to create 1000 objects, you made the only possible choice of code which actually doesn't create any objects at all.
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell and Paul. I have modified the code to create object but still it doesn't show any difference in memory before and after creating objects:

 
Saloon Keeper
Posts: 13483
304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Likely because the (JIT-)compiler performs some optimizations:

1) It sees that you're doing absolutely nothing with the created strings, so the VM just doesn't allocate them.
2) It sees that no objects escape the method call, so it might allocate all of them on the stack. I'm not sure if freeMemory() includes stack memory.

Try the following and see if it makes any difference:

Note the following changes:

  • Use an instance field to keep track of the objects, so they don't get allocated on the stack.
  • Use Object instead of String, because the compiler may optimize strings more aggressively.
  • Run the garbage collector before executing the test.
  • Let the current thread sleep for a while to increase chances of the garbage collector actually running.
  • Clear the array after running the test.
  • Run the test multiple times to see if there is a difference between the runs.

  • Here's the output for one particular execution of this program on my machine:

    Interestingly, when I run this code my first test run doesn't seem to consume any memory, but the amount of free memory is very different from the two other test runs. That implies to me that the first few calls to freeMemory() are inaccurate and maybe the JIT compiler must run before it starts returning accurate results. This is just conjecture though. There could be any number of reasons it returns different results. Note that the documentation says that freeMemory() just returns an approximation.

    The other two test runs consistently return a memory usage of 41952 bytes. That is slightly under 42 bytes per object. The fact that the number isn't evenly divisible by 1000 implies that some hidden bookkeeping is done somewhere in the VM.
     
    Campbell Ritchie
    Marshal
    Posts: 74645
    335
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The freeMemory() method is a bit vague about it, but you are probably right that it gives you available heap space. I thought the same as you, that the allocations are probably optimised away, and such objects being used only in restricted scope would be created on the stack. I thought escape analysis became a standard optimisation in Java6.
    I tried you code and got −41952 too. I also noticed JShell's heap became smaller after the first run.
     
    lowercase baba
    Posts: 13018
    66
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:

  • Run the garbage collector before executing the test.


  • Please correct me if things have changed, but I have always been told this only suggests to the JVM that now might be a good time to run gc...it doesn't actually force it to run.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic