-
Notifications
You must be signed in to change notification settings - Fork 26.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ISSUE #10020] add MemorySafeLinkedBlockingQueue #10021
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 3.0 #10021 +/- ##
============================================
- Coverage 65.75% 65.71% -0.04%
Complexity 320 320
============================================
Files 1218 1222 +4
Lines 53052 53174 +122
Branches 8037 8057 +20
============================================
+ Hits 34883 34943 +60
- Misses 14368 14417 +49
- Partials 3801 3814 +13 ☔ View full report in Codecov by Sentry. |
@dragon-zhang That's a good idea. Can you provide some results of memory analysis, such as |
Hi, I believe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
:) |
Let me ask a question, where should I add description to |
nice idea |
我很困惑,为什么MemorySafeLinkedBlockingQueue#hasRemainedMemory()中要使用usage.getCommitted()与maxFreeMemory相比较。 |
嗯,思路挺好的~不过,我这里有些疑问以及思考,仅供交流:MemorySafeLBQ 是限制每个队列对象的使用内存大小,但是使用 Instrumetation.getObjectSize() 只能检查这个对象的占用,这个对象引用的对象的内存占用米有算进去。实际业务使用引用非常复杂, 放入队列的一般是门面对象,主要内存占用不在这里。然后是 MemoryLimitedLBQ 通过 MemoryMXBean 查看当前堆内存实际占用,但是没有考虑 GC 的因素,当前内存占用也许大部分都是可以回收的的,也许 YoungGC 之后就有那么多内存可以使用了。总体来看,MemorySafeLBQ 限制并没有限制住,MemoryLimitedLBQ 限制的可能太死。这个可能还是需要底层 JDK + JNI 去实现,查看队列中的对象的存活时间,结合整个堆对象的存活时间以及占用情况,判断是否真的内存紧缺了。或者是限制一个队列用的内存大小,但是检查里面放入对象的本身内存以及所有引用内存。 不过这些,其实引入 Project Valhalla 以及 Project Loom 之后就都好说了,不需要线程池了。但是这个限制内存占用的队列还是有意义的,我觉得可以向 Java 社区提一下这个建议 |
我觉得也是有点问题,可用内存应该是 long available = usage.getMax() - usage.getUsed(); 吧 根据这个进行限制 而不是commit |
CN EN |
上文提到:"当发现JVM剩余内存<256MB时,MSLBQ会拒绝写入" 个人肯定通过可用内存来判断MSLBQ是否可写这一方案的价值。 |
Wait a minute, I'll make sure. Maybe I should use |
MemoryUsage#getMax() - MemoryUsage#getUsed()即可 |
I've test in local machine, Thanks for pointing out my mistake, I'll fix it. |
-Xms -Xmx配置不相同的时候,可用内存=Runtime.maxMemory() - Runtime.totalMemory() + Runtime.freeMemory() 这样可用内存应该更准确 |
CN EN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
MemoryUsage#getMax() 表示可用于内存管理的最大内存量(以字节为单位)。它的值可能是未定义的。如果已定义,最大内存量可能会随时间变化。如果定义了max ,则已使用和已提交的内存量将始终小于或等于max 。如果尝试增加已使用的内存,即使used <= max仍然为 true(例如,当系统的虚拟内存不足时),内存分配可能会失败。 max定义的内存可能会由于系统内存不足分配失败,是不是commited - used 更妥😆😆😆😆😆 |
问题的起源在于剩余内存如何较为科学与准确的计算,从而引申出一系列的内存概念,一共有四个分别是:init、used、commited、max,如果jvm启动之时没有将xmx、mxs两者配置为同值,运行之时堆大小是会随着实际情况根据高低水位线进行占用内存的扩缩容,commit此时代表的是此时jvm堆内存总量,max - used带来的计算可能会导致内存再分配,你的担心在于硬件条件不允许。 |
|
||
private static volatile long maxAvailable; | ||
|
||
private static final ScheduledExecutorService SCHEDULER = Executors.newSingleThreadScheduledExecutor(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This scheduledExecutorService can be managed by Dubbo Framework. I will patch it later. #10178
sure, thanks!
…------------------ Original ------------------
From: Albumen Kevin ***@***.***>
Date: Mon,Jun 20,2022 10:01 AM
To: apache/dubbo ***@***.***>
Cc: dragon-zhang ***@***.***>, Mention ***@***.***>
Subject: Re: [apache/dubbo] [ISSUE #10020] add MemorySafeLinkedBlockingQueue (PR #10021)
@AlbumenJ commented on this pull request.
In dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/MemoryLimitCalculator.java:
> +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * ***@***.*** java.lang.Runtime#freeMemory()} technology is used to calculate the + * memory limit by using the percentage of the current maximum available memory, + * which can be used with ***@***.*** MemoryLimiter}. + * + * @see MemoryLimiter + * @see <a href="https://github.com/apache/incubator-shenyu/blob/master/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/MemoryLimitCalculator.java">MemoryLimitCalculator</a> + */ +public class MemoryLimitCalculator { + + private static volatile long maxAvailable; + + private static final ScheduledExecutorService SCHEDULER = Executors.newSingleThreadScheduledExecutor();
This scheduledExecutorService can be managed by Dubbo Framework. I will patch it later.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
What is the purpose of the change
Can completely solve the OOM problem caused by {@link java.util.concurrent.LinkedBlockingQueue}, does not depend on {@link java.lang.instrument.Instrumentation} and is easier to use than {@link MemoryLimitedLinkedBlockingQueue}.
Brief changelog
Verifying this change
Checklist