|
| 1 | +Sample to a show memory references in a Java Application |
| 2 | +======================================================== |
| 3 | + |
| 4 | +This sample application creates objects from classes `A`, `B`, `C`, `D`, `E`, `F`, and `G`. |
| 5 | +Each of the previously mentioned classes extend to `Base` class, which creates a significantly |
| 6 | +large byte array in the constructor. |
| 7 | + |
| 8 | +Following is the class diagram. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +The application is created to demonstrate the references among objects. |
| 13 | + |
| 14 | +The sample application creates an object from class `A` in the main thread and another object from |
| 15 | +class `G` in a daemon thread. |
| 16 | + |
| 17 | +The instance created from class `A` references the instances created from other classes as shown below. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Following is the object diagram showing the object references after the application is started. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +### How to run |
| 26 | + |
| 27 | +Use the following command to run the application. The application creates an object from class `A` in |
| 28 | +the main thread and another object from class `G` in a daemon thread. After that the application will |
| 29 | +wait for a keyboard input. |
| 30 | + |
| 31 | +```bash |
| 32 | +java -Xmn1g -Xmx2g -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -javaagent:target/memoryref.jar -jar target/memoryref.jar |
| 33 | +``` |
| 34 | + |
| 35 | +### Looking at object references using a Heap Dump. |
| 36 | + |
| 37 | +A heap dump can be obtained from following command to look at the object references in heap memory. |
| 38 | + |
| 39 | +```bash |
| 40 | +jmap -dump:file=target/heap.hprof $(pgrep -f memoryref) |
| 41 | +``` |
| 42 | + |
| 43 | +Use [Eclipse Memory Analyzer Tool (MAT)][mat] to analyze the heap dump. |
| 44 | + |
| 45 | +For more information, read following story based on this sample application. |
| 46 | + |
| 47 | +[Basic concepts of Java heap dump analysis with Eclipse Memory Analyzer Tool (MAT)][medium] |
| 48 | + |
| 49 | +[mat]:(https://www.eclipse.org/mat/) |
| 50 | +[medium]:(https://medium.com/@chrishantha/basic-concepts-of-java-heap-dump-analysis-with-mat-e3615fd79eb) |
| 51 | + |
| 52 | +Note: Images were created using [draw.io](https://www.draw.io/). |
0 commit comments