shenyu: [BUG]

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

memory leaked

Expected Behavior

when i use System.gc(), no memory leaked

Steps To Reproduce

public class TestMemoryLeak { // 1m内存 private static final int M_1 = 1 * 1024 * 1024;

public static void main(String[] args) throws InterruptedException, IOException {
    System.in.read();
    try {
        mswtMapTest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.gc();
    TimeUnit.SECONDS.sleep(Long.MAX_VALUE);
}

public static void mswtMapTest() {
    for (int i = 0; i < 10; i++) {
        System.gc();
        MemorySafeWindowTinyLFUMap<String, Byte[]> map = new MemorySafeWindowTinyLFUMap<>(1, 100);
        map.put(String.valueOf(i), new Byte[M_1 * 2]);
        // 手动GC
    }
}

public static void commonMapTest() {
    for (int i = 0; i < 10; i++) {
        System.gc();
        Map<String, Byte[]> map = new HashMap<>();
        map.put(String.valueOf(i), new Byte[M_1 * 2]);
        // 手动GC
        System.gc();
    }
}

}

Environment

jvm: -Xms20m -Xmx20m

ShenYu version(s):

Debug logs

Exception in thread “main” java.lang.OutOfMemoryError: Java heap space at org.apache.shenyu.common.cache.TestMemoryLeak.mswtMapTest(TestMemoryLeak.java:44) at org.apache.shenyu.common.cache.TestMemoryLeak.main(TestMemoryLeak.java:32)

Anything else?

MemorySafeWindowTinyLFUMap hold a static reference ALL, every reference of MemorySafeWindowTinyLFUMap will be always hold by the reference, and may cause a memory leak

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 19 (17 by maintainers)

Most upvoted comments

It looks like the request is to use a weak cache for the static ALL field so that instances may be collected when there are no strong references held by the application. As this is a static field that can be called by multiple threads, it should also be thread-safe. It doesn’t look like ordering matters, so you could use a Caffeine weakKey cache with Collections.newSetFromMap. Something like,

private static final Set<MemorySafeWindowTinyLFUMap<?, ?>> ALL = Collections.newSetFromMap(
    Caffeine.newBuilder().weakKeys().<MemorySafeWindowTinyLFUMap<?, ?>>build().asMap());