WKWebView crash out of memory: 100 images
Opening my game's card collection killed the app with no JavaScript error. The viewer drew all 100 full-size images at once. What I measured, and what not.
Contents
The bug report was one line: open the stats screen in ASMR Logo Craft Block, go to the collection, tap a picture you have collected, and the app crashes.
The game is React 19 and Vite inside a Capacitor 8 WebView. It ships on Google Play only, but I keep an iOS build running for development, and this was the iPhone build going down. Every iOS number below comes from the iOS 18.1 simulator (an iPhone 16 Pro profile) on my Mac. The physical iPhone got the fixed build installed and never got tested, for a reason I come to at the end.
The short version: there was no bug in the code, in the sense of anything that throws. The collection viewer drew all 100 cards the moment it opened, every one a full-size decoded picture. A WKWebView that runs out of memory does not raise a JavaScript error: iOS kills its web process, and from the outside that looks like a crash.
No JavaScript error, no crash log
A crash on one tap usually means an exception, so that is where I started. The viewer was a small component: a horizontal scroll-snap strip, one page per collection, opening on the card you tapped. I checked every import and every string key it used, because a missing export throws on render. All present.
Then I reproduced it outside the phone. Playwright's WebKit build, a 430×932 viewport at
deviceScaleFactor: 3, progress seeded so some cards were collected, tap the card:
viewer open: 1 pages: 100
no errors
The viewer opened. A hundred pages, no pageerror, nothing on the console.
Next, the three Android emulators. I searched each one's logcat for a WebView renderer death
(onRenderProcessGone, Fatal signal, the low-memory killer). Nothing on any of them.
Last, the iPhone's own crash logs, which you can list without Xcode's Organizer:
xcrun devicectl device info files --device <device-id> --domain-type systemCrashLogs
The only JetsamEvent report that day named a different app as the largest process, so I
set it aside. There was no crash report for my app at all. I do not know for certain whether
a killed WebContent process leaves a report you can pull this way; I only know I found none.
So: no exception in WebKit, no failure on Android, nothing in the logs. That pointed away from code. Of the explanations I had, a WKWebView crash out of memory was the one that fit all three.
What the viewer was drawing
The collection has 100 cards, one per group of ten levels, each with a cover picture. On disk they are WebP files at 600×900, about 150 KB each, 13 MB for the folder. That is the number you see in the bundle, and it is the wrong one. A decoded image costs width × height × 4 bytes no matter how well it compressed:
600 × 900 × 4 bytes = 2.16 MB per picture
× 100 pictures = 216 MB decoded
I wrote about the same trap with a sprite sheet in Sprite sheet texture atlas: 10.1 MB to 2.7 MB: 250 KB on disk, 10.1 MB once decoded. Here it was a hundred of them.
The pictures were not the only cost. Every card that was not the one showing got a small 3D
tilt, rotateY(8deg) scale(0.94) to one side or the other, so 99 cards each had a transform
on them. My estimate for that part went like this: a viewer card is up to 570 points tall at
a 2:3 ratio, so about 380×570, which at 3× is 1140×1710 pixels, or 7.8 MB at 4 bytes a pixel.
If every tilted card got its own layer at full resolution, a hundred of them come to roughly
780 MB. Add the pictures and you are near a gigabyte.
That 780 MB is arithmetic and nothing more. I did not measure it, and I cannot say WebKit gives every off-screen tilted card a full backing store. What I could measure was the part that showed up in the web process.
Measuring WebContent memory in the simulator
The simulator's WebContent processes run as ordinary processes on the Mac, so macOS's
footprint tool can read them directly:
ps -axo pid,command | grep WebContentExtension | grep CoreSimulator
footprint <pid>
To drive the page I used the WebKit remote inspector through appium-remote-debugger, with
a small script: tap Stats, open the Collection tab, open a card, read memory, swipe six times,
read memory again. Two things got in the way, and both are worth knowing if you try this:
- The simulator's web inspector listens on a unix socket, not on TCP. Connecting to the
default port gave
ECONNREFUSED. The socket path is in the simulator's environment:xcrun simctl spawn <sim> launchctl getenv RWI_LISTEN_SOCKET. - The inspector can only reach the page, not native UI. The first run found no Stats button because the native ad-consent dialog was covering the game, and the next one because a test interstitial was. I let my device-test runner answer the consent form, then seeded a save with ads removed so nothing native would cover the page.
With that in place, the same probe on the old viewer and the fixed one:
| WebContent growth | Old viewer | Fixed viewer |
|---|---|---|
| after opening on a card | 115 → 135 MB (+20) | 106 → 125 MB (+19) |
| after six swipes | 115 → 306 MB (+191) | 106 → 127 MB (+21) |
| cards drawn | all 100 | 3 |
Opening cost about the same either way. Swiping is where they parted: the old viewer grew by 191 MB over six swipes, the fixed one by 21 MB.
The old viewer did not die in the simulator. The Mac has far more memory than a phone, and I
am not sure how much of the GPU side footprint sees for a simulated device, so I read 191 MB
as a floor for what the phone would pay.
The fix: draw three cards, not a hundred
The home screen already had this problem solved. Its collection strip draws only the panel showing and one on each side; every other panel is an empty box holding its place. That rule was added earlier for frame time rather than memory: a hundred drawn panels made each frame restyle thousands of elements nobody could see. The viewer was written later and never got it.
So the viewer now does the same. A simplified version of the idea, not the game's code:
const DRAWN_AROUND = 1;
{cards.map((card) =>
Math.abs(card.index - current) > DRAWN_AROUND
? <div key={card.index} className="page" /> // keeps its width on the strip
: (
<div key={card.index} className="page">
<Card card={card} tilt={Math.sign(card.index - current)} />
</div>
))}
The empty pages matter. They keep the strip 100 pages wide, so scroll-snap positions, the
3/100 counter and opening on the tapped card all work as before. The other piece was already
in the CSS: scroll-snap-stop: always on each page, so one swipe can never pass more than one
page. The card a swipe brings in is always a neighbour, and the neighbours are already drawn.
How I checked it
- A unit test opens the viewer on card 2 and asserts that only pages 1, 2 and 3 have content, then swipes and checks the window moves. I ran it against the old viewer first: it failed there and passes on the new one. The full suite, 7,954 tests, passes.
- WebKit in Playwright: drawn pages
[0, 1, 2]on open,[1, 2, 3]after a swipe, counter reading3/100, and a screenshot that looks right. - A device test taps a collected card, checks the viewer is up, closes it and checks the app is still on the stats screen. It passes on the iOS simulator and on an Android emulator. It does not swipe. My runner could not make a reliable sideways drag there: after the swipe landed, it still counted the card that had scrolled off-screen as visible. A test that fails on a correct screen is worse than a shorter one, so I cut the swipe out.
- The built bundles: I grepped both the iOS and the Android web bundles for the change, so the numbers above are for the code I thought they were.
What I have not proven
I never watched it crash. Not on the phone, not in the simulator, not in WebKit on the Mac. The case for "out of memory" is the absence of any error anywhere, plus a viewer that measurably grew its web process by 191 MB in six swipes where the fix grows it by 21.
I have not confirmed the fix on the iPhone. The fixed build is installed there, but the phone
was paired over Wi-Fi. That is enough for devicectl to install and launch, and not enough
for the web inspector, which needs USB. The remote debugger stopped at:
Error: Could not find the expected device '<udid>'
So the phone check is still a manual one: open a collected card and swipe through a few.
And "about a gigabyte" is an estimate. The 216 MB of pictures is exact arithmetic on known files. The rest is my calculation of what the tilted layers could cost, which I could not measure from the simulator.
Takeaway
If a WebView app "crashes" on iOS and nothing throws, look at memory before you look at code. WKWebView reports running out of memory by not being there any more.
For any strip of full-size images, count decoded bytes, not file bytes, and draw a window:
the item showing and its neighbours. Keep the rest as empty boxes so the layout does not
change. And measure the WebContent process in the simulator with footprint before and after,
because "it stopped crashing on my phone" is a much weaker check than a number that drops
from 191 MB to 21.