How can I check object memory usage in java

UPDATE

I tried to answer

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

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

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'm looking for, so I asked this question again. Does this question duplicate the question?


I want to check the memory usage in 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 the result is "0"

Can anyone tell me 1. Why the result is 0 2. What is the correct way to measure memory usage for an object

early


UPDATE

When I tried the same code in an Android project as shown 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"

How is this different and why?

+5
source share
2 answers

The Instrumentation interface provides a method called getObjectSize()

Returns an approximate approximation of the amount of storage consumed by the specified object. The result may include some or all of the service data of the object and, therefore, is useful for comparison within the implementation, but not between implementations. The score may change during a single JVM call.

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

Perhaps this may help you.

Interface Toolkit Documentation

0
source

As stated in javadoc, the method returns

"The approximate value of the total memory currently available for future allocated objects, measured in bytes."

See also: Java - Runtime.freeMemory ()

As methods return approximations, it is difficult to detect small differences. To get more information about the memory used by the application, you can see more about memory using the profiling tool. For example, check out the Eclipse memory analyzer https://eclipse.org/mat/

Update: Reply to the update. The methods are implemented in the JVM, which means implementation, and perhaps the approximation accuracy can vary between different JVMs, i.e. returning results to JVMs running on Windows or Linux, and the android can return different values.

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

Gets the total amount of memory in the Java virtual machine. the value returned by this method may change over time, depending on the host environment.

Please note that the amount of memory required to store an object of any type may be implementation dependent.

So you also use a different approximation, which also changes over time. Using only freeMemory() to calculate the free memory reduction might be better.

0
source

Source: https://habr.com/ru/post/1234168/


All Articles