禁止延迟开启偏向锁jvm参数

jdk6默认开启偏向锁,但是是输入延时开启,也就是说,程序刚启动创建的对象是不会开启偏向锁的,几秒后后创建的对象才会开启偏向锁的

//关闭延迟开启偏向锁
-XX:BiasedLockingStartupDelay=0
//禁止偏向锁
-XX:-UseBiasedLocking 
//启用偏向锁
-XX:+UseBiasedLocking 

验证

  • 引入依赖包
<!--可以查看mark word-->
        <!-- https://mvnrepository.com/artifact/org.openjdk.jol/jol-core -->
        <dependency>
            <groupId>org.openjdk.jol</groupId>
            <artifactId>jol-core</artifactId>
            <version>0.10</version>
        </dependency>

示例代码

package test;

import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;
/**
 * @description: 偏向锁
 * @author: Seldom
 * @time: 2020/3/14 20:26
 */
@Slf4j(topic = "c.BaisedTes")
public class BaisedTes {
    public static void main(String[] args) throws InterruptedException {
        log.debug(ClassLayout.parseInstance( new Dog()).toPrintable());
        Thread.sleep(4000);
        log.debug(ClassLayout.parseInstance( new Dog()).toPrintable());
    }
}

未加参数前输出

20:49:38.609 [main] DEBUG c.BaisedTes - test.Dog object internals:
 OFFSET  SIZE               TYPE DESCRIPTION                               VALUE
      0     4                    (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4                    (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                    (object header)                           94 ef 00 f8 (10010100 11101111 00000000 11111000) (-134156396)
     12     4   java.lang.String Dog.name                                  null
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

20:49:42.614 [main] DEBUG c.BaisedTes - test.Dog object internals:
 OFFSET  SIZE               TYPE DESCRIPTION                               VALUE
      0     4                    (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
      4     4                    (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                    (object header)                           94 ef 00 f8 (10010100 11101111 00000000 11111000) (-134156396)
     12     4   java.lang.String Dog.name                                  null
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

加了参数之后

20:50:28.334 [main] DEBUG c.BaisedTes - test.Dog object internals:
 OFFSET  SIZE               TYPE DESCRIPTION                               VALUE
      0     4                    (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
      4     4                    (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                    (object header)                           94 ef 00 f8 (10010100 11101111 00000000 11111000) (-134156396)
     12     4   java.lang.String Dog.name                                  null
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

20:50:32.339 [main] DEBUG c.BaisedTes - test.Dog object internals:
 OFFSET  SIZE               TYPE DESCRIPTION                               VALUE
      0     4                    (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
      4     4                    (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                    (object header)                           94 ef 00 f8 (10010100 11101111 00000000 11111000) (-134156396)
     12     4   java.lang.String Dog.name                                  null
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

你可能感兴趣的:(java,JVM)