Edit: There is no memory leak, just a noobs misunderstanding of what the Netbeans Profiler is telling me. Live Profiling Results -> Allocated Objects refers to the TOTAL allocated over the lifetime of the program, not the total currently in memory. For that, get a heap dump. Sorry for the confusion, but maybe someone else will find this and it will clear that up for them.
Im using the NetBeans 7.0.1 profiler to troubleshoot memory growth in my application, and am seeing two major issues. Ive successfully created test applications and see the same results in the profiler, included below. I wasnt able to find a reference to these anywhere else.
Case 1: Looping through the entryset of a HashMap causes the object java.util.HashMap&EntryIterator to grow. Is there anything to do here except figure out a solution with a different container? Looping through the collection every time in the while loop is necessary to our application.
public static void main(String[] args) throws InterruptedException {
boolean run = true;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer> ();
map.put(1, 2);map.put(3, 4);map.put(5, 6);map.put(7, 8);
while(true){
for (Entry<Integer, Integer> entry : map.entrySet()) {
Integer i = entry.getValue();
}
//app specific code here
Thread.sleep(50);
}
}
Case 2: Creating empty ArrayLists in a loop causes a growth of java.lang.Object[]. I would expect the ArrayLists to be eligible for GC at the end of the while loop. Is my assumption wrong? Or is something else at play here?
public static void main(String[] args) throws InterruptedException {
boolean run = true;
while(run){
ArrayList<Integer> a1 = new ArrayList<Integer>();
ArrayList<Integer> a2 = new ArrayList<Integer>();
// app specific code here
Thread.sleep(50);
}
}