VisualVM can be used to monitor memory allocations and detect possible memory leaks. Let's modify our application to simulate a large memory allocation that has not been released. We will modify the CourseService class:
public class CourseService { private CourseDAO courseDAO = new CourseDAO(); //Dummy cached data used only to simulate large //memory allocation private byte[] cachedData = null; public synchronized List<CourseDTO> getCourses() { //To simulate large memory allocation, //let's assume we are reading serialized cached data //and storing it in the cachedData member try { this.cachedData = generateDummyCachedData(); } catch (IOException e) { //ignore } return courseDAO.getCourses(); } private byte[] generateDummyCachedData() ...