
iOS Safari and Memory Management - ymolodtsov
https://ilyabirman.net/meanwhile/all/ios-safari-and-memory-management/
======
gok
It's unfortunate how widely "virtual memory" is misunderstood to mean "an
unlimited dynamic swap file". Why phones don't have unlimited dynamic swap and
"why cannot Safari just save what it has downloaded to disk" are effectively
the same question, just asked about the browser instead of the kernel. It
would wear out the NAND chips on phone-class storage too quickly.

~~~
scarface74
It's no more "misunderstood" than "serverless", when someone feels the need to
explain to the poor unwashed masses that servers are involved and your
programs aren't being run using pixie dust. VM == Swap is shorthand that
people have been using for well over two decades.

~~~
mort96
The word "serverless" is a new word which wasn't used for anything in
particular, and it's fairly descriptive, as you don't have to manage severs,
you just have code which runs whenever you make certain HTTP requests.
Obviously there's a server involved, but you don't have to care about it (for
the most part; obviously abstractions are leaky).

The phrase "vrtual memory" is old and still widely used, and it refers to...
virtual memory. The memory addresses programs use are virtual addresses into
virtual memory, and the kernel sets up lookup tables to let the CPU resolve
that virtual memory address into a physical memory address. Saying that "VM ==
Swap" might be common, but it still seems completely wrong, and makes people
make ridiculous claims such as "unlike on the Mac, there is no virtual memory
on the iPhone".

~~~
scarface74
In context of the article, do you really think that most readers on HN didn’t
immediately know that the article was referring to a swap file?

If you search on google right now for “virtual memory” for articles geared
toward everyday people, you will see that the common vernacular is to refer to
it being the same as the swap file. It’s referred to that way in Windows now
and I remember “turning off virtual memory” being a control panel option on
MacOS System 7 in 1991.

If someone said that the “army was decimated” would you think, knowing how
language has evolved, that they meant the army was reduced by 10% or that it
was left in ruins? Would you go on to explain to the ignorant plebe and start
a sentence with “well actually...”?

When the term “virtual memory” came into the vernacular of everyday people, it
was understood to mean swapping to disk.

It’s the same old descriptivism vs prescriptivism debate. Most of the time
when someone lands on the prescriptivism side it comes off as pedantic.

Would you correct someone who said that they were buying a PC instead of Mac
and say “well actually a Mac is s personal computer therefore it is also a
PC”?

~~~
Someone
_”and I remember “turning off virtual memory” being a control panel option on
MacOS System 7 in 1991”_

I think that truly turned off virtual memory (Mac OS 7 didn’t have separate
address spaces, did it?)

~~~
scarface74
No it didn’t. There was only one address space. So yeah, it did literally turn
off virtual memory.

I went down a rabbit hole trying to remember the technical details of VM back
in the day:

[https://retrocomputing.stackexchange.com/questions/2804/why-...](https://retrocomputing.stackexchange.com/questions/2804/why-
did-mac-os-7-perform-poorly-with-virtual-memory-enabled)

[old man rant] I remember having at the time an obscene 10MB of RAM in 1992
for my Mac LCII to avoid using VM and to run Soft PC acceptably.

Then I had a whopping 24MB of RAM two years later for the PowerMac 6100/60
because it shared RAM with the DX2/66 DOS Card.

Then two years later I bought 32MB of RAM to attach directly to the DOS
Card... [/old_man_rant]

------
gumby
> My photos and videos don’t get destroyed when the phone is low on memory,
> why webpages should?

Well, they can be. That is, my iOS devices are set to have only thumbnails
(plus most recently taken/viewed) on the device and dynamically load anything
I want to look at (upscaling the thumbnail and then faulting in the full
picture). The device memory is treated as a cache which can be flushed as
needed.

It makes sense for Safari to do the same since most pages are reloadable (I
wonder what it does with nocache-marked pages). And in particular, old tabs
are unlikely to be looked at again. You can do some experiments: I'd bet forms
are not purged either.

~~~
ymolodtsov
Forms are almost always purged. Had it happened with Google Forms.

------
_bxg1
This seems like a... misguided question. How did it make the front page? Was
it just because lots of people chimed in with corrections?

~~~
hombre_fatal
That made me chuckle. Reminds me of the temptation to upvote every comment
upstream of yours so that your snarky response gets more visibility even
though the whole thread should probably be downvoted and sunk to the bottom.

------
ymolodtsov
This is a thing I never was able to understand — why it just doesn't cache the
state of the page? I'd never even try filling a complicated web form on the
iPad because it can reload the webpage anytime it pleases.

~~~
code_duck
This happens to apps like Instagram, too. I could be making a post, have the
photo all edited, put in 27 hashtags, then go to Safari to research another
tag and... Come back and the entire app reloads, without saving the draft.

~~~
minikites
I think this is Instagram's fault, there have been lots of times where I
absentmindedly leave the Instagram app for the home screen and tap on
Instagram again a second later and the app reloads. My hunch is that it earns
them another chance to show ads.

~~~
kitsunesoba
It could also be iOS aggressively killing resource hog apps, as it tends to
do. As a dev, I’ve found that the apps that I work on almost never get killed
by the OS, sometimes sitting in memory for several days, because they’re quite
conservative with resource use. Many of the (often needlessly) heavier apps by
the “big guys” on the other hand get killed on a regular basis, even on
devices with a lot of RAM.

------
rhinoceraptor
iOS does have virtual memory, it just doesn't swap pages to disk. The first
iPhone had very slow flash, so swapping pages would totally kill performance.

~~~
ymolodtsov
It's a shame we all still go around with the first iPhones in our pockets for
sure :)

~~~
rhinoceraptor
I think in most cases the tradeoff of re-initializing app state, or forcing
developers to cache state to disk manually is better than forcing the poor
phone's disk to constantly swap and having developers be lazy and consume a
ton of RAM.

------
pdimitar
Using iPhone 8 Plus 256GB so I can't agree with other commenters this
behaviour is due to low storage. I have about 180GB free.

It seems Safari / iOS memory management isn't very clever. Sometimes Safari
reloads pages _I just left_. Super annoying if you started filling out
something there.

I don't understand why they don't just unload apps / pages on an LRU
principle: whatever you do, do NOT unload an app / page the user was just in.

I like a lot of things about the iPhone and iOS but this peculiarity ain't one
of them. It feels very backwards.

\---

EDIT: I also heard the storage is almost at the laptop SSD class (if not
better). So I am not sure swapping memory to it here and there would amortise
it that badly. But I don't know for sure so not claiming anything.

------
asdfasgasdgasdg
I don't know for certain, but I do have a guess. Web pages are not represented
in browsers as a contiguous block of memory. They are represented as thousands
of objects that contain bits of data and point to other objects, GPU
resources, etc. I suspect it would be absurdly complicated to serialize these
objects to a form that could be stored on disk.

~~~
rayiner
Operating systems have been serializing arbitrary application memory to disk
for decades. Virtual memory makes it trivial. You swap a whole page out to
disk, and mark the page “not present” in the page table. When the app tries to
access that page, you get a page fault. The kernel handles the page fault by
copying the page back to memory (at an arbitrary page-aligned location in
physical memory) and updating the page table to point to that location. You
never need to serialize the browser data structures.

~~~
asdfasgasdgasdg
As the fine article tells, there is no swap on iOS. It sounds like a great
solution for the case where there is swap.

------
alphabettsy
His comments about iOS not having virtual memory are incorrect, but I rarely
if ever have the issues he’s talking about, but maybe it’s because I have only
used iPad Pro’s or iPhone Plus/Max devices in recent years which have more
memory.

I regularly have dozens of tabs open and only reopening a tab that hasn’t been
visited in some time results in a refresh.

~~~
ken
I have an iPad Mini (iOS 9). Two modern webpages is definitely enough to put
it over the top and reload each page when you switch between them. Sometimes
even a single webpage will reload for no apparent reason.

It's generally true that memory issues seem to go away if you throw enough
extra RAM at it.

Similarly, I recently upgraded from a GT120 (0.5GB) to an RX580 (8GB), and
miraculously, my Mac no longer crashes when I switch spaces quickly.

------
SigmundA
I honestly haven't seen this behavior in a while in iOS, yes it used to happen
all the time and I have lost a lot of mid edit posts, I used to copy the text
to the clipboard before switching tabs because of it.

I guess I assumed they did something using the bfcache and serializing page
state to disk in newer iOS if needed but perhaps my newer device just have
more ram than before?

------
skybrian
This is pretty annoying behavior. I'm wondering if websites can save drafts to
session storage as a workaround?

------
kccqzy
> and completely forget about being nice software.

So you think thrashing is nice? I find that difficult to believe.

