rocketmq icon indicating copy to clipboard operation
rocketmq copied to clipboard

把循环index的类型由long改为int,让循环前放置Safepoint,效果会不会比Thread.sleep(0)更好?

Open somePeopleFireAndWood opened this issue 2 years ago • 9 comments

如题 MappedFile 类,507~527行这个循环,把循环的i、j改成long类型。删除掉522行的Thread.sleep(0)。 修改

这么改,关于gc方面的优化,效果会不会更好?代码看着也会更加优雅

somePeopleFireAndWood avatar Aug 26 '22 09:08 somePeopleFireAndWood

虽然是sleep 0,但因操作系统时钟精度原因,实际会sleep 几毫秒,不同的系统,精度可能值会不一样; 删除之后,就没有sleep的效果了;

fuyou001 avatar Aug 26 '22 10:08 fuyou001

虽然是sleep 0,但因操作系统时钟精度原因,实际会sleep 几毫秒,不同的系统,精度可能值会不一样; 删除之后,就没有sleep的效果了;

@fuyou001 请问为什么要有sleep的效果呢?没有sleep的效果不是更好吗? 另外还有的问题是,如果追求sleep的效果,sleep(0)有没有可能被jit优化啊?

somePeopleFireAndWood avatar Aug 26 '22 10:08 somePeopleFireAndWood

@foxeatsspicy can you make a test code ?

francisoliverlee avatar Aug 30 '22 03:08 francisoliverlee

@foxeatsspicy can you make a test code ?

@francisoliverlee the person doesn't seem to exist,page is 404

sunheyi6 avatar Aug 30 '22 14:08 sunheyi6

safepoint,安全点。

Yong0724 avatar Sep 09 '22 01:09 Yong0724

虽然是sleep 0,但因操作系统时钟精度原因,实际会sleep 几毫秒,不同的系统,精度可能值会不一样; 删除之后,就没有sleep的效果了;

@fuyou001

No. It is not true. The intention of sleep(0) is to introduce safe points. When there is an ongoing GC, this thread will take fewer milliseconds to reach a safe point. This would be helpful if the available memory runs low. See https://stackoverflow.com/questions/67232199/how-to-reduce-time-taken-on-threads-reaching-safepoint-sync-state

lizhanhui avatar Sep 09 '22 03:09 lizhanhui

@duhenglucky The pull request is not completed yet. Need more insights involved.

lizhanhui avatar Sep 09 '22 03:09 lizhanhui

continuing to pay attention

linlinjie avatar Sep 09 '22 11:09 linlinjie

虽然是sleep 0,但因操作系统时钟精度原因,实际会sleep 几毫秒,不同的系统,精度可能值会不一样; 删除之后,就没有sleep的效果了;

@fuyou001

No. It is not true. The intention of sleep(0) is to introduce safe points. When there is an ongoing GC, this thread will take fewer milliseconds to reach a safe point. This would be helpful if the available memory runs low. See https://stackoverflow.com/questions/67232199/how-to-reduce-time-taken-on-threads-reaching-safepoint-sync-state

Maybe Thread.yield() is more clear for the usage?

gmshake avatar Sep 15 '22 10:09 gmshake

实际上,这里的改动无效:for循环里有方法调用,就不存在这个问题。方法调用栈退出的地方,是可以加check safepoint的。

for循环退不出的场景,也可以通过-XX:+UseCountedLoopSafepoints解决,不需要改代码。 @lizhanhui

kimmking avatar Mar 05 '23 12:03 kimmking