6 ms·
iOS Safari and Memory Management
- ymolodtsov 7y agoThis 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 7y agoThis 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 7y agoI 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.
- code_duck 7y agoFacebook definitely does not care about the user returning to the same place, ever, or having any sort of continuity or coherence. I think that they do bank on confusion somehow making them more money. What I dislike is opening Instagram, and it shows me posts loaded the last time, and I want to click on them... But before I can, it reloads and shows me an entirely new semi-random selection of posts. Basically just like the Facebook news feed, you might see things that you wish to go back to, but have no hope of finding them ever again unless you have a great memory and are very dedicated.
- kitsunesoba 7y agoIt 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.
- kdot 7y agoYour device may be low on storage. I've done what you've described, and come back hours later with the app still in the same state, even with video.
- code_duck 7y agoIt was only an iPhone 6, and sometimes I was low on storage, not other times. Storage depended on whether iCloud was actually syncing photos or not. I assumed that it was about being on low on resources, since it was an iPhone 6 running 2019 Instagram.
- selectodude 7y agoThe iPhone 6 was a dog, unfortunately.
- code_duck 7y agoI was fairly satisfied with it overall, especially given did I bought it outright for a low price. However, recently the home button stopped working after less than two years.
- _jal 7y agoBrowsers are not editors. I think it is that simple - mentally, the model is browsers are for consumption, so who cares about state? Contrast that with people writing editors - the mere idea of tossing the user's edits randomly is simply absurd. Add in creepy surveillance features like javascript to capture your pre-submit edits and the other awful crap people get up to, and I write things in editors and paste in to browsers.
- asdfasgasdgasdg 7y agoI 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 7y agoOperating 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 7y agoAs the fine article tells, there is no swap on iOS. It sounds like a great solution for the case where there is swap.
- vbezhenar 7y agoMost of these resources should be easy to recreate. Basically you need to save DOM, JavaScript objects, things like timers. It should be possible.
- olliej 7y agoYou have to save a huge amount of state. The complete JS object graph, the page layout, torn off elements, network requests, separate threads (the workers), etc, etc Sure this is technically possible, because we already know that it’s possible to suspend a virtual machine, but that doesn’t mean it’s a reasonable use of engineering resources. It also fails to acknowledge the very first problem: IOS doesn’t use swap because of flash wear (or something?), but what you’re proposing is essentially inventing the same problem again, but this time in user space with a pile a expensive heap traversal added for fun.
- rhinoceraptor 7y agoiOS 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 7y agoIt's a shame we all still go around with the first iPhones in our pockets for sure :)
- rhinoceraptor 7y agoI 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.
- 0815test 7y agoMore relevantly, swapping pages to flash storage would kill the flash storage itself. (Flash memory has terrible write endurance, and what endurance it has relies on obscure GC algorithms that would interact quite terribly with a "swap file"-like pattern!) Performance isn't actually a huge concern until you start swapping out your working set (i.e. actively threshing); even swapping to spinning rust, which has terrible performance, is better than having no swap at all! Edit: You could still swap out to "external", modular storage like an SD card, since killing that would be no big deal - just treat it as disposable. But no SD card slot on iPhones :-P
- gok 7y agoIt'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 7y agoIt'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 7y agoThe 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 7y agoIn 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”?
- skybrian 7y agoThis is pretty annoying behavior. I'm wondering if websites can save drafts to session storage as a workaround?
- gumby 7y ago> 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 7y agoForms are almost always purged. Had it happened with Google Forms.
- SigmundA 7y agoI 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?
- alphabettsy 7y agoHis 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 7y agoI 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.
- ymolodtsov 7y agoHe clearly meant the swap file.
- kccqzy 7y ago> and completely forget about being nice software. So you think thrashing is nice? I find that difficult to believe.
- pdimitar 7y agoUsing 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.
- _bxg1 7y agoThis 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 7y agoThat 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.