2

Below code prints the same value for the memory? Ideally after object allocation free memory should be reduced. Is my understanding correct?

System.out.println(Runtime.getRuntime().freeMemory());
Person p = new Person();
p.setFirstName("bob");
System.out.println(Runtime.getRuntime().freeMemory());
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2775185
  • 1,099
  • 3
  • 17
  • 30
  • 2
    Perhaps [this](http://stackoverflow.com/questions/3571203/what-is-the-exact-meaning-of-runtime-getruntime-totalmemory-and-freememory) will help. – Josh M Jan 01 '14 at 05:47

3 Answers3

3

As the Runtime javadoc states, freeMemory() returns an approximation to the total amount of memory currently available for future allocated objects, meaning you cannot expect a precise value.

Amila
  • 5,195
  • 1
  • 27
  • 46
2

This is because JVM created Person in TLAB (thread-local allocation buffer), you can find short explanation in Peter Lawrey's comment and more detail from one of HotSpot developers here https://blogs.oracle.com/daviddetlefs/entry/tlab_sizing_an_annoying_little. What actually happening is as follows:

Before you ran the first Runtime.freeMemory the current thread had already created a TLAB in heap and Runtime.freeMemory showed free memory without TLAB. When you created a Person it was placed in TLAB because TLAB had free space in it, so from the point of view of Runtime.freeMemory nothing changed.

Try to turn off this feature when running the test

java -XX:-UseTLAB ...

and it will show the correct Person's size

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • +1 The TLAB allocates memory in chunks so each thread can create object concurrently (from their own chunks) Turning TLAB off is slower for multiple threaded applications, but gives you accurate accounting. – Peter Lawrey Jan 01 '14 at 09:56
  • So when we run the first runtime.freeMemory() a chunk was already allocated for our thread, right? – Evgeniy Dorofeev Jan 01 '14 at 10:28
1

Please go through the official documentation for free memory and total memory

docs

Free Memory:

public long freeMemory() : Returns the amount of free memory in the Java Virtual Machine. Calling the gc method may result in increasing the value returned by freeMemory.

Returns: an approximation to the total amount of memory currently available for future allocated objects, measured in bytes.

Total Memory:

public long totalMemory() Returns the total amount of memory in the Java virtual machine. The value returned by this method may vary over time, depending on the host environment.

Note that the amount of memory required to hold an object of any given type may be implementation-dependent.

Returns: the total amount of memory currently available for current and future objects, measured in bytes.

If you want to find out the memory used by your Object, then you have to calculate by subtracting totalMemory - freeMemory as i shown below.

   System.out.prinltn("Memory Used by the Person Object" +
Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90