heiher / hev-socks5-tunnel

A high-performance tun2socks for Linux/Android/FreeBSD/macOS/iOS/WSL2 (IPv4/IPv6/TCP/UDP)
MIT License
636 stars 130 forks source link

No limit on Memory consumption for ios < 15 or IPhone 7 #109

Open finalvpn opened 3 months ago

finalvpn commented 3 months ago

Case: Using hev-socks5-tunnel for ios below 15 or iphone 7 devices causes memory limit exceeded

The library increasingly uses memory and breaks the limit of 15MB of NEPacketTunnelProvider limit. This causes the extension process to be killed by the ios.

Thread 14: EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory limit exceeded) (limit=15 MB)

Is there any way to limit the amount of memory used in the library?

pronebird commented 3 months ago

I have the same issue. On modern iOS the limit is 50MB but nonetheless can be hit with heavy downloads.

heiher commented 3 months ago

Force close recently inactive sessions, any other ideas?

pronebird commented 2 months ago

Couple of questions and ideas:

  1. We may have a setting to limit the max number of simultaneous TCP and UDP sessions and wait before accepting new connections if the pool is full? But I am not entirely sure it's a great idea. I'd have to look into how others do that.
  2. How long do UDP sessions stick around given that UDP is stateless? Can we have a setting for that?
  3. Another challenge is how to measure how much memory a single connection takes?
shangjiyu commented 2 months ago

same here UDP Session make tunnel killed (douyin dns req burst)

is there way disable udp?

finalvpn commented 2 months ago

Disabling UDP is not possible (required). If the buffer size and concurrent session count is the problem, is there a way to limit them in conf file?

Yewenyu commented 1 month ago

It's recommended to give up on systems below iOS 15; they're not worth the effort.

finalvpn commented 1 month ago

As @pronebird mentioned, it is a problem even in ios versions above 15, causing extension to die

Yewenyu commented 1 month ago

The library works well on iOS 15 and above when I use it. Could the insufficient memory issue be due to other third-party proxy protocols not managing memory properly, rather than a problem with this library?

heiher commented 2 weeks ago

There is a way to limit the number of TCP connections to limit memory usage. The current default value is 1024. I tested 1024 concurrent connections on Linux 64-bit system, which requires about 11M of memory.

https://github.com/heiher/lwip/blob/master/src/ports/include/lwipopts.h#L125

/**
 * MEMP_NUM_TCP_PCB: the number of simulatenously active TCP connections.
 * (requires the LWIP_TCP option)
 */
#define MEMP_NUM_TCP_PCB                1024
heiher commented 2 weeks ago

The v2.6.9 allocates TCP buffers on the stack and optimizes the memory footprint, which helps reduce physical memory usage.

shangjiyu commented 5 days ago

The v2.6.9 allocates TCP buffers on the stack and optimizes the memory footprint, which helps reduce physical memory usage.

it seems would crash in my macOS app

heiher commented 1 day ago

The v2.6.9 allocates TCP buffers on the stack and optimizes the memory footprint, which helps reduce physical memory usage.

it seems would crash in my macOS app

Check the task-stack-size: https://github.com/heiher/hev-socks5-tunnel/blob/master/conf/main.yml#L37