<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Moein Sadraei</title>
    <description>The latest articles on DEV Community by Moein Sadraei (@moeinsadraei).</description>
    <link>https://dev.to/moeinsadraei</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4277997%2Fe6a111ee-39da-4967-80d8-86406996ae4f.jpg</url>
      <title>DEV Community: Moein Sadraei</title>
      <link>https://dev.to/moeinsadraei</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/moeinsadraei"/>
    <language>en</language>
    <item>
      <title>Surviving iOS NetworkExtension's 50 MB Jetsam Limit with a Go Core</title>
      <dc:creator>Moein Sadraei</dc:creator>
      <pubDate>Wed, 02 Dec 2026 08:48:53 +0000</pubDate>
      <link>https://dev.to/moeinsadraei/surviving-ios-networkextensions-50-mb-jetsam-limit-with-a-go-core-he4</link>
      <guid>https://dev.to/moeinsadraei/surviving-ios-networkextensions-50-mb-jetsam-limit-with-a-go-core-he4</guid>
      <description>&lt;p&gt;If you have ever tried embedding a Go-based networking core like Xray-core inside an iOS &lt;code&gt;NEPacketTunnelProvider&lt;/code&gt;, you have probably hit the same wall: the tunnel works fine in light testing, and then iOS silently kills the extension under heavy load.&lt;/p&gt;

&lt;p&gt;On iOS, Network Extensions run in a tightly constrained process with roughly a 50 MB jetsam memory limit. Exceed it even briefly during a traffic spike, and the system terminates the extension without warning.&lt;/p&gt;

&lt;p&gt;When building &lt;strong&gt;MXray&lt;/strong&gt; (an open-source Swift package for running Xray-powered tunnels on iOS 15+ and macOS 13+), most of the real work was not wrapping the Go library. It was keeping the runtime predictable inside that 50 MB ceiling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Go cores blow past 50 MB on iOS
&lt;/h2&gt;

&lt;p&gt;Go's garbage collector is designed for servers with gigabytes of RAM, not mobile extensions. Left at default settings, the runtime delays collection and lets the heap grow until it hits Apple's hard limit.&lt;/p&gt;

&lt;p&gt;On top of the Go runtime itself, a few protocol behaviors quietly eat memory over time:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Long-lived UDP DNS sessions:&lt;/strong&gt; Apps and background services fire off dozens of DNS queries. If UDP session timeouts are too generous, idle NAT/session state piles up in memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Large HTTP/2 receive windows:&lt;/strong&gt; Transports like XHTTP can buffer significant data per stream when the default HTTP/2 window sizes are used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Kernel buffer pressure on Darwin TUN:&lt;/strong&gt; Under bursty throughput, writing packets back to the Darwin TUN interface can fail transiently with &lt;code&gt;ENOBUFS&lt;/code&gt;. Dropping or mishandling those writes leads to stalled connections and retry storms.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What actually keeps memory flat
&lt;/h2&gt;

&lt;p&gt;To make Xray-core reliable inside a Packet Tunnel on iOS, you have to tune both the Go runtime and the transport defaults together:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Set a mobile Go heap ceiling:&lt;/strong&gt; Constraining the Go heap forces the garbage collector to run earlier and more aggressively, trading a tiny amount of CPU for predictable memory usage well below 50 MB.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shorten UDP DNS idle timeouts:&lt;/strong&gt; Cleaning up finished DNS mappings quickly prevents gradual memory creep during normal browsing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cap XHTTP HTTP/2 receive windows:&lt;/strong&gt; Bounding the receive window keeps per-connection buffering predictable when downloading large payloads on fast cellular or Wi-Fi links.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retry on Darwin TUN &lt;code&gt;ENOBUFS&lt;/code&gt;:&lt;/strong&gt; Instead of treating &lt;code&gt;ENOBUFS&lt;/code&gt; as a fatal write error, brief retries let the kernel drain the buffer without tearing down active flows.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Wrapping it for Swift apps
&lt;/h2&gt;

&lt;p&gt;Once the runtime behavior is stable, the app-side integration should stay simple. In MXray, the extension side only needs to subclass &lt;code&gt;XrayPacketTunnelProvider&lt;/code&gt;, while the host app talks to it through &lt;code&gt;XrayTunnelController&lt;/code&gt; (connect/disconnect and live traffic stats) and &lt;code&gt;GeoFilesLoader&lt;/code&gt; for managing geoip/geosite assets.&lt;/p&gt;

&lt;p&gt;To avoid manually recompiling &lt;code&gt;LibXray.xcframework&lt;/code&gt; every time upstream changes, a weekly GitHub Actions workflow rebuilds the XCFramework from a patched fork and updates &lt;code&gt;Package.swift&lt;/code&gt; automatically.&lt;/p&gt;

&lt;p&gt;If you are building a VPN or networking tool on Apple platforms and want to see how these pieces fit together, the package is open-source under Apache-2.0 and listed on the Swift Package Index:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/themoein100/MXray" rel="noopener noreferrer"&gt;https://github.com/themoein100/MXray&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Swift Package Index: &lt;a href="https://swiftpackageindex.com/themoein100/MXray" rel="noopener noreferrer"&gt;https://swiftpackageindex.com/themoein100/MXray&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>networking</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
