如何测量Java方法的内存使用率 [英] How can I measure memory usage of java method

查看:89
本文介绍了如何测量Java方法的内存使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

听起来很奇怪,但仍然如此.

想象一下我有一个Java方法

sounds strange, but still.

Imagine I have a java method

boolean myMethod(Object param1, Object param2) { 
   // here can be some simple calculations. can be some services invokation
   // or calls to DB. doesn't matter
  return someValue;
}

如何测量JVM用于执行此方法的内存量?实际上有多少内存使用此方法执行该方法?

How can I measure how much memory JVM uses to execute this method ? How much memory actually uses this method for its execution ?

有没有工具,或者也许我可以在方法中添加一些额外的代码以查看已用的内存?

Is there any tools or I maybe I can add some extra code to my method to be able to see used memory ?

我正在使用JConsole来监视JVM,我也知道我们可以使用java.lang.Runtime来查看freeMemory,但实际上我无法弄清楚我的方法在此工具中使用了多少内存.

I am using JConsole to monitor JVM, I also know that we can use java.lang.Runtime to see freeMemory, but actually I can't figure out how much memory my method uses with this tools.

推荐答案

测量方法的每次调用的真实大小并不像听起来那样简单,并且JVM不支持它.复杂性来自于a)因为JVM被设计为隐藏此低级细节,并且b)JVM在任何时间点可能在飞行中具有相同方法的多个变体,这听起来很疯狂,但这也是事实.有时,该方法将被内联,进行更大量的优化,甚至以特殊的方式重新编译,以支持该方法在运行时进行热交换(!),从而可以从根本上改变其运行时大小.

Measuring the true size of of each invocation of a method is not as simple as it sounds and the JVM does not support it. The complexity comes about a) because the JVM was designed to hide this low level detail and b) the JVM may have more than one variation of the same method in flight at any point of time, which sounds nuts but it is also true. Some times the method will have been inlined, or optimised more heavily or even recompiled in a special way to support hot swapping of the method while it is running (!) which can radically change its runtime size.

测量方法调用使用的堆量

鉴于方法调用不使用堆,因此很想说0个字节.但是,可能要知道方法调用分配的任何新对象的大小.可以回答,但是对象在方法调用和线程之间共享.因此,必须非常清楚他们想要测量什么,并注意不要重复计算两次.有一些库可以给出Java中Object的大小,它们通常通过反射和启发式工作,或者包装sun.misc.Unsafe来检查指向对象及其数据的指针.例如 http://code.google.com/p/javabi-sizeof/

Given that method invocations do not use the heap, it is tempting to say 0 bytes. However one may want to know the size of any new object that was allocated by a method call. This can be answered, however objects are shared across method calls and threads. So one has to be very clear about what they want to measure, and take care to not count it twice. There are libraries that will give the size of an Object in Java, they usually work either by reflection and a heuristic or they wrap sun.misc.Unsafe to inspect pointers to the object and its data. For example http://code.google.com/p/javabi-sizeof/.

测量方法的分配率

YourKit具有出色的工具,此处描述了该工具,用于跟踪每个对象的分配从源头上报告其总规模和费率.对于找出是谁引起GC流失非常有用.

YourKit has an excellent tool described here for tracking every object allocation at its source point and reporting back their total size and rates. Very useful for finding out who is causing GC churn.

测量每个方法调用使用的堆栈量

JVM不会发布此信息,并且如前所述,它会根据当前处于活动状态的优化以及所使用的运行时编译器而有所不同.

The JVM does not publish this information, and as mentioned before it will vary depending on the optimizations that are currently active and which runtime compiler is being used.

替代方法是使用启发式.类似于计算方法中使用了多少个变量,然后将计数乘以4得出以字节为单位的答案(假设每个变量的大小为4字节,即int).显然,这种启发式方法是有缺陷的,可以改进,但它足够简单,可以给出一个快速且具有代表性的答案.

The alternative is to use a heuristic. Something like counting up how many variables are used within the method and multiply the count by 4 to give an answer in bytes (which assumes that every variable is 4 bytes in size, ie an int). Obviously this heuristic is flawed and could be improved, but it is simple enough to give a quick and fairly representative answer.

这篇关于如何测量Java方法的内存使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆