{"id":476,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:41","modified_gmt":"2020-08-22T20:26:41","slug":"this-is-the-page-title-toplevel-309","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=476","title":{"rendered":"Virtual Memory Basics"},"content":{"rendered":"\n<title>Virtual Memory Basics<\/title>\n<p>\nOne interesting aspect about modern operating systems is the fact that\nthey can run programs that require more memory than the system actually has.\nLike the Tardis in Dr. Who, Linux memory is much bigger on the inside than\non the outside.\n<\/p>\n<p>\nAt the extreme end, this means that if your CPU is 32-bit (meaning that it has\n<glossary>register<\/glossary>s that are 32-bits), you can access up to\n2<sup>32<\/sup> bytes (that 4,294,967,296 or 4 <b>billion<\/b>). That means you\nwould need 4 Gb of main memory (<glossary>RAM<\/glossary>) in order to to completely take\nadvantage of this.\nAlthough many systems are currently available (2007) with 512 MB or\neven 1 GB, more RAM than that is rare; and 4 GB is extremely rare\nfor a home PC.\n<\/p>\n<p>\nThe interesting thing is that when you sum the memory requirements\nof the programs you are running, you often reach far beyond the physical memory\nyou have. Currently one of my system appears to need about 570 Mb. Although my machine only has 384 Mb.\nSurprisingly enough, I don&#8217;t notice any performance problems. So, how is this\npossible?\n<\/p>\n<p>\nWell, Linux, Unix and many other operating systems take advantage of the\nfact that most programs don&#8217;t use all of the memory that they &#8220;require&#8221;,\nas you typically do not use every part of the program at once.\nFor example, you might be using a word processor,\nbut not currently using the spell checking feature, or printing function,\nso there is no need to keep these in memory at the same time.\nAlso, while you are using\nyour word processor, your email program is probably sitting around doing\nnothing.\n<\/p>\n<p>\nFrom the user&#8217;s perspective the email program (or parts of the word processor)\nare loaded into memory. However, the system only loads what it needs. In\nsome cases, they might all be in memory at once. However, if you load enough\nprograms, you eventually reach a point where you have more programs than you\nhave memory.\n<p>\nTo solve this problem, Linux uses something called &#8220;virtual memory&#8221;.\nIt&#8217;s virtual because it can use more than you actually have. In fact, with\nvirtual memory you can use the whole 2<sup>32<\/sup> bytes.\nBasically, what this means is that you can run more programs at once without\nthe need for buying more memory.\n<\/p>\n<question id=\"\" type=\"\" text=\"When a program starts, Linux loads all of it into memory.\" \/>\n<concept id=\"\" description=\"When a program starts, Linux does not load all of it, just the portion it takes to get started.\" \/>\n<concept id=\"\" description=\"An important aspect of virtual memory is keeping parts of the program that are not needed on the hard disk.\" \/>\n<p>\nWhen a program starts, Linux does not load all of it, just the portion\nit takes to get started.\nAn important aspect of virtual memory is keeping parts of the program that are\nnot needed on the hard disk. As the process runs,\nwhen it finds it needs other parts of the program, it goes and gets\nthem. Those parts that are never needed are never loaded and the\nsystem does not use all of the memory it appears to &#8220;require&#8221;.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What is the process of moving data to and from the hard disk as it is needed?\" \/>\n<concept id=\"\" description=\"The process of moving data to and from the hard disk like this is called swapping.\" \/>\n<p>\nIf you have more <b>data<\/b> than physical memory, the system might store\nit temporarily on the hard disk should it not be needed at the moment. The\nprocess of moving data to and from the hard disk like this is called\n<glossary>swapping<\/glossary>, as the data is &#8220;swapped&#8221; in and out.\nTypically, when you install the system, you define a specific partition\nas the swap partition, or swap &#8220;space&#8221;. However, Linux can also swap to\na physical file, although with older Linux versions this is much slower than\na special partition.\nAn old rule of thumb is that you have at least as much swap space as you do\nphysical RAM, this ensures that all of the data can be swapped out,\nif necessary. You will also find that some texts say that you should have\nat least <i>twice<\/i> as much swap as physical <glossary>RAM<\/glossary>. We go into details on swap in the section in <site id=\"331\">installing and upgrading<\/site>.\n<\/p>\n<p>\nIn order to do all this, the system needs to manage your memory. This\nfunction is logically called &#8220;memory management&#8221; and is one of the core aspects\nof any modern operating system. Although the details are different from one\noperating system to the next, the basic principles apply, even between\ndifferent <glossary>CPU<\/glossary> types.\n<\/p>\n<p>\nIn other sections of the tutorial, we will talk about the details of memory management\nfrom both the <site id=\"119\">perspective of the CPU<\/site> and\nthe <site id=\"260\">operating system kernel<\/site>.<br \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Virtual Memory Basics One interesting aspect about modern operating systems is the fact that they can run programs that require more memory than the system actually has. Like the Tardis in Dr. Who, Linux memory is much bigger on the &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=476\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-476","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=476"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/476\/revisions"}],"predecessor-version":[{"id":786,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/476\/revisions\/786"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}