What is the memory consumption of an object in Java?

前端 未结 12 2334
眼角桃花
眼角桃花 2020-11-22 03:20

Is the memory space consumed by one object with 100 attributes the same as that of 100 objects, with one attribute each?

How much memory is allocated for an object?<

相关标签:
12条回答
  • 2020-11-22 03:39

    In case it's useful to anyone, you can download from my web site a small Java agent for querying the memory usage of an object. It'll let you query "deep" memory usage as well.

    0 讨论(0)
  • 2020-11-22 03:40

    no, 100 small objects needs more information (memory) than one big.

    0 讨论(0)
  • The question will be a very broad one.

    It depends on the class variable or you may call as states memory usage in java.

    It also has some additional memory requirement for headers and referencing.

    The heap memory used by a Java object includes

    • memory for primitive fields, according to their size (see below for Sizes of primitive types);

    • memory for reference fields (4 bytes each);

    • an object header, consisting of a few bytes of "housekeeping" information;

    Objects in java also requires some "housekeeping" information, such as recording an object's class, ID and status flags such as whether the object is currently reachable, currently synchronization-locked etc.

    Java object header size varies on 32 and 64 bit jvm.

    Although these are the main memory consumers jvm also requires additional fields sometimes like for alignment of the code e.t.c.

    Sizes of primitive types

    boolean & byte -- 1

    char & short -- 2

    int & float -- 4

    long & double -- 8

    0 讨论(0)
  • 2020-11-22 03:43

    The total used / free memory of an program can be obtained in the program via

    java.lang.Runtime.getRuntime();
    

    The runtime has several method which relates to the memory. The following coding example demonstrate its usage.

    package test;
    
     import java.util.ArrayList;
     import java.util.List;
    
     public class PerformanceTest {
         private static final long MEGABYTE = 1024L * 1024L;
    
         public static long bytesToMegabytes(long bytes) {
             return bytes / MEGABYTE;
         }
    
         public static void main(String[] args) {
             // I assume you will know how to create a object Person yourself...
             List < Person > list = new ArrayList < Person > ();
             for (int i = 0; i <= 100000; i++) {
                 list.add(new Person("Jim", "Knopf"));
             }
             // Get the Java runtime
             Runtime runtime = Runtime.getRuntime();
             // Run the garbage collector
             runtime.gc();
             // Calculate the used memory
             long memory = runtime.totalMemory() - runtime.freeMemory();
             System.out.println("Used memory is bytes: " + memory);
             System.out.println("Used memory is megabytes: " + bytesToMegabytes(memory));
         }
     }
    
    0 讨论(0)
  • 2020-11-22 03:45

    Each object has a certain overhead for its associated monitor and type information, as well as the fields themselves. Beyond that, fields can be laid out pretty much however the JVM sees fit (I believe) - but as shown in another answer, at least some JVMs will pack fairly tightly. Consider a class like this:

    public class SingleByte
    {
        private byte b;
    }
    

    vs

    public class OneHundredBytes
    {
        private byte b00, b01, ..., b99;
    }
    

    On a 32-bit JVM, I'd expect 100 instances of SingleByte to take 1200 bytes (8 bytes of overhead + 4 bytes for the field due to padding/alignment). I'd expect one instance of OneHundredBytes to take 108 bytes - the overhead, and then 100 bytes, packed. It can certainly vary by JVM though - one implementation may decide not to pack the fields in OneHundredBytes, leading to it taking 408 bytes (= 8 bytes overhead + 4 * 100 aligned/padded bytes). On a 64 bit JVM the overhead may well be bigger too (not sure).

    EDIT: See the comment below; apparently HotSpot pads to 8 byte boundaries instead of 32, so each instance of SingleByte would take 16 bytes.

    Either way, the "single large object" will be at least as efficient as multiple small objects - for simple cases like this.

    0 讨论(0)
  • 2020-11-22 03:46

    It appears that every object has an overhead of 16 bytes on 32-bit systems (and 24-byte on 64-bit systems).

    http://algs4.cs.princeton.edu/14analysis/ is a good source of information. One example among many good ones is the following.

    enter image description here

    http://www.cs.virginia.edu/kim/publicity/pldi09tutorials/memory-efficient-java-tutorial.pdf is also very informative, for example:

    enter image description here

    0 讨论(0)
提交回复
热议问题