前几日,下了班在家时突然收到监控报警,线上一组业务机器 CPU 被打满至 100%,为了保持服务的稳定运行,临时采取了升级配置、加机器等粗暴的方法将当晚扛了过去。
Java 为什么对常量的修改没有生效?
写这篇文章的起因是前几天同事改了一个常量类中的提示,发布到测试环境后没有生效,正好看 《Java 解惑(谜题 93: 类的战争)》 提到了这个问题,所以写篇文章记录一下。
以下均使用命令行进行演示,至于为什么没有使用 IDE 后面会提到。
先看一个简单的 Constants 类:
1 | /** |
Java Mission Control
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collection framework built into the Oracle JDK. It allows Java administrators and developers to gather detailed low level information about how the Java Virtual Machine (JVM) and the Java application are behaving. Java Mission Control is an advanced set of tools that enables efficient and detailed analysis of the extensive of data collected by Java Flight Recorder. The tool chain enables developers and administrators to collect and analyze data from Java applications running locally or deployed in production environments.
题图为监测 pull-readhub.me 的示例数据。
Java 对象指针压缩
很久之前就看到过 Java 对象指针压缩这个技术,只是一直没具体想为什么要偏移 3 位,好吧,最近才知道原因是因为大多数 JVM 实现都是采用 8 位对齐,所以二进制位中的后三位都是 0。
JOL
我把示例代码放了一份在 Github 上,jol-samples,想看的同学可以 clone 下来把每个 example 跑一跑就能看到 JVM 对象在内存中布局的一些信息,包括对象头占多大、字节怎么对齐、字段在内存中的顺序不一定与声明的顺序一致、涉及到继承时字段是如何存放的、带 transient
修饰符的字段被特殊处理、Java8 新增的 @Contended
注解的作用、JVM 的平台相关性、对象头中的 mark word 及 class word、轻量锁、偏向锁、重量锁、Hashcode、GC 需要用到的对象引用图、哈希碰撞时转换为链表以及 Java8 中极端情况下转换为红黑树、观察 mark word 中的 age 字段的值在 GC 后的变化 等等,我没有一个一个写出来,因为 25 个例子太多了,设计到的知识点也比较多,需要一些前置知识才能理解。