简体   繁体   中英

Memory leak in Flux, but where?

My application consists of a rather complex system of Flux and Mono chains. When running the program I can detect a memory leak using VisualVM.

The problem is that, allthough I can clearly see which instances are causing the memory issues, the reference tree of these instances, due to the nature of the reactive code, is pretty much opaque to me. See the screenshot below.

VisualVM中参考树的屏幕快照

So my question is: Is there a way to read this tree? Can I tell where the memory leak is occuring, or at least in which operator?

As the reference tree mentions a PublishOnSubscriber I removed all instances where I explicitly called .publishOn , but without success.

A memory leak could be a sign of a Processor configured with an unbounded queue with no subscribers. The visual vm screenshot shows "queue" in the path which is why I am suggesting this.

The following will cause a memory leak:

UnicastProcessor<Integer> subject = UnicastProcessor.create();
int j = 0;
for(int i = 0; i < 400000000; i++) {
    Integer g = Integer.valueOf(i);
    subject.onNext(g);
    if(j++ > 100000) {
        j = 0;
        long memoryUsage = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        System.out.println(memoryUsage / 1024 / 1024);
    }
}

Calling UnicastProcessor.create() uses an unbounded queue by default. Since there are no subscribers, items accumulate in the queue. Solution is simply to make sure the processor has a subscriber, like this:

        UnicastProcessor<Integer> subject = UnicastProcessor.create();
---->   subject.subscribe();
        int j = 0;
        for(int i = 0; i < 400000000; i++) {
            Integer g = Integer.valueOf(i);
            subject.onNext(g);
            if(j++ > 100000) {
                j = 0;
                long memoryUsage = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
                System.out.println(memoryUsage / 1024 / 1024);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM