How can I get the occupied space using a java object in the memory heap?

I study Java, I read the book First First Java, and I think it's great so far ... However, I really like to know how much space an object occupies a lot of memory, you know ... bits, bytes, kb, etc. .d. How can I do that?

This is my first question here ... I hope this is not a stupid question, and also sorry for my English skills, thanks.

+3
source share
2 answers

This is much more complicated than just using free memory calculations (which do not take into account garbage collections and new allocations of other threads). Take a look at:

http://www.javaspecialists.eu/archive/Issue142.html

.

- EDIT -

( , , , ).

java 1.5 (. Instrumentation.getObjectSize()).

+1

, , JVM , .

, , .

:

public class Monkey {
  int arms;
  Animal parent;
}

.. 1 1 int , 32- 8 .

0

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


All Articles