Windows 上的 Java 需要记录 CPU 负载和类似操作系统的特定性能信息
有一个在 Windows 机器上运行的 Java 库,需要记录有关操作系统的信息,如 CPU 负载、JVM 占用的内存等,我很确定 Java 本身无法获取这些信息,因为它是特定于操作系统的。
该库的日志中需要此信息,以便向客户端指出由于该库无法获取足够的资源而导致某些操作失败。
不可能选择 JVM,即我们不能要求我们的客户应该使用实现 Windows 操作系统特定功能的特定 JVM。
是否有可以通过 JNI 使用的 Windows 库 (DLL) 或 API?
我们还可以自己用 C++ 或 C# 实现 DLL,我需要在哪里查看如何最有效地完成此操作?
编辑:我需要访问有关 JVM 本身进程的数据,我猜我只能通过本机 Windows API 获取这些数据。所以我想我必须实现一个小型 C 程序并通过 JNI 将其链接为本机 DLL。有什么建议吗?
There is a Java library running on windows machines that needs to log information about the OS like CPU load, memory occupied by the JVM etc. which I'm quite sure Java itself cannot obtain, because it is OS specific.
This information is needed in the logs of this library in order to point out to clients that certain operations failed because the library could not obtain enough resources.
It is not possible to choose the JVM, i.e. we cannot demand that our clients should use a specific JVM that does implement windows OS specific functionality.
Is there a windows library (DLL) or API that could be used via JNI?
We could also implement a DLL in C++ or C# ourselves, where would I need to look to see how this could be done most effectively?
Edit: I need access to data about the process of the JVM itself, which I can get through the native Windows API only, I guess. So I think I'll have to implement a small C program and link this as native DLL via JNI. Any tips on that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于内存部分,看一下的几个内存相关的方法运行时。
要获取占用的内存,请尝试以下操作:
我还没有尝试过,但对于 CPU 负载,请查看 以下。
getSystemLoadAverage()
方法应该可以做到这一点。希望这有帮助!
For the memory part, look at several memory related methods of the Runtime.
To get the occupied memory, try the following:
I have not tried this yet, but for the CPU load take a look at the following.
The
getSystemLoadAverage()
method should do it.Hope this helps!
如果 java.lang.Runtime 不能满足您的需求,您可以采用 JNI 路线并设计一个能够准确提供您所需的 Java API。
我不知道您可以(使用 JNI)包装任何开箱即用的 DLL,但您需要的数据位于注册表中,即所谓的“性能计数器”。在 Linux 上,您可以从 /proc 读取数据。
有关 性能计数器。
If your needs aren't met by java.lang.Runtime, you could go the JNI route and design a Java API that provides exactly what you need.
I don't know of any out of the box DLLs that you could wrap (with JNI) but the data you're after is in the Registry as the so-called "performance counters". On linux you could read the data from /proc.
See the MSDN site for more info on Performance Counters.