当前位置:网站首页>偏向锁、轻量级锁测试工具类级相关命令
偏向锁、轻量级锁测试工具类级相关命令
2022-07-29 05:27:00 【魔道不误砍柴功】
依赖包
依赖包推荐使用 0.9 版本的,其他版本可能有不一样的效果,珍重。
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.9</version>
</dependency>
演示代码
package com.xxl.job.admin.mytest;
import org.openjdk.jol.info.ClassLayout;
import java.util.concurrent.TimeUnit;
public class ObjectMarkWordDemo {
public void test() {
Object obj = new Object();
synchronized (obj) {
System.out.println("111");
}
synchronized (obj) {
System.out.println("111");
}
synchronized (obj) {
System.out.println("111");
}
}
public static void main(String[] args) throws InterruptedException {
// 打印 JVM 相关的信息
// System.out.println(VM.current().details());
// 打印每个对象是否为 8 的整数倍大小
// System.out.println(VM.current().objectAlignment());
MyObject myObject = new MyObject();
System.out.println(Integer.toHexString(myObject.hashCode()));
new Thread(()->{
// 在 myObject 对象头上进行加锁(默认直接干到轻量级锁,这里我非要把他干到偏向锁状态)
// 默认是开启偏向锁的,所以这里我们只需要把开启偏向锁的延迟时间修改成 0 方便看效果 -XX:+BiasedLockingStartupDelay=0
synchronized (myObject) {
// 给这个线程加锁,并且还设置了偏向线程 ID
System.out.println(ClassLayout.parseInstance(myObject).toPrintable());
}
}).start();
TimeUnit.MICROSECONDS.sleep(500);
// 锁被释放了,所以这里打印的肯定是无锁状态 001
System.out.println(ClassLayout.parseInstance(myObject).toPrintable());
}
}
class MyObject {
}
运行结果:
76fb509a
# WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
com.xxl.job.admin.mytest.MyObject object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) e8 29 c3 0a (11101000 00101001 11000011 00001010) (180562408)
4 4 (object header) 03 00 00 00 (00000011 00000000 00000000 00000000) (3)
8 4 (object header) 44 c1 00 f8 (01000100 11000001 00000000 11111000) (-134168252)
12 4 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
# WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
com.xxl.job.admin.mytest.MyObject object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) e8 29 c3 0a (11101000 00101001 11000011 00001010) (180562408)
4 4 (object header) 03 00 00 00 (00000011 00000000 00000000 00000000) (3)
8 4 (object header) 44 c1 00 f8 (01000100 11000001 00000000 11111000) (-134168252)
12 4 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
常用命令
设置 JVM 堆大小
-Xms10m -Xmx10m
查询 JVM 启动运行了那些命令
java -XX:+PrintCommandLineFlags -version
关闭对象头中类指针的压缩配置
-XX:-UseCompressedClassPointers
查询偏向锁是否开启
java -XX:+PrintFlagsInitial | grep BiasedLock
运行结果:
intx BiasedLockingBulkRebiasThreshold = 20 {
product}
intx BiasedLockingBulkRevokeThreshold = 40 {
product}
intx BiasedLockingDecayTime = 25000 {
product}
// 然后偏向锁开启之后默认会有4s钟的延迟,测试的时候需要注意,可以将这个值设置成0,方便查看效果
intx BiasedLockingStartupDelay = 4000 {
product}
bool TraceBiasedLocking = false {
product}
// JVM 默认开启了偏向锁的设置
bool UseBiasedLocking = true {
product}
开启偏向锁设置
-XX:+UseBiasedLocking -XX:BiasedLockingStartupDelay=0
边栏推荐
猜你喜欢

day13_ Under multithreading

Huawei switch ce12808 import and export configuration file

Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (2) -- modk operation environment construction

注解(Annotation)

Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?

day03_1_流程控制

On defect description style

C语言内存-栈与堆使用

如何画出优秀的架构图

Hongke shares | why EtherCAT is the best solution to improve the performance of the control system?
随机推荐
Online multiplayer chat room based on UDP communication
案例补充、ATM
DDoS攻击与CC攻击的区别
What is the basic principle of Library collision and library collision attack
5G服务化接口和参考点
Floating point addition and subtraction method of vivado IP core floating point
Solution for website being suspended
Clickhouse failed to import CSV without error but no data
Advanced socket programming (options and control information)
软件包设置成——>YUM源
【面试题】2022年最新软件测试面试题(400道)【附带答案】持续更新...
如何判断业务被DDoS攻击?又会造成哪些危害?
What is DNS amplification attack
VMware虚拟机在物理机win10系统下如何连接外网
Conversion of fixed-point number to floating-point number of vivado IP core
比较单片机3种时钟电路方案
day02_基本语法
Hongke | uses jesd204 serial interface to bridge analog and digital worlds at high speed
day13_多线程下
四、 局域网和城域网