Какой смысл загружать программу в ОЗУ, если виртуальная память просто помещает ее обратно на диск?

1568
Celeritas

Прочитав этот вопрос о виртуальной памяти, я был вдохновлен, чтобы спросить: кажется, что ходить по кругу, когда программа запускается, она загружается в память с диска, но затем не хватает места в памяти, поэтому она загружается обратно на диск (как файл подкачки). Нужно ли компьютерам изначально загружать в память 100% программы? Я вспоминаю, что узнал о чем-то, что дает каждому процессу ощущение, что он работает один в адресном пространстве, поэтому он может начинаться с самого первого адреса, является ли это основной причиной виртуализации памяти или это не имеет к этому никакого отношения?

1
Некоторые программы могут выбирать, что здесь делать (использовать оперативную или виртуальную память и т. Д.). Тем не менее, как общее правило, виртуальная память используется, когда у вас заканчивается ОЗУ. Dave 10 лет назад 3
Приведенный выше комментарий является вашим ответом. легко и просто. Lorenzo Von Matterhorn 10 лет назад 0

2 ответа на вопрос

1
barlop

Reason is Speed. Also a process can grow while it runs, like k-meleon. Or spawn new processes while it runs like chrome.exe. Cache memory is faster than RAM which is faster than disk.

So things get put in cache where cache is available. Ideal is if CPU is executing things in cache. Second to that is if it's executing things in RAM.

CPU never processes things on disk, it doesn't address things on disk, it only addresses RAM locations or no doubt, cache locations. Or locations that read/write to devices.

It's not like everything even everything you might want to run is swapped out to disk and then back to RAM. Only Things not currently being used and not likely to be used soon are preferred when swapping things out, so as to free RAM up for things that are more in use.

Web browsers have a cache too, a disk cache! They store things locally on disk, rather than fetch from from online. There are web proxies that you can run locally that store a website locally and if it changes online then it will fetch. This enables you to browser much quicker. (though I suppose you can run into the problem of a website not specifying properly that a change was made, and you get stuck loading an old copy from your local disk cache) but the idea is that it's faster. In that situation the quicker area is the local hard disk and the slower area is the remote/online area.

You do this kind of swapping in life, with things you need near you, and things you don't need in storage. You don't put everything in storage/garage arguing that there's loads of room in storage/garage. It'd be too slow.

0
X Tian
  1. The organisation on disk can (almost certainly will) be different so the page/swap file partition is optimised to load pages of virtual memory whilst your every-day file-system will be optimised for it's particular characteristics, eg "don't waste disk space".
  2. Also your executable is not quite runable whilst on disk, relative pointers are adjusted to the virtual address space upon loading, so once this has been done upon load it's all there ready to be paged in and out.
  3. Your program may be minuscule on disk, and then the first thing it does is allocate a huge chunk of memory for something, this can get paged out into swap for use later in the programs execution.

To sum-up it's generally faster to do it the way it works.

Похожие вопросы