我们知道在java的伪共享概念中,“共享”的最小内存区域大小就是一个cache line。当两个以上CPU都要访问同一个cache line大小的内存区域时,就会引起冲突。
因此我们通常采用填充的办法,解决cache line 带来的速度慢的问题。
问题:
如何查看自己的HotSpot JVM的缓存行cache line的大小呢?我知道默认的是64字节,但是不同机子还是不一样的。
阿神2017-04-18 09:23:00
This cache line size is closely related to the CPU. jdk does not provide such an API.
I don’t know what the windows system is like. In the linux system, there are these files in the /sys/devices/system/cpu/cpu0/cache/
directory
coherency_line_size
level
number_of_sets
physical_line_partition
shared_cpu_list
shared_cpu_map
size
type
ways_of_associativity
The coherency_line_size records the cache line size.
It can also be obtained through `getconf LEVEL1_DCACHE_LINESIZE
64` on the command line.
If you want to get this number when the Java program is running, what you can think of now is to dynamically execute the shell or view the file when the program starts to get this number. Then set this number into the system property, which can be taken out and used later.