7

UPDATE

I tried the answer of

In Java, what is the best way to determine the size of an object?

but when I tried the code below, it gave me 16 for below code

public class MemoryTest {

    public static void main(String[] args) {
        System.out.println(ObjectSizeFetcher.getObjectSize(new ArrayList<>().add(new DummyObject())));
    }
}

and 16 is not what I am looking for, so I asked this question again. Is this question still duplicated question?


I want to check the memory usage of an object, and here is what I tried. (java 8)

// Dummy Object class
public class DummyObject {
    int dummy;
}

// Separate class to check memory
public class MemoryTest {
    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();

        long before = runtime.totalMemory() - runtime.freeMemory();
        DummyObject obj = new DummyObject();
        long after = runtime.totalMemory() - runtime.freeMemory();

        System.out.println(after - before);
    }
}

and result is "0"

Can anyone tell me
1. why result is 0
2. what is the proper way of measuring memory usage of an object

thanks in advance


UPDATE

When I tried the same code in Android project as below, (SDK version 23)

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Runtime runtime = Runtime.getRuntime();

        long before = runtime.totalMemory() - runtime.freeMemory();
        DummyObject obj = new DummyObject();
        long after = runtime.totalMemory() - runtime.freeMemory();

        System.out.println("test memory: " + (after - before));
    }
}

the result was "680"

What makes this difference and why?

Community
  • 1
  • 1
user2932011
  • 81
  • 1
  • 6
  • 1
    if you want alternative ways for calculating object size read [this](http://stackoverflow.com/questions/9368764/calculate-size-of-object-in-java), [this](http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object) – codiacTushki Oct 21 '15 at 07:13
  • @user2932011 See my answer below for memory profiling.. I think you are not looking for the memory footprint of a single object in memory. (Or are you?) If you are looking for the size in memory of all instances of a Class then you need memory profiling. – Spyros K Oct 21 '15 at 15:29

2 Answers2

1

The Interface Instrumentation provides a method called getObjectSize()

Returns an implementation-specific approximation of the amount of storage consumed by the specified object. The result may include some or all of the object's overhead, and thus is useful for comparison within an implementation but not between implementations. The estimate may change during a single invocation of the JVM.

Returns an implementation-specific approximation of the amount of storage consumed by the specified object

Maybe this can help you.

Documentation of Interface Instrumentation

Patrick
  • 12,336
  • 15
  • 73
  • 115
0

As stated in the javadoc the method returns

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

See also: Java - Runtime.freeMemory()

As the methods return approximations it is difficult to spot small differences. In order to get more info about the memory used by an application you can see more about memory by using a profiling tool. For example check The Eclipse Memory Analyzer https://eclipse.org/mat/

Update: Answer to the update. The methods are implemented in the JVMs, which means implementation and perhaps approximation accuracy can vary between different JVMs, that is the results return on the JVM running in windows or Linux and android could return different values.

Also the totalMemory() based on the javadoc: http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#totalMemory%28%29

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.

So you are also using another approximation which also varies with time. Using only the freeMemory() to calculate the reduction of free memory could be perhaps better.

Community
  • 1
  • 1
Spyros K
  • 2,480
  • 1
  • 20
  • 37
  • To check small differences is the problem for getting result 0, I created DummyObject[1000] array and put 1000 objects in that. But still, I get 0. Can you explain why I'm still getting 0? – user2932011 Oct 22 '15 at 00:42
  • I guess the object size is important also. Try increasing the size of the array and waiting a bit. The "approximation" clause of the javadoc allows for a wide range of accuracy, this function simply does not allow for precise calculations. – Spyros K Oct 22 '15 at 12:29