当前位置:网站首页>Learn cache lines and pseudo sharing of JVM slowly
Learn cache lines and pseudo sharing of JVM slowly
2022-06-26 05:21:00 【Bronze God】
Cache line (Cache Line)
The cache line is CPU The smallest unit of cache ,CPU The cache of is composed of multiple cache lines , A cache line is usually 64 Byte size . So a cache line can store 8 individual long Variable of type .
CPU The workflow of caching is to access the cache every time , If cache hits , The entire cache line will be read out and modified . This mechanism will cause pseudo sharing problem .
Pseudo sharing problem
The pseudo sharing problem arises in a multi-threaded environment , Threads A With threads B Modify different data in the same cache row at the same time , Cause cache to fail . They will overwrite each other, resulting in frequent cache misses ,
public class FalseShareTest implements Runnable {
// Number of concurrent threads :4
public static int NUM_THREADS = 4;
// The number of iterations :100 Ten thousand times
public final static long ITERATIONS = 1_000_000L;
// Array index number
private final int arrayIndex;
// VolatileLong An array of objects
private static VolatileLong[] longs;
// Total time spent
public static long SUM_TIME = 0l;
public FalseShareTest(final int arrayIndex) {
this.arrayIndex = arrayIndex;
}
private static void runTest() throws InterruptedException {
Thread[] threads = new Thread[NUM_THREADS];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(new FalseShareTest(i));
}
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
}
}
// Modify the object array
public void run() {
long i = ITERATIONS + 1;
while (0 != --i) {
longs[arrayIndex].value = i;
}
}
public final static class VolatileLong {
// Add volatile Make changes to variables visible to all threads
public volatile long value = 0L;
}
public static void main(final String[] args) throws Exception {
// perform 10 Time
for (int j = 0; j < 10; j++) {
// Build an array of experimental objects
longs = new VolatileLong[NUM_THREADS];
for (int i = 0; i < longs.length; i++) {
longs[i] = new VolatileLong();
}
// Start timestamp
final long start = System.currentTimeMillis();
// Run the test program
runTest();
// End timestamp
final long end = System.currentTimeMillis();
SUM_TIME += end - start;
}
System.out.println(" Total time :" + SUM_TIME);
}
}Running results

How to improve ?
The first way , Because a cache line can store 64 Bytes , That is to say 8 individual long Type variable , Then I'll put them in front and back 7 various long Type variable , Let field value, There is no village ahead , There's no store behind . This is also a high-performance queue Disruptoer The solution .
public final static class VolatileLong {
// fill
public long p1, p2, p3, p4, p5;
// Add volatile Make changes to variables visible to all threads
public volatile long value = 0L;
// fill
public long p6, p7, p8, p9, p10;
}There is a detail here , One long Type variables in 32 The bit operating system occupies 8 Bytes ,64 The bit operating system occupies 16 Bytes , That means we just need 5 individual long Variables can fill up 64 Bytes
The second way , Use JDK8 Newly added @Contented, Use @Contented The comment will add 128 Bytes of padding, Add... When startup is required -XX:-RestrictContented Option to take effect .
边栏推荐
- Thoughts triggered by the fact that app applications are installed on mobile phones and do not display icons
- Command line interface of alluxio
- C# 39. string类型和byte[]类型相互转换(实测)
- How to select the data transmission format of instant messaging application
- Protocol selection of mobile IM system: UDP or TCP?
- uni-app吸顶固定样式
- Implementation of IM message delivery guarantee mechanism (II): ensure reliable delivery of offline messages
- 瀚高数据库自定义操作符‘!~~‘
- [leetcode] 713: subarray with product less than k
- 【上采样方式-OpenCV插值】
猜你喜欢
Why does the mobile IM based on TCP still need to keep the heartbeat alive?

递归遍历目录结构和树状展现

How does P2P technology reduce the bandwidth of live video by 75%?

How to rewrite a pseudo static URL created by zenpart

基于SDN的DDoS攻击缓解
![[unity3d] rigid body component](/img/57/344aae65e4ac6a7d44b235584f95d1.png)
[unity3d] rigid body component

红队得分方法统计

Apktool tool usage document

Official image acceleration

Anaconda creates tensorflow environment
随机推荐
Tp5.0框架 PDO连接mysql 报错:Too many connections 解决方法
出色的学习能力,才是你唯一可持续的竞争优势
Chapter 9 setting up structured logging (I)
How to select the data transmission format of instant messaging application
Computer Vision Tools Chain
How to make your big file upload stable and fast?
CMakeLists.txt Template
cartographer_fast_correlative_scan_matcher_2d分支定界粗匹配
ECCV 2020 double champion team, take you to conquer target detection on the 7th
C# 39. string类型和byte[]类型相互转换(实测)
apktool 工具使用文档
cartographer_ local_ trajectory_ builder_ 2d
Codeforces Round #800 (Div. 2)
【ARM】在NUC977上搭建基于boa的嵌入式web服务器
Secondary bootloader about boot28 Precautions for ASM application, 28035
Why does the mobile IM based on TCP still need to keep the heartbeat alive?
Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
Cookie and session Basics
Sentimentin tensorflow_ analysis_ layer
The parameter field of the callback address of the payment interface is "notify_url", and an error occurs after encoding and decoding the signed special character URL (,,,,,)