0

enter image description here

This is a one-day memory curve for my Java project.How do I find out which class or method USES a lot of memory?

MLM520
  • 59
  • 2
  • here you go - https://docs.oracle.com/javase/8/docs/technotes/guides/visualvm/profiler.html – Scary Wombat Jan 16 '19 at 01:49
  • Thanks, I know about this tool, but my supervisor wants to know which methods are responsible for a large amount of memory usage at a certain time, and which methods generate a large number of objects in the heap. – MLM520 Jan 16 '19 at 01:58
  • For example,Which method produces this String object – MLM520 Jan 16 '19 at 02:01
  • How do you collect the data shown in your graph? The source is important. – Thorbjørn Ravn Andersen Jan 16 '19 at 02:35
  • I used the JavaMelody in this project, but not every project used it, so I wanted to find a generic wayI used the JavaMelody for this project, – MLM520 Jan 16 '19 at 06:32

2 Answers2

2

You need a tool that can do JVM allocation profiling.

Java Flight Recorder (JFR) can help you with low overhead allocation profiling (1, 2 — docs for older versions, the UI looks quite different now, but "Allocations" and "TLAB" are still the words used to find stuff). Make sure that allocation profiling is enabled in the settings you use. It's a commercial feature until OpenJDK 11. They say some other tools can do it with low overhead these days too (e.g. async-profiler).

If you don't care about your app becoming unusably slow and are on JDK < 11(?), VisualVM used to be able to show you stack traces of allocations. I can't find it in the latest version (1.4.2 as of now), but can find it in JVisualVM shipped with my Oracle JDK 8-something. "Profiler" > check "Settings" > "Memory Settings" > check "Record allocation stack traces". Start profiling, wait, wait, wait, identify the biggest allocations, right-click, "Take Snapshot and Show Allocation Stack Traces". It looks like YourKit does basically the same, as well as JProfiler (they call it "allocation recording"). Again: slows down the application a lot, so don't use it in prod.

starikoff
  • 1,601
  • 19
  • 23
1

You have take a heap dump periodically and analyse these with tools like MAT- Memory Analyzer Tool (https://www.eclipse.org/mat/). The tool provides the which Object / Class occupying more memory and from which thread it is created etc.

How to find from which thread particular object is created?

  1. In the MAT, click on the Histogram - It will show the list of classes available in the dump. To demonstrate, let me filter out the class java.lang.String only

enter image description here

  1. Right click any class name , in the popup-window choose List Objects ---> with outgoing references

enter image description here

  1. Above Step list out all the instances of the selected class. Right click any one of the class name and from the pop up choose Merge Shortest Paths to GC Roots ---> with all references

enter image description here

  1. After the above step, you can view the From which Thread class particular object is created.

enter image description here

Ramesh Subramanian
  • 944
  • 1
  • 12
  • 28
  • _"and from which thread it is created"_ -- the last time I checked, I couldn't find this information in a heap dump, can you maybe give a hint where to look? – starikoff Jan 24 '19 at 22:10
  • @starikoff , updated the answer for "from which thread it is created". – Ramesh Subramanian Jan 25 '19 at 06:21
  • Thank you for the elaborate comment and for your effort! I think though you are looking at things slightly wrong: this thread is what has a reference to the object at the time of the dump (this is kinda the definition of a "GC root"). And not all objects will have Thread as the GC root at this point; in fact most probably won't, look into some of your (non-JVM) classes for examples. – starikoff Jan 25 '19 at 08:22