[GH-ISSUE #5909] [Feature]: Set different values of GOMEMLIMIT for xui and xray #4094

Open
opened 2026-10-31 20:37:08 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @ilya-practicum on GitHub (Jul 12, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5909

You cannot set different values of GOMEMLIMIT for x-ui and xray processes since GOMEMLIMIT overwrite XUI_MEMORY_LIMIT parameter.

Describe the solution you'd like

If both parameters set - XUI_MEMORY_LIMIT and GOMEMLIMIT, then first one will be applied to x-ui and second one to the xray. You will be able to set soft limits to two processes independently.

Alternatives you've considered

No response

Concrete use case

No response

Mockups, screenshots, or examples

No response

Which parts of the panel would this affect?

No response

Are you willing to help?

  • I'd like to implement this feature myself and open a PR.
  • I can help test once a PR is open.

Before submitting

  • I searched existing issues and this feature has not been requested.
  • This feature request is written in English.
Originally created by @ilya-practicum on GitHub (Jul 12, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5909 ### Is your feature request related to a problem? You cannot set different values of GOMEMLIMIT for x-ui and xray processes since GOMEMLIMIT overwrite XUI_MEMORY_LIMIT parameter. ### Describe the solution you'd like If both parameters set - XUI_MEMORY_LIMIT and GOMEMLIMIT, then first one will be applied to x-ui and second one to the xray. You will be able to set soft limits to two processes independently. ### Alternatives you've considered _No response_ ### Concrete use case _No response_ ### Mockups, screenshots, or examples _No response_ ### Which parts of the panel would this affect? _No response_ ### Are you willing to help? - [ ] I'd like to implement this feature myself and open a PR. - [ ] I can help test once a PR is open. ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this feature has not been requested. - [x] This feature request is written in English.
Author
Owner

@github-actions[bot] commented on GitHub (Jul 12, 2026):

Confirmed: this is the current implementation's intended behavior, not a bug, and the split you're asking for is technically feasible without a new env var.

Two separate things are going on:

  1. GOMEMLIMIT intentionally wins over XUI_MEMORY_LIMIT for x-ui today. In internal/util/sys/memlimit.go, applyMemoryLimit() checks GOMEMLIMIT first and returns immediately if it's set, without ever looking at XUI_MEMORY_LIMIT (lines 56-59); XUI_MEMORY_LIMIT is only applied via debug.SetMemoryLimit() when GOMEMLIMIT is unset (lines 61-67). This matches the documented precedence in .env.example and docs/content/docs/en/reference/env-vars.mdx ("takes precedence over above").

  2. GOMEMLIMIT also reaches the xray-core child process, independently of x-ui's own code. GOMEMLIMIT is a native Go runtime variable: any Go binary that sees it in its environment applies it automatically before main() runs. Xray-core is itself a Go program (github.com/xtls/xray-core, pinned in go.mod), and internal/xray/process.go's Start() (line 533) launches it via exec.CommandContext(GetBinaryPath(), "-c", configPath) without ever setting cmd.Env. With a nil Env, Go's os/exec makes the child inherit the parent's full environment, so a GOMEMLIMIT set for x-ui is passed straight through to xray-core, whose own runtime applies it on its own. So the "overwrite" you're seeing is really two independent Go runtimes reading the same shared variable, not 3x-ui code overwriting one flag with the other.

Your proposed split is achievable: Go allows overriding an env-derived limit at runtime, not just at startup - debug.SetMemoryLimit() can be called at any point to replace whatever GOMEMLIMIT set initially, for that process only. applyMemoryLimit() could be changed to always apply XUI_MEMORY_LIMIT (when explicitly set) to the x-ui process via debug.SetMemoryLimit(), regardless of whether GOMEMLIMIT is also present in the environment. GOMEMLIMIT would keep flowing to the xray-core child unchanged (no change needed in process.go), so it would still set xray's own soft limit independently. That gives exactly the outcome you described - XUI_MEMORY_LIMIT for x-ui, GOMEMLIMIT for xray, both effective at once - but it would flip today's documented precedence for the case where both are set, which is a user-visible behavior change worth a release note if implemented.

Already labeled enhancement correctly; no relabeling needed. @MHSanaei for a decision on prioritization and whether flipping the precedence (vs. introducing a separate xray-specific variable) is the preferred design.

This reply was generated automatically by an AI assistant. A maintainer may follow up.

<!-- gh-comment-id:4951007135 --> @github-actions[bot] commented on GitHub (Jul 12, 2026): Confirmed: this is the current implementation's intended behavior, not a bug, and the split you're asking for is technically feasible without a new env var. Two separate things are going on: 1. **`GOMEMLIMIT` intentionally wins over `XUI_MEMORY_LIMIT` for x-ui today.** In `internal/util/sys/memlimit.go`, `applyMemoryLimit()` checks `GOMEMLIMIT` first and returns immediately if it's set, without ever looking at `XUI_MEMORY_LIMIT` (lines 56-59); `XUI_MEMORY_LIMIT` is only applied via `debug.SetMemoryLimit()` when `GOMEMLIMIT` is unset (lines 61-67). This matches the documented precedence in `.env.example` and `docs/content/docs/en/reference/env-vars.mdx` ("takes precedence over above"). 2. **`GOMEMLIMIT` also reaches the xray-core child process, independently of x-ui's own code.** `GOMEMLIMIT` is a native Go runtime variable: any Go binary that sees it in its environment applies it automatically before `main()` runs. Xray-core is itself a Go program (`github.com/xtls/xray-core`, pinned in `go.mod`), and `internal/xray/process.go`'s `Start()` (line 533) launches it via `exec.CommandContext(GetBinaryPath(), "-c", configPath)` without ever setting `cmd.Env`. With a nil `Env`, Go's `os/exec` makes the child inherit the parent's full environment, so a `GOMEMLIMIT` set for x-ui is passed straight through to xray-core, whose own runtime applies it on its own. So the "overwrite" you're seeing is really two independent Go runtimes reading the same shared variable, not 3x-ui code overwriting one flag with the other. Your proposed split is achievable: Go allows overriding an env-derived limit at runtime, not just at startup - `debug.SetMemoryLimit()` can be called at any point to replace whatever `GOMEMLIMIT` set initially, for that process only. `applyMemoryLimit()` could be changed to always apply `XUI_MEMORY_LIMIT` (when explicitly set) to the x-ui process via `debug.SetMemoryLimit()`, regardless of whether `GOMEMLIMIT` is also present in the environment. `GOMEMLIMIT` would keep flowing to the xray-core child unchanged (no change needed in `process.go`), so it would still set xray's own soft limit independently. That gives exactly the outcome you described - `XUI_MEMORY_LIMIT` for x-ui, `GOMEMLIMIT` for xray, both effective at once - but it would flip today's documented precedence for the case where both are set, which is a user-visible behavior change worth a release note if implemented. Already labeled `enhancement` correctly; no relabeling needed. @MHSanaei for a decision on prioritization and whether flipping the precedence (vs. introducing a separate xray-specific variable) is the preferred design. *This reply was generated automatically by an AI assistant. A maintainer may follow up.*
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#4094
No description provided.