determining java memory usage

后端 未结 8 2365
说谎
说谎 2020-12-05 22:01

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

相关标签:
8条回答
  • 2020-12-05 22:32

    I've used the profiler that comes with newer versions of Netbeans a couple of times and it works very well, supplying you with a ton of information about memory usage and runtime of your programs. Definitely a good place to start.

    0 讨论(0)
  • 2020-12-05 22:38

    If you are using a pre 1.5 VM - You can get the approx size of objects by using serialization. Be warned though.. this can require double the amount of memory for that object.

    0 讨论(0)
  • 2020-12-05 22:44

    I used JProfiler a number of years ago and it did a good job, and you could break down memory usage to a fairly granular level.

    0 讨论(0)
  • 2020-12-05 22:46

    This might be not the exact answer you are looking for, but the bosts of the following link will give you very good pointers. Other Question about Memory

    0 讨论(0)
  • 2020-12-05 22:48

    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.

    As a rough guide in Hotspot on 32 bit machines:

    • 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)

    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.

    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!!)

    0 讨论(0)
  • 2020-12-05 22:49

    See if PerfAnal will give you what you are looking for.

    0 讨论(0)
提交回复
热议问题