Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance size of Java object are different in different JVM

Here I found a problem that instance size of same class are not same in different version of JVM (it's 40 in 1.6.0_21 and 24 in 1.6.0_31). even though, the code are same. Do you anyone encounter similar problem before? Or do you have any suggestions?

JDK 1.6.0_21

# java -version 
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) 64-Bit Server VM (build 17.0-b16, mixed mode)

# java obj.ObjectSize &

# jps | grep ObjectSize
27251 ObjectSize

# jmap -histo 27251 | grep US_ASCII
 145:             1             40  sun.nio.cs.US_ASCII

JDK 1.6.0_31

# java -version
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)

# java obj.ObjectSize &

# jps | grep ObjectSize
26645 ObjectSize

# jmap -histo 26645 | grep US_ASCII
161:             1             24  sun.nio.cs.US_ASCII

ObjectSize.java

package obj;
import java.util.concurrent.TimeUnit;
import sun.nio.cs.US_ASCII;

public class ObjectSize {

    public static void main(String[] args) {
        US_ASCII as = new US_ASCII();
        System.out.println(as);

        try {
            TimeUnit.MINUTES.sleep(5);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
like image 767
Jason Hu Avatar asked Feb 17 '23 22:02

Jason Hu


1 Answers

I think what you are encountering is just a fact originating in how compiled programming languages work, especially if they run inside a VM.

Changes in the implementation of the virtual machines are allowed to behave differently, e.g. producing Java byte code of different sizes -- as long as they keep to the same Java API.

Is the difference in memory usages really that big? If the increase in memory size is actually a problem I would dare to suggest that you already had a memory problem in the first place.

If you were working at 50% capacity with one VM and are now hitting the cap with the other I guess you need to undertake some deeper changes in your code. Or throw more hardware at the problem. ;)

like image 160
fgysin Avatar answered Feb 25 '23 09:02

fgysin