当前位置:网站首页>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 .
边栏推荐
- Learn from small samples and run to the sea of stars
- C# 40. byte[]与16进制string互转
- vscode config
- 国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
- Create SSH key pair configuration steps
- Beidou navigation technology and industrial application of "chasing dreams in space and feeling for Beidou"
- Apktool tool usage document
- 定位设置水平,垂直居中(多种方法)
- AD教程系列 | 4 - 创建集成库文件
- Procedural life
猜你喜欢

FastAdmin Apache下设置伪静态

Official image acceleration

cartographer_ optimization_ problem_ 2d

cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching

apktool 工具使用文档

国务院发文,完善身份认证、电子印章等应用,加强数字政府建设

Setting pseudo static under fastadmin Apache

Second day of deep learning and tensorfow

Status of processes and communication between processes

Practical cases | getting started and mastering tkinter+pyinstaller
随机推荐
Why does the mobile IM based on TCP still need to keep the heartbeat alive?
百度API地图的标注不是居中显示,而是显示在左上角是怎么回事?已解决!
6.1 - 6.2 公钥密码学简介
RESNET practice in tensorflow
LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
瀚高数据库自定义操作符‘!~~‘
【ARM】讯为rk3568开发板buildroot添加桌面应用
Serious hazard warning! Log4j execution vulnerability is exposed!
C# 39. Conversion between string type and byte[] type (actual measurement)
Redis installation on Linux
Red team scoring method statistics
LeetCode 19. 删除链表的倒数第 N 个结点
How to rewrite a pseudo static URL created by zenpart
skimage.morphology.medial_axis
data = self._ data_ queue. get(timeout=timeout)
Classic theory: detailed explanation of three handshakes and four waves of TCP protocol
PHP之一句话木马
uni-app吸顶固定样式
Keras actual combat cifar10 in tensorflow