Skip to content

Leak Examples

eostermueller edited this page Jan 7, 2017 · 6 revisions

This page shows heapSpank in action.

Exhibit A

This screenshot shows one piece of code, with and without a memory leak. See how LKY% goes to 100%? Quick Memory Leak Detection

Exhibit B

Perhaps this is the most common memory leak -- filling up a container that is never cleared out. Compile and run this yourself -- should just take a minute to watch how heapSpank shows you the exact class name (MemLeak) of the culprit.

package com.post.memory.leak;

import java.util.Map;

public class MemLeak { public final String key;

public MemLeak(String key) {
    this.key =key;
}

public static void main(String args[]) {
    try {
        Map map = System.getProperties();
        
        for(;;) {
            map.put(new MemLeak("key"), "value");
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
}

}

taken from here.

Exhibit C

This stackoverflow question shows a number of different examples of memory leaks. I've got it on my todo list to package these up nicely for display and heapSpank demos.

Clone this wiki locally