zen icon indicating copy to clipboard operation
zen copied to clipboard

The memory usage is too high, about the same as that of browsers and online games

Open 2372281891 opened this issue 9 months ago • 22 comments
trafficstars

Description

Image

Version

0.8.0

Operating System

WINDOWS 10

Steps to Reproduce

No response

Additional Context

No response

2372281891 avatar Feb 04 '25 23:02 2372281891

Hello, @2372281891! Thanks for the report. Marking this as help wanted—I'd appreciate it if someone could run tests using Go's memory profiler to help identify whether there's a memory leak and/or determine which data structures consume the most memory.

anfragment avatar Feb 07 '25 11:02 anfragment

I agree. I hope it can be reduced soon.

Zero3K avatar Apr 01 '25 14:04 Zero3K

Image

Zero3K avatar Apr 02 '25 01:04 Zero3K

To whoever decides to take this up in the future: Here's a memory profile of Zen after initialization with the filter lists (click on the image to get the full view):

Image

The vast majority is consumed by our primary data structures, ruletree and node, which represent the filter list rules as a trie. They're far from being written in a memory-efficient manner, so there's a potential for improvement.

anfragment avatar May 09 '25 12:05 anfragment

I am working on this issue on a fork https://github.com/ilovelinabell/zen-desktop but how did you guys do the profiling? I think I have shrunk the mem usage a bit but want to quantitatively compare. If you guys can pull my branch and run the profiler, that could also be good. Thanks

ilovelinabell avatar May 12 '25 22:05 ilovelinabell

@ilovelinabell Excited to see what comes out of it! The profiling can be done with Go's built-in pprof, this guide looks good enough to follow: https://www.codereliant.io/p/memory-leaks-with-pprof

anfragment avatar May 13 '25 06:05 anfragment

Thanks for the reply. I am not entirely sure how to use pprof, I will look into it soon. For now, here is a screenshot of my current mem usage.

Image

I have the following filterlists active:

Image

ilovelinabell avatar May 13 '25 18:05 ilovelinabell

Keeping this open for future measures

anfragment avatar Jun 26 '25 15:06 anfragment

400+ MB is still a lot of memory for it to be using.

Zero3K avatar Jun 26 '25 21:06 Zero3K

@Zero3K what is a target RAM usage you were looking at?

ilovelinabell avatar Jun 26 '25 21:06 ilovelinabell

30-100 MB

Zero3K avatar Jun 26 '25 21:06 Zero3K

I’m unfortunately unsure whether we can get to <100 MB since we are essentially running an entire HTTP proxy to man-in-the-middle stop ads from being served. You can look at mitmproxy as another example of a HTTP proxy service.

ilovelinabell avatar Jun 26 '25 21:06 ilovelinabell

We will keep thinking about optimizations to lower memory usage, however. Thanks

ilovelinabell avatar Jun 26 '25 21:06 ilovelinabell

@ilovelinabell If you're up for it, I believe rewriting RuleTree as a compressed trie will help massively with memory consumption

anfragment avatar Jun 26 '25 21:06 anfragment

@anfragment when you have time can you run your profiler again on the current application to see what it is reporting for the mem usage per function? I want to see what the breakdown is

ilovelinabell avatar Jun 26 '25 21:06 ilovelinabell

The overwhelming majority is still consumed by RuleTree:

Image

(click to get the full image)

I'd note that the memory profiler reports a figure significantly lower than what's shown by the OS. It's just a guess at this point, but I suspect heap fragmentation might be the reason. In addition to compressing the trie, switching to an array-based structure (where nodes are stored in a contiguous array and reference their children by index rather than by pointer) might also yield a significant improvement.

@ilovelinabell fyi, you can reproduce the profiling setup very easily - I've shared the code on anfragment/mem. Once you start the app and initialize the proxy/filter, run:

$ go tool pprof http://localhost:6060/debug/pprof/heap
Fetching profile over HTTP from http://localhost:6060/debug/pprof/heap
Saved profile in /Users/anfragment/pprof/pprof.Zen.alloc_objects.alloc_space.inuse_objects.inuse_space.002.pb.gz
File: Zen
Type: inuse_space
Time: 2025-06-27 02:32:18 +05
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) pdf
Generating report in profile001.pdf
(pprof) svg
Generating report in profile001.svg

anfragment avatar Jun 26 '25 21:06 anfragment

@anfragment I added a debug.SetGCPercent(25) statement to the main function and I am seeing 370 MB usage on Windows 11 Pro now

ilovelinabell avatar Jun 27 '25 17:06 ilovelinabell

Supposedly, setGCPercent helps with reducing heap fragmentation

ilovelinabell avatar Jun 27 '25 17:06 ilovelinabell

@ilovelinabell probably not something we can run in production though, right?

anfragment avatar Jun 27 '25 20:06 anfragment

I added it to the code so it will run in the main function. I will play around with the value and tell you if we should merge in my changes

ilovelinabell avatar Jun 27 '25 20:06 ilovelinabell

@anfragment Here is a new PR to shrink mem usage a bit more https://github.com/ZenPrivacy/zen-desktop/pull/388

ilovelinabell avatar Jul 04 '25 17:07 ilovelinabell

Took a quick look in https://github.com/ZenPrivacy/zen-desktop/pull/458 maybe it will help, can someone try it out?

lzap avatar Sep 22 '25 19:09 lzap