确定java内存使用情况 [英] determining java memory usage

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

问题描述

嗯。 Java中的内存使用是否有任何入门?我本以为Sun或IBM本来会有一篇关于这个主题的好文章,但我找不到任何看起来非常可靠的文章。我有兴趣知道两件事:

Hmmm. Is there a primer anywhere on memory usage in Java? I would have thought Sun or IBM would have had a good article on the subject but I can't find anything that looks really solid. I'm interested in knowing two things:


  1. 在运行时,弄清楚我的包中的类在给定时间内使用了多少内存

  2. 在设计时,估算各种内容的一般内存开销要求,例如:


    • 需要多少内存开销一个空对象(除了其字段所需的空间)

    • 创建闭包时需要多少内存开销

    • 内存开销是多少像ArrayList这样的集合需要

  1. at runtime, figuring out how much memory the classes in my package are using at a given time
  2. at design time, estimating general memory overhead requirements for various things like:
    • how much memory overhead is required for an empty object (in addition to the space required by its fields)
    • how much memory overhead is required when creating closures
    • how much memory overhead is required for collections like ArrayList

我可能创建了数十万个对象我想成为一个好邻居,不要过度浪费RAM。我的意思是我并不在乎我是否比最佳情况(无论是那种情况)使用的内存多10%,但是如果我实现了一些使用5倍内存的东西,如果我做了一个简单的话改变,我想使用更少的内存(或者能够为固定数量的内存创建更多的对象)。

I may have hundreds of thousands of objects created and I want to be a "good neighbor" to not be overly wasteful of RAM. I mean I don't really care whether I'm using 10% more memory than the "optimal case" (whatever that is), but if I'm implementing something that uses 5x as much memory as I could if I made a simple change, I'd want to use less memory (or be able to create more objects for a fixed amount of memory available).

我发现了一些文章( Java专家通讯和来自 Javaworld )和其中一个内置类 java.lang.instrument.getObjectSize()声称要测量内存使用的近似值(??),但这些看起来都很模糊......

I found a few articles (Java Specialists' Newsletter and something from Javaworld) and one of the builtin classes java.lang.instrument.getObjectSize() which claims to measure an "approximation" (??) of memory use, but these all seem kind of vague...

(是的,我意识到在两个不同的操作系统上运行的JVM可能会为不同的对象使用不同的内存量)

(and yes I realize that a JVM running on two different OS's may be likely to use different amounts of memory for different objects)

推荐答案

从Java 5开始,在Hotspot和其他支持它的VM上,您可以使用Instrumentation接口向VM询问给定对象的内存使用情况。它很繁琐,但你可以做到。
如果您想尝试这种方法,我已经在我的网站上添加了一个页面使用Instrumentation框架查询Java对象的内存大小。

As of Java 5, on Hotspot and other VMs that support it, you can use the Instrumentation interface to ask the VM the memory usage of a given object. It's fiddly but you can do it. In case you want to try this method, I've added a page to my web site on querying the memory size of a Java object using the Instrumentation framework.

作为32位计算机上Hotspot的粗略指南:

As a rough guide in Hotspot on 32 bit machines:


  • 对象使用8个字节用于
    管家

  • 字段使用您期望的字段到
    使用给定它们的位长(虽然布尔值往往被分配一个整个字节)

  • 对象引用使用4个字节

  • 整体obejct size的
    粒度为8个字节(即如果
    有一个带有1个布尔字段
    的对象,它将使用16个字节;如果你有一个带有8个布尔值的
    对象,它将还
    使用16个字节)

  • objects use 8 bytes for "housekeeping"
  • fields use what you'd expect them to use given their bit length (though booleans tend to be allocated an entire byte)
  • object references use 4 bytes
  • overall obejct size has a granularity of 8 bytes (i.e. if you have an object with 1 boolean field it will use 16 bytes; if you have an object with 8 booleans it will also use 16 bytes)

关于VM如何处理它们,集合没什么特别之处。它们的内存使用量是它们内部字段的总和加上 - 如果你在计算它 - 它们包含的每个对象的用法。您需要考虑诸如ArrayList的默认数组大小之类的事情,以及每当列表变满时该大小增加1.5的事实。但无论是询问虚拟机还是使用上述指标,查看集合的源代码并通过它,基本上都可以帮助您找到答案。

There's nothing special about collections in terms of how the VM treats them. Their memory usage is the total of their internal fields plus -- if you're counting this -- the usage of each object they contain. You need to factor in things like the default array size of an ArrayList, and the fact that that size increases by 1.5 whenever the list gets full. But either asking the VM or using the above metrics, looking at the source code to the collections and "working it through" will essentially get you to the answer.

如果通过闭合你的意思是像Runnable或Callable这样的东西,再一次,它只是一个像其他任何东西一样无聊的旧物体。 (N.B.他们不是真的关闭!!)

If by "closure" you mean something like a Runnable or Callable, well again it's just a boring old object like any other. (N.B. They aren't really closures!!)

这篇关于确定java内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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