如何查看java程序使用了多少资源?

发布于 2024-07-26 05:30:05 字数 295 浏览 2 评论 0原文

如何查看java程序使用了多少资源?

在java中,创建一个大而好的程序非常容易,它几乎可以完成您想要它做的所有事情,但有一点副作用是,也很容易错误地间接分配大量内存和CPU周期(即,让我们只使用“new “几次在错误的地方)。

如果程序应该在资源非常有限的环境中运行,比如说嵌入式设备或者手机(J2ME、Android 等), 了解您的程序对资源的需求程度至关重要。

注意:由于过去几年我的主要关注点是 c,所以我可能会在这里遗漏一些非常简单的东西,所以请不要假设太多;)

谢谢 约翰

How do I check how much resources a java program uses?

In java it is quite easy to create a big nice program that does practically everything you want it to, but a little side effect is that is also very easy to indirectly allocate to much memory and cpu cycles by mistake (i.e. let's just use "new" a couple of times in the wrong place).

And if the program are supposed to run in a environment with very limited amount of resources, let's say something embedded or maybe a mobile phone (J2ME, Android etc etc),
it is crucial to know how resource hungry your program is.

Note: Since my main focus has been c the last years I could be missing something very simple here, so please don't assume to much ;)

Thanks
Johan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那片花海 2024-08-02 05:30:05
maxMemory();
totalMemory();
freeMemory();
maxMemory();
totalMemory();
freeMemory();
a√萤火虫的光℡ 2024-08-02 05:30:05

您似乎对自己真正需要的东西有些困惑。 在你的问题中,我感觉到了一些不安:“Java会在我背后吞噬多少资源来咬我?”。

用道格拉斯·亚当斯的话来说,答案是:“不要惊慌!” :-)。

说真的,Java 不一定比“本机”应用程序使用更多的资源,因此您可能不必担心。

快速概述:

  • 由于 JVM,Java 程序将具有固定的内存开销,即即使是一个简单的程序也会占用几 MB 的内存。 然而,对于更复杂的软件来说,这不会增长,因此在实践中通常不是问题。 此外,并行运行的多个 Java VM 将共享大部分 RAM。
  • CPU:一般来说,Java 程序将使用与等效的本机程序相同的 CPU 时间。 Java 并没有需要更多的根本原因。 垃圾收集确实有一些开销,但通常并不重要(并且手动内存管理也有开销)。
  • 也就是说,意外的对象保留或不必要的大对象之类的事情可能会导致内存问题。 但这是当问题出现时你要解决的问题。

简短版本:别担心,只需像往常一样操作即可:使其运行、使其正确运行、使其快速运行(通过分析)。

You appear to be somewhat confused about what you really need. In your question I sense some uneasiness about "how many resources will Java gobbly up behind my back to bite me later?".

The answer to this is, to paraphrase Douglas Adams: "Dont' panic!" :-).

Seriously, Java does not necessarily use much more resources than a "native" application, so you probably need not worry.

A quick rundown:

  • Java programs will have a fixed memory overhead because of the JVM, i.e. even a trivial program will eat a few MB of memory. This however is will not grow for more complex software, so it's not usually a problem in practice. Also, several Java VMs running in parallel will share much of the RAM.
  • CPU: In general, a Java program will use the same CPU time as an equivalent native program. There's not fundamental reason why Java would need more. Garbage collection does have some overhead, but it's not usually significant (and manual memory management also has an overhead).
  • That said, things like accidental object retention or unneccessarily large objects can cause memory problems. But that's a problem you tackle when it arises.

Short version: Don't worry, just do as you always do: Make it run, make it run correctly, make it run fast (by profiling).

荒人说梦 2024-08-02 05:30:05

如果您试图在程序之外捕获这些属性以进行性能测试; 您可能还想使用 Java Profiler。 那里有很好的分析器可用。 Java 6 还提供了一种方法来执行此类活动。
例如
J 分析器
如果你想要更多这样的工具; 让我知道。

if you are trying to capture these attributes outside the program for performance tests; you might also wanna use Java Profilers. There are good profilers available out there. Java 6 also provides one to do these kinds of activities.
for example
J Profiler
If you want more such tools; let me know.

贱人配狗天长地久 2024-08-02 05:30:05

从 Java 1.6 开始,出现了一组允许监视 JVM 的工具。 其中之一是 jmap。 你可以看一下。

我不太确定它如何应用于 J2ME,但我猜您可以将相同的技术应用于计算机上的 J2ME 模拟器(使用完整的 JVM,而不是 KVM)。

Since Java 1.6 there's a set of tools allowing to monitor the JVM. One of them is jmap. You could take a look at it.

I am not quite sure how it applies to J2ME, but I would guess you could apply the same technique to the J2ME emulator on a computer (using full JVM, not a KVM).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文