Primary Secondary Virtual Memory - CSU360 - Shoolini University

Practical 6: Read and Write about primary, secondary and virtual memory

6.1. Primary Memory

Primary memory, also known as main memory or RAM (Random Access Memory), is the central storage area that the CPU accesses directly to read and write data. It is faster than secondary memory and is volatile, meaning it loses its data when the power is turned off. RAM is critical for storing the operating system, applications, and the current working set of data.

6.1.1. Demonstration of Primary Memory (RAM)

To observe the usage of primary memory, you can use commands that display the amount of RAM in use, free, and the total available.

free -h  # Displays the amount of free and used memory in the system.
free -m  # Displays RAM usage in megabytes.
vmstat -s  # Shows various VM statistics including memory usage.
top -n 1 | grep 'Mem'  # Shows a snapshot of current RAM usage.

6.2. Secondary Memory

Secondary memory refers to storage devices like hard drives, SSDs (Solid State Drives), and external drives. Unlike primary memory, secondary memory is non-volatile, meaning it retains data even when the computer is turned off. It is used for long-term data storage and is slower compared to primary memory.

6.2.1. Demonstration of Secondary Memory

These commands help you view the usage statistics for your storage devices, providing insight into the capacity and available space.

df -h  # Displays disk space usage for all mounted filesystems.
df -hT  # Displays disk space usage in human-readable format with file system types.
df -i  # Displays the number of used and available inodes on the file system.
lsblk # Lists information about all available or the specified block devices.
lsblk -f  # Lists all block devices along with file system information.
sudo hdparm -I /dev/sda  # Displays detailed information about the primary disk (replace /dev/sda if necessary).

6.3. Virtual Memory

Virtual memory is a memory management capability of an operating system that uses both hardware and software to allow a computer to compensate for physical memory shortages, by temporarily transferring data from random access memory (RAM) to disk storage. This process is seamless and allows applications to use more memory than is physically available on the system.

6.3.1. Demonstration of Virtual Memory

To manage and view details about virtual memory, you can use commands to check the swap usage and configuration.

swapon --show  # Shows swap size and location.
cat /proc/sys/vm/swappiness  # Displays the kernel's preference for swap space usage.
cat /proc/swaps  # Shows swap usage statistics and priority.
sysctl vm.swappiness  # Displays the swappiness value of the kernel.
sudo sysctl vm.swappiness=10  # Sets the swappiness parameter to 10 to reduce swap usage, assuming there's adequate RAM.
echo 10 | sudo tee /proc/sys/vm/swappiness  # Sets the swappiness value to 10 to prioritize RAM over swap.
6.3.2. Configuring Virtual Memory

Virtual memory can be configured by adjusting the swap space, which is the area on a hard drive that is reserved for use as virtual memory. You can increase or decrease the swap space based on your needs.

sudo fallocate -l 1G /swapfile  # Creates a 1GB swap file.
sudo mkswap /swapfile  # Sets up the file as swap space.
sudo swapon /swapfile  # Enables the swap file.
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab  # Makes the swap file permanent.
6.3.3. Monitoring Virtual Memory Usage

To ensure optimal performance, monitoring virtual memory usage is crucial. Tools such as vmstat provide real-time monitoring of processes, memory, paging, block IO, traps, and CPU activity.

vmstat 1 5  # Reports virtual memory statistics every second for five seconds.

Primary Memory (RAM)

  • 1. Volatility: Volatile (loses data on power-off).
  • 2. Speed: Fastest among all memory types.
  • 3. Cost: More expensive per unit of data stored.
  • 4. Capacity: Smaller capacity compared to secondary memory.
  • 5. Usage: Directly accessed by the CPU for running applications.
  • 6. Type: Consists mainly of semiconductors.
  • 7. Power Dependency: Needs constant power to retain data.
  • 8. Physical Form: Chips installed in memory slots.
  • 9. Accessibility: Any part can be reached in the same amount of time (random access).
  • 10. Example Devices: RAM sticks such as DDR4.

Secondary Memory

  • 1. Volatility: Non-volatile (retains data without power).
  • 2. Speed: Slower than primary memory.
  • 3. Cost: Cheaper per unit of data stored.
  • 4. Capacity: Higher capacity; suitable for long-term storage.
  • 5. Usage: Used for permanent data storage and backups.
  • 6. Type: Includes magnetic and optical disks.
  • 7. Power Dependency: Can retain data without power.
  • 8. Physical Form: External and internal hard drives, SSDs, CDs.
  • 9. Accessibility: Slower access time; sequential access for some types like magnetic tapes.
  • 10. Example Devices: Hard disk drives, SSDs, USB flash drives.

Virtual Memory

  • 1. Volatility: Depends on the underlying physical memory (typically non-volatile).
  • 2. Speed: Slower than primary memory, as it uses secondary storage.
  • 3. Cost: Utilizes existing secondary storage, so no extra cost.
  • 4. Capacity: Can be configured to be very large.
  • 5. Usage: Extends available memory resources by using disk space.
  • 6. Type: A management approach rather than physical memory.
  • 7. Power Dependency: Based on secondary storage's non-volatility.
  • 8. Physical Form: No physical form; it's a segment of a hard drive.
  • 9. Accessibility: Managed by the operating system’s memory manager.
  • 10. Example Devices: Implemented using hard drive space.