1 00:00:00,000 --> 00:00:12,000 In this video, I want to tell you a little bit more about analyzing memory statistics. 2 00:00:12,000 --> 00:00:16,000 First, it helps if you know about the right terminology. 3 00:00:16,000 --> 00:00:20,000 Cache is an area of memory that is used as temporary storage 4 00:00:20,000 --> 00:00:25,000 for frequently accessed data for faster access. 5 00:00:25,000 --> 00:00:27,000 You have cache at different levels. 6 00:00:27,000 --> 00:00:29,000 From disk, you cache in RAM. 7 00:00:29,000 --> 00:00:33,000 There's internet cache, for instance, as well, that can be used on a proxy, 8 00:00:33,000 --> 00:00:36,000 which is at a completely different level. 9 00:00:36,000 --> 00:00:41,000 Linux buffer memory is used for temporary storage in memory for data while it is moved. 10 00:00:41,000 --> 00:00:44,000 Most commonly used are the write buffers. 11 00:00:44,000 --> 00:00:48,000 So when you write a file, the file is committed to memory, 12 00:00:48,000 --> 00:00:53,000 and then it sits in the buffers until it's time to flush the data to disk. 13 00:00:53,000 --> 00:00:57,000 Anonymous memory is memory that is allocated by applications. 14 00:00:57,000 --> 00:01:03,000 Swap is emulated RAM on disk, which can be used for anonymous memory. 15 00:01:03,000 --> 00:01:08,000 Virtual memory is an address space that is determined by the CPU architecture. 16 00:01:08,000 --> 00:01:10,000 It has nothing to do with swap. 17 00:01:10,000 --> 00:01:15,000 The thing is that every Linux application wants to allocate a certain amount of memory. 18 00:01:15,000 --> 00:01:16,000 That's a reservation. 19 00:01:16,000 --> 00:01:18,000 That doesn't mean that it is used, 20 00:01:18,000 --> 00:01:21,000 but it means that it is blocked for that specific application. 21 00:01:21,000 --> 00:01:25,000 And from the virtual memory, a resident memory is obtained, 22 00:01:25,000 --> 00:01:28,000 and that is memory that is actually being used. 23 00:01:28,000 --> 00:01:32,000 Active memory is memory that has recently been used, 24 00:01:32,000 --> 00:01:36,000 and inactive memory is memory that has not recently been used. 25 00:01:36,000 --> 00:01:39,000 This is pretty important for the Linux kernel to determine 26 00:01:39,000 --> 00:01:43,000 which memory pages are a candidate for moving to swap, 27 00:01:43,000 --> 00:01:45,000 or if the memory pages are in cache. 28 00:01:45,000 --> 00:01:50,000 The Linux kernel uses this to determine which memory pages can be dropped 29 00:01:50,000 --> 00:01:52,000 if more memory is needed. 30 00:01:52,000 --> 00:01:56,000 As I just mentioned, inactive file memory is eligible to be dropped 31 00:01:56,000 --> 00:01:58,000 in case of memory shortage, 32 00:01:58,000 --> 00:02:02,000 and inactive anonymous memory is eligible to be moved to swap 33 00:02:02,000 --> 00:02:04,000 in case of memory shortage. 34 00:02:04,000 --> 00:02:06,000 Now, if memory shortage is bad, 35 00:02:06,000 --> 00:02:09,000 then active anonymous memory may be moved to swap as well, 36 00:02:09,000 --> 00:02:13,000 and that is also bad because your system will be very busy moving data 37 00:02:13,000 --> 00:02:18,000 in and out of swap, and that won't make it any faster. 38 00:02:18,000 --> 00:02:23,000 To monitor memory, 3-M is a good start for a generic overview. 39 00:02:23,000 --> 00:02:27,000 VMstat gives more detailed memory usage information, 40 00:02:27,000 --> 00:02:31,000 and for the ultimate detail, you want to check out cat-proc meminfo. 41 00:02:31,000 --> 00:02:35,000 That is where the kernel stores information about memory. 42 00:02:35,000 --> 00:02:38,000 Let's check it out. 43 00:02:38,000 --> 00:02:43,000 So here we have 3-M output, where we see the total amount of memory, 44 00:02:43,000 --> 00:02:47,000 the amount of memory that is used, and the amount of memory that is free. 45 00:02:47,000 --> 00:02:52,000 And, hey, in case you are thinking, well, between used and free and total, 46 00:02:52,000 --> 00:02:54,000 there's a difference. What is in this difference? 47 00:02:54,000 --> 00:02:56,000 Well, that is your buffer cache. 48 00:02:56,000 --> 00:03:01,000 Total memory is buffer cache plus shared plus free plus used. 49 00:03:01,000 --> 00:03:05,000 Now, the available parameter considers memory that is currently free, 50 00:03:05,000 --> 00:03:09,000 but also inactive memory in cache, 51 00:03:09,000 --> 00:03:12,000 which can be cleared immediately to make space for more memory. 52 00:03:12,000 --> 00:03:15,000 This system also has 2 GB of swap, 53 00:03:15,000 --> 00:03:19,000 but the 2 GB of swap is not used at all, and that is what we see here. 54 00:03:19,000 --> 00:03:24,000 There's no need to use swap, and the Linux kernel doesn't favor 55 00:03:24,000 --> 00:03:28,000 moving inactive anonymous memory to swap if it doesn't have to. 56 00:03:28,000 --> 00:03:31,000 I mean, we have enough memory available here. 57 00:03:31,000 --> 00:03:35,000 Now, VMstat is used with a couple of parameters. 58 00:03:35,000 --> 00:03:38,000 I'm using VMstat 2.5, which is more than enough. 59 00:03:38,000 --> 00:03:41,000 The 2 is for a 2-second interval, 60 00:03:41,000 --> 00:03:44,000 and the 5 means that I want to do it 5 times. 61 00:03:44,000 --> 00:03:48,000 And right here, I can see my overview of memory, 62 00:03:48,000 --> 00:03:53,000 and I can see SISO, which is indicating the movement of memory. 63 00:03:53,000 --> 00:03:55,000 Swap in, swap out. 64 00:03:55,000 --> 00:03:58,000 So if I have swap that is actively being used, 65 00:03:58,000 --> 00:04:00,000 I should see a high activity here. 66 00:04:00,000 --> 00:04:04,000 Now, finally, if you want to know more about the state of your system, 67 00:04:04,000 --> 00:04:07,000 check out procmeminfo. 68 00:04:07,000 --> 00:04:10,000 In procmeminfo, you find a lot of parameters. 69 00:04:10,000 --> 00:04:14,000 The parameters that are most interesting are right here, 70 00:04:14,000 --> 00:04:16,000 active and inactive memory. 71 00:04:16,000 --> 00:04:19,000 So active is what really needs to be used 72 00:04:19,000 --> 00:04:21,000 and needs to be in your RAM, and inactive, 73 00:04:21,000 --> 00:04:24,000 well, it could be doing something else as well. 74 00:04:24,000 --> 00:04:26,000 And as you can see on this system, 75 00:04:26,000 --> 00:04:29,000 we have about 2 gigabytes of inactive memory, 76 00:04:29,000 --> 00:04:31,000 and that explains a lot about the current state 77 00:04:31,000 --> 00:04:33,000 of what we see in 3-M.