def clear_kernel_cache()

in ssiog/util.py [0:0]


def clear_kernel_cache(log):
  """
  Clears the Linux kernel cache (page cache, dentries, and inodes) H
  without invoking a shell command.
  """
  log.info(f"Before: {get_ram_info()}")
  try:
      # Open the file for writing with superuser privileges
      with open('/proc/sys/vm/drop_caches', 'w', encoding='utf-8') as f:
          f.write('1')  # Clear only data page cache not dentries.
      time.sleep(1)  # Wait for the caches to be cleared
      log.info("Kernel cache cleared successfully.")
  except (IOError, OSError) as e:
      log.error(f"Error clearing kernel cache: {e}")
      log.info(f"Falling back to clearing using shell command, for password less sudo access.")
      clear_kernel_cache_bash(log)
  log.info(f"After: {get_ram_info()}")