This is a course for Java professionals (or aspiring Java professionals) who need an in depth understanding of how memory works in Java. In this course you'll learn what kinds of memory leaks are possible in Java, and how to avoid them. In addition I'll show you tools you can use to analyse how your applications are performing, and detect inefficient memory use, such as objects which are taking up too much memory, or problems causing inefficient garbage collection. And we'll see how to fix these.
You don't need to be a Java Expert to do this course, but you should be able to confidently create basic Java code before you start.
An introduction to how memory works in Java - outlining what we'll be studying in this section.
Java's memory management is based on two important concepts - the stack and the heap. In this chapter we learn what the stack is, and we see how variables are added to and removed from the stack during the execution of a Java program.
Java uses the heap to store objects. In this chapter we learn about how the heap and the stack work together to allow data to be passed between methods.
In Java variables are always passed between objects by value - we understand what this means, and compare it to the "by reference" alternative that some languages offer.
Many programmers think that objects are passed by reference in Java - this isn't the case however, so we learn in this chapter how it does work, and why it's significant.
In this chapter we understand why the final keyword cannot be relied on to make an object immutable (that it cannot change its state). We'll see why there's no "const correctness" in Java.
This video, together with the code files that go with it, form a challenge for you to think about - you need to predict what will happen if you run the sample code, using what we've learned so far to work out the outcome.
We understand what escaping references are, why they are dangerous, and start to look at one way to avoid them when you need to pass collections between methods.
In this video we continue learning how to avoid escaping references - this time we look at custom objects.
First we discuss why immutable objects are safe in Java, and then there's an exercise for you to try - to detect and remove escaping references in some sample code.
Before we get into the detail of garbage collection, this chapter focuses on how Java can be efficient when you are creating strings, by placing them in a pool.
Garbage Collection is one of the ways that memory leaks become pretty impossible in Java. We start by understanding how the virtual machine decides which objects are eligible for garbage collection.
Java gives us the gc() method that we can use to try and influence a garbage collection to take place. We can use the finalise() method to find out if it did.
In this chapter we'll explore the closest thing to a memory leak that you can have in Java, which I refer to as a soft leak. I'll show you how to detect whether these are happening in your applications using the VisualVM tool.
We'll learn more about how the garbage collection process works, using a system called mark and sweep.
The JVM makes the mark and sweep process very efficient by using generations for the objects. We'll see this in practice using the VisualVM tool.
We'll use what we've learned so far to visualise the soft leak we saw earlier, and understand the impact it has on the garbage collection process.
In this chapter we use a further tool to help us identify the source of a memory leak - Eclipse's Memory Analyzer Tool
To finish this section, we'll talk about a special part of the heap called Permgen, and why in Java 8, you should no longer see any errors indicating that the Permgen space is full.
We start by seeing how to specify the heap size that we want our applications to be able to use
If you're using Java 7 or below, you may wish to set the permgen size. We'll see how to do that in this video.
Next we see how to log when garbage collections take place, and we can specify the size of the young generation.
If you have an application which is crashing in production systems with an out of memory error, this option will help you to investigate.
Java gives us a choice of garbage collector, which can be used to improve performance depending on your hardware. We'll learn how to choose a collector, and how to find out which is the default for your computer.
We'll be looking for a memory leak in a real world application - a Java website. In this video we'll install the website and see it running.
We use the Visual VM tool to montior the application's memory usage, and the Apache j-meter tool to simulate production use.
We finally analyse the heap dump to work out where our memory leak is happening, and then fix and re-test.
This is a bonus chapter, covering weak and soft references, and the useful WeakHashMap object.
Having worked for over 20 years as a professional programmer, mainly in banking, Matt now teaches for Virtual Pair Programmers. His specialist areas are JavaEE, Android, Hadoop and NoSQL. Matt's currently working on a Clojure project, which he finds an enjoyable challenge!
Outside of work, Matt enjoys cycling, but prefers going downhill to uphill, and he also plays the piano… very badly.