Xray-core icon indicating copy to clipboard operation
Xray-core copied to clipboard

High RAM consumption when using xhttp

Open dev-kick opened this issue 1 year ago • 11 comments

Integrity requirements

  • [x] I have read all the comments in the issue template and ensured that this issue meet the requirements.
  • [x] I confirm that I have read the documentation, understand the meaning of all the configuration items I wrote, and did not pile up seemingly useful options or default values.
  • [x] I provided the complete config and logs, rather than just providing the truncated parts based on my own judgment.
  • [x] I searched issues and did not find any similar issues.
  • [x] The problem can be successfully reproduced in the latest Release

Description

In Russia, TCP transport stopped working and many switched to xhttp. However, on all iOS clients, using HTTP causes memory consumption to exceed 50 MB over time, which causes iOS to automatically turn off the VPN tunnel.

Reproduction Method

Just use

Client config

{ "outbounds" : [ { "streamSettings" : { "security" : "reality", "realitySettings" : { "serverName" : "-", "fingerprint" : "chrome", "shortId" : "-", "spiderX" : "", "publicKey" : "-" }, "network" : "xhttp", "xhttpSettings" : { "mode" : "packet-up", "path" : "/new-weels", "host" : "-" } }, "settings" : { "address" : "-", "id" : "-", "encryption" : "none", "port" : 443 }, "protocol" : "vless", "tag" : "proxy" }, { "settings" : { "response" : { "type" : "none" } }, "protocol" : "blackhole", "tag" : "block" }, { "settings" : {

  },
  "protocol" : "freedom",
  "tag" : "direct"
}

], "id" : "-", "stats" : {

}, "policy" : { "system" : { "statsOutboundDownlink" : true, "statsOutboundUplink" : true, "statsInboundUplink" : true, "statsInboundDownlink" : true } }, "remarks" : "-", "log" : { "loglevel" : "info" }, "inbounds" : [ { "listen" : "[::1]", "port" : 1080, "settings" : { "udp" : true }, "protocol" : "socks", "tag" : "socks", "sniffing" : { "routeOnly" : false, "enabled" : true, "destOverride" : [ "http", "quic", "tls" ] } } ] }

Server config

{ "tag": "VLESS TCP REALITY", "listen": "0.0.0.0", "port": 443, "protocol": "vless", "settings": { "clients": [], "decryption": "none" }, "streamSettings": { "network": "xhttp", "xhttpSettings": { "path": "/new-weels", "mode": "auto" }, "security": "reality", "realitySettings": { "show": false, "dest": "127.0.0.1:8443", "xver": 0, "serverNames": [ «.» ], "privateKey": «.», "publicKey": «.», "shortIds": [ ".", "." ] } }, "sniffing": { "enabled": true, "destOverride": [ "http", "tls", "quic" ], "metadataOnly": false } }

Client log

2025/11/24 13:57:44.607707 [Warning] core: Xray 25.10.15 started 24.11.2025 13:57:44 XrayCoreStart - {"success":true} app: v3.5.3 24.11.2025 13:59:14 Tunnel stopped. Stop reason: - userInitiated

Server log

Empty

dev-kick avatar Nov 25 '25 10:11 dev-kick

*upd, same problem with tcp transport

The largest memory consumption is observed when watching YouTube

dev-kick avatar Nov 25 '25 23:11 dev-kick

*upd, same problem with tcp transport

The largest memory consumption is observed when watching YouTube

check your client routing, if you use something like a "geosite:category-ads-all" you are cooked bro

nasralbek avatar Nov 26 '25 05:11 nasralbek

check your client routing, if you use something like a "geosite:category-ads-all" you are cooked bro

This is independent of geo files. It happens with geo files disabled. I am aware of the issue with large geo files and checked it immediately.

dev-kick avatar Nov 26 '25 07:11 dev-kick

just use Android

Fangliding avatar Nov 26 '25 07:11 Fangliding

just use Android

👍🏻

dev-kick avatar Nov 26 '25 08:11 dev-kick

just use Android

But what if that's not an option? A lot of people still have ios.

drexample avatar Nov 26 '25 12:11 drexample

I've run into the same issue with excessive RAM consumption (which never happened with plain VLESS+REALITY) on an OpenWrt router with 256 megabytes of RAM (~140 MB available at startup), with xray-core updated to the latest version, so this problem isn't specific to iOS. There's very likely a memory leak in the XHTTP implementation, because I don't have large geo files or a large number of devices connected to my network.

debushoukun avatar Nov 26 '25 17:11 debushoukun

I've run into the same issue with excessive RAM consumption (which never happened with plain VLESS+REALITY) on an OpenWrt router with 256 megabytes of RAM (~140 MB available at startup), with xray-core updated to the latest version, so this problem isn't specific to iOS. There's very likely a memory leak in the XHTTP implementation, because I don't have large geo files or a large number of devices connected to my network.

For me, the solution was to use the pocket-up mod, which uses less memory.

dev-kick avatar Nov 26 '25 20:11 dev-kick

I've run into the same issue with excessive RAM consumption (which never happened with plain VLESS+REALITY) on an OpenWrt router with 256 megabytes of RAM (~140 MB available at startup), with xray-core updated to the latest version, so this problem isn't specific to iOS. There's very likely a memory leak in the XHTTP implementation, because I don't have large geo files or a large number of devices connected to my network.

For me, the solution was to use the pocket-up mod, which uses less memory.

what “pocket up mode” is?

romaneplanov avatar Dec 01 '25 03:12 romaneplanov

I've investigated the root causes of the high memory consumption in XHTTP transport and prepared a PR with optimizations that reduce total allocations by ~63% (from ~68 MB down to ~25 MB for a 10 MB transfer).

Root causes found via pprof profiling:

  1. io.ReadAll with grow-and-copy pattern (512 -> 1K -> 2K -> ... -> target size) on both client and server - accounted for ~52 MB (77%) of all allocations
  2. slices.Concat unconditionally copying payload even when only one source has data - ~10 MB (15%)
  3. bytes.Buffer full HTTP request serialization doubling memory per upload
  4. Unbounded concurrent upload goroutines multiplying in-flight buffer memory
  5. globalDialerMap never cleaning up dead entries (leak over time)

Profiling results (pprof alloc_space, 10 MB transfer):

Allocator Before After
io.ReadAll + bytes.growSlice 51.7 MB 0 MB (eliminated)
slices.Concat 10.2 MB 0 MB (eliminated)
Total 66.6 MB 24.7 MB (-63%)

This should significantly help with the iOS 50 MB Network Extension limit and low-memory devices like OpenWrt routers mentioned by @debushoukun.

PR: #5797

ozeranskii avatar Mar 13 '26 13:03 ozeranskii

FYI, here is another way to reduce RAM overhead of XHTTP in some scenarios: #6268

darkk avatar Jun 04 '26 16:06 darkk