Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Memory Usage

 
Ranch Hand
Posts: 431
2
Java
  • Mark post as helpful
  • send pies
  • 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: 70711
288
  • Mark post as helpful
  • send pies
  • 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.
 
Marshal
Posts: 25965
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 431
2
Java
  • Mark post as helpful
  • send pies
  • 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: 12436
269
  • Mark post as helpful
  • send pies
  • 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: 70711
    288
    • Mark post as helpful
    • send pies
    • 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: 12915
    64
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • 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.
     
    Seriously? That's what you're going with? I prefer this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic