Skip to content

Instantly share code, notes, and snippets.

@kimchy
Created July 12, 2011 19:09
Show Gist options
  • Select an option

  • Save kimchy/1078717 to your computer and use it in GitHub Desktop.

Select an option

Save kimchy/1078717 to your computer and use it in GitHub Desktop.
native memory leak with GarbageCollectorMXBean#getLastGcInfo
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;
public class TestMemoryLeak {
public static void main(String[] args) throws Exception {
while (true) {
List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gcMxBean : gcMxBeans) {
((com.sun.management.GarbageCollectorMXBean) gcMxBean).getLastGcInfo();
}
}
}
}
@mlaccetti

Copy link
Copy Markdown

Could this just be that GC isn't having enough time to clean up as you loop through?

@kimchy

kimchy commented Jul 14, 2011

Copy link
Copy Markdown
Author

No, since there is nothing to clean.

@mlaccetti

Copy link
Copy Markdown

Not entirely true - that List of MBeans will hang around for a while until the GC runs and evicts it. If you loop fast enough before the GC is triggered, you can wipe your memory.

@kimchy

kimchy commented Jul 14, 2011

Copy link
Copy Markdown
Author

Thats heap memory, not RES memory. Heap wise all is well, and things get collected.

@mlaccetti

Copy link
Copy Markdown

Time to fire up YourKit!

@dinomite

Copy link
Copy Markdown

I stumbled across this gist while looking for more information about this HotSpot bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7066129

@kimchy

kimchy commented Jul 14, 2011

Copy link
Copy Markdown
Author

Thats the bug I opened :)

@dinomite

Copy link
Copy Markdown

Hah, I wondered whether that was the case—I just figured there should be a connection between the two!

@tobiasbrenner

Copy link
Copy Markdown

@kimchy: do you have a workaround for this? Can you confirm such kind of bugs for the other MXBeans, too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment