How to resolve OutOfMemoryError with ImageIO plugins as the cause?

拥有回忆 提交于 2019-12-01 06:20:06

Late answer to an old question, but anyway:

Because the ImageIO plugin registry (the IIORegistry) is "VM global", it doesn't by default work well with servlet contexts. This is especially evident if you load plugins from the WEB-INF/lib or classes folder, as the OP seems to do.

Servlet contexts dynamically loads and unloads classes (using a new class loader per context). If you restart your application, old classes will by default remain in memory forever (because the next time scanForPlugins is called, it's another ClassLoader that scans/loads classes, and thus they will be new instances in the registry. In the test code loop, the same ClassLoader is used all the time, thus instances are replaced, and the real problem never manifests).

To work around this resource leak, I recommend using a ContextListener to make sure these "context local" plugins are explicitly removed. Here's an example IIOProviderContextListener I've used in a couple of projects, that implements dynamic loading and unloading of ImageIO plugins.

Looks like a ugly memory leak to me, i can't guess where it is without looking the whole code, but i know what you should check very carefully.

Possible causes:

_Global Variable List, adding elements, never remove them.

_Infinite/big loops, loading lots of elements to Lists, never remove them.

Also it would help if you use a Java profiler, such as VisualVM

If you provide more information in order to have a better idea of how it works I i may be able to improve my answer, there's nothing i can do without more information =)

Hope it helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!