Tag Archives: java

Java Memory Management in Practice: Video Notes

This post contains the notes that accompany the video tutorial JMM in Practice on YouTube. On this video you can learn the basic principles of how the JVM performs garbage collection and manages the heap space.

Download the Source Code

You can download the source code from this GitHub repository.
It is a Maven project that you can directly import into your favourite IDE.

Installing and Running Visual VM

The Visual VM download page is located here.

To install Visual VM you just need to unzip the downloaded archive into a directory of your choice.

To run Visual VM invoke the binary for your operating system, passing the options for the JDK and user directory (this is the location where you want Visual VM to store data files).

For example, on Windows:

visualvm.exe --jdkhome <path to JDK> --userdir <path to user directory>

 

Once Visual VM is running you can activate the Visual GC plugin as follows:

1. From the menu select Tools -> Plugins
2. In the popup window select the tab “Available Plugins”
3. Check “Visual GC” from the list and click on the Install button
4. Close the popup

You should see the “Visual GC” tab now available on Visual VM.

Watch the Video

You now have all the tools you need to follow the video and replicate the experiment on your system.

Learn from the Experts: A Look at the Java Source Code

The best way to learn and improve coding skills in any programming language is to look at code written by expert programmers. The Java Development Kit (JDK) comes with the source code of the complete Java Runtime Library; it is located in the src.zip archive under the JDK installation directory.

java-src

The location of the source file archive in the JDK 7 installation

Obviously your main reference when using the Java library classes should be always the API documentation, which can be found online or downloaded separately from the JDK downloads website. Here is the URL for the Java 8 API:

https://docs.oracle.com/javase/8/docs/api/

However, it can be very instructive to take a look at the source code of some of the key classes in the library, such as those in the java.lang and java.util packages.

Using Eclipse to view the Java Runtime Library Source Code

First, make sure you have the JDK selected as the default installed JRE. To do this select from the Eclipse menu: Window → Preferences. The following dialog pops up:

Setting up the JDK as the default JRE in Eclipse Mars

Setting up the JDK as the default JRE in Eclipse Mars

Select Java → Installed JREs

If the JDK is not there, click [Add…] and follow the prompts to select your JDK installation directory. Then make this JRE default by selecting the checkbox next to it. Now click [Edit…]. The following dialog pops up:

Setting up the runtime library source attachment

Setting up the runtime library source attachment

Expand the rt.jar library (this is the runtime binary) and click on [Source Attachment…]

The source attachment configuration dialog

The source attachment configuration dialog

Browse to the external location of your src.zip and click [OK]. Apply and close all parent windows.

Now to view the source file of a library class, e.g. LinkedList, either select from the menu:
Navigate → Open Type or use the shortcut [CTRL]+[SHIFT]+[T]

The Eclipse Open Type dialog

The Eclipse Open Type dialog

Type the name of the class, or select it from the list. The source code will open in the Editor.

the Eclipse editor showing the source code for LinkedList.java

The Eclipse editor showing the source code for LinkedList.java

Happy studying!!