开发者

getMethod is caching and cause a memory leak

I am using an automatic generated Java class for executing a special m开发者_运维百科ethod. Because of this i have to invoke the method by reflection.

This execution is triggered by a Swing Thread, because the method (invoked from the "unknown" class) is updating a UI Element. Every execution of a new Thread is searching for a Method in the class by calling

Class {
...
public Method[] getMethods() throws SecurityException
...
}

The logic of getMethods, caches the element, but i do not know why. A Profiler shows me, that Method obejcts are floating the memory.

I can disable the method caching of Java by setting the system property "sun.reflect.noCaches", but my application became incredible slow, after i changed these property.

Question: I could implement my own caching algorithm, before i try to get the method from the class. But the project i am working is very big and we have multiple reflection calls.

What can solve these problem?

Which cirumstances can trigger these memory leak (wrong classloader,...)?

The project is running under Linux using Java 1.5


  1. First thing, the cache that it uses is a SoftReference, so the cached methods will be removed before an OOME ever occurs.

  2. If you still want to remove it, just call the clearCachesOnClassRedefinition() method after every call to getMethods(..); you will have to use reflection as the method is private.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜