Finding Optimal Page Size


Operating system has a concept known as the optimal page size that is affected by a number of variables, such as the system architecture, the amount of physical memory at hand, and the workload of the running applications.

Steps/ Approach

The following steps can be used to find the ideal page size:

Step 1: Establish the system's design:Different CPU designs support varied page sizes. For instance, x86 CPUs typically offer 4KB page sizes, whereas ARM CPUs support 4KB, 16KB, or 64KB page sizes.

Step 2: Calculate the physical memory capacity:The ideal page size depends on the physical memory capacity. Larger page sizes can enhance performance by lowering the number of page table entries necessary, but they also raise the possibility of memory waste if a page is only partly used. Larger page sizes might be more appropriate if the machine has a lot of physical memory.

Step 3: Examine the application workload:This is important because it affects how big a website should be. Smaller page sizes might be more suitable, for instance, if the workload consists of numerous small memory allocations. Larger page sizes, on the other hand, might be more suitable if the workload calls for big memory allotments or working with large files.

Step 4: Check for different sizes:Try out various page sizes to find the best one after you have an understanding of the system architecture, the quantity of physical memory, and the workload of the applications. You can set the page size on many operating systems, so you can experiment with various values and gauge system speed to determine which page size is most effective.

Step 5: Monitor system performance:After you have set the page size, you should monitor 3system performance to ensure that it is meeting your requirements. If you notice performance issues, you may need to adjust the page size or investigate other system parameters to improve performance.

Here are a few scenarios of how to calculate the optimal page size for a system:

Scenario 1

A system has 8GB of physical memory and a workload that involves many small memory allocations. We can calculate the optimal page size using the following formula:

Page size = sqrt(2 x Physical memory / Number of page table entries)

For this example, let's assume we have 4KB page table entries. Plugging in the values, we get:

Page size  = sqrt(2 x 8GB / (8 x 1024 x 1024 x 1024 / 4KB))
      = sqrt(17179869184 / 2097152)
      = sqrt(8192)
      	      = 90.51 bytes

In this case, the optimal page size would be 4KB since it is the closest standard page size to 90.51 bytes.

Scenario 2

A system has 16GB of physical memory and a workload that involves working with large files. We can calculate the optimal page size using the following formula:

Page size = (Physical memory / Number of page table entries) / 2

Let's assume we have 2MB page table entries. Plugging in the values, we get:

Page size  = (16GB / (16 x 1024 x 1024 x 1024 / 2MB)) / 2
      = (16GB / 8192) / 2
      = 2MB

In this case, the optimal page size would be 2MB since it is the same as the page table entry size and is appropriate for working with large files.

Scenario 3

A system has 4GB of physical memory and a workload that involves a mix of small and large memory allocations. We can calculate the optimal page size using the following formula:

Page size = sqrt(2 x Physical memory x Average allocation size / Number of page table entries)

Let's assume we have 4KB page table entries and an average allocation size of 2KB. Plugging in the values, we get:

Page size = sqrt(2 x 4GB x 2KB / (4 x 1024 x 1024 x 1024 / 4KB))
      = sqrt(8589934592 / 1048576)
      = sqrt(8192)
      = 90.51 bytes

In this case, the optimal page size would be 4KB since it is the closest standard page size to 90.51 bytes and is appropriate for a mix of small and large memory allocations.

Pseudocode

Following is a pseudocode for finding the optimal page size:

// Set default values
page_sizes = [4KB, 8KB, 16KB, 32KB, 64KB]
physical_memory = get_physical_memory()
average_allocation_size = get_average_allocation_size()
number_of_page_table_entries = get_number_of_page_table_entries()

// Calculate optimal page size for each page size
for page_size in page_sizes:
    page_table_entry_size = page_size / 4KB  // Assume 4KB page table entries
    optimal_page_size = sqrt(2 * physical_memory * average_allocation_size / (number_of_page_table_entries * page_table_entry_size))
    print("Optimal page size for", page_size, "is", optimal_page_size, "bytes")

// Choose the page size with the lowest optimal page size
min_optimal_page_size = float('inf')
for page_size, optimal_page_size in optimal_page_sizes.items():
    if optimal_page_size < min_optimal_page_size:
        min_optimal_page_size = optimal_page_size
        chosen_page_size = page_size

print("Chosen page size is", chosen_page_size)

Note: You may need to modify it based on your specific needs and system requirements.

Java implementation

Here's an example Java implementation for finding the optimal page size:

Example

import java.lang.Math;
public class OptimalPageSize {
   public static void main(String[] args) {
      // Set default values
      int[] pageSizes = {4096, 8192, 16384, 32768, 65536};
      long physicalMemory = getPhysicalMemory();
      int averageAllocationSize = getAverageAllocationSize();
      int numberOfPageTableEntries = getNumberOfPageTableEntries();
      
      // Calculate optimal page size for each page size
      double[] optimalPageSizes = new double[pageSizes.length];
      for (int i = 0; i < pageSizes.length; i++) {
         int pageSize = pageSizes[i];
         int pageTableEntrySize = pageSize / 4096; // Assume 4KB page table entries
         double optimalPageSize = Math.sqrt(2 * physicalMemory * averageAllocationSize / (numberOfPageTableEntries * pageTableEntrySize));
         optimalPageSizes[i] = optimalPageSize;
         System.out.println("Optimal page size for " + pageSize + " is " + optimalPageSize + " bytes");
      }      
      // Choose the page size with the lowest optimal page size
      double minOptimalPageSize = Double.POSITIVE_INFINITY;
      int chosenPageSize = 0;
      for (int i = 0; i < pageSizes.length; i++) {
         if (optimalPageSizes[i] < minOptimalPageSize) {
            minOptimalPageSize = optimalPageSizes[i];
            chosenPageSize = pageSizes[i];
         }
      }
      System.out.println("Chosen page size is " + chosenPageSize);
   }   
   private static long getPhysicalMemory() {
      // Implementation for getting physical memory goes here
      return Runtime.getRuntime().totalMemory();
   }   
   private static int getAverageAllocationSize() {
      // Implementation for getting average allocation size goes here
      return 2048; // 2 KB
   }   
   private static int getNumberOfPageTableEntries() {
      // Implementation for getting number of page table entries goes here
      return 524288; // 2 GB / 4 KB
   }
}

Output

Optimal page size for 4096 is 1453.80053652487 bytes
Optimal page size for 8192 is 1027.9922178693766 bytes
Optimal page size for 16384 is 726.900268262435 bytes
Optimal page size for 32768 is 513.9961089346883 bytes
Optimal page size for 65536 is 363.4501341312175 bytes
Chosen page size is 65536

Updated on: 22-Aug-2023

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements