-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory Fragmentation Analyzer.py
More file actions
97 lines (75 loc) · 3.63 KB
/
Memory Fragmentation Analyzer.py
File metadata and controls
97 lines (75 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import tkinter as tk
from tkinter import messagebox
import matplotlib.pyplot as plt
class MemoryFragmentationAnalyzer:
def __init__(self, root):
self.root = root
self.root.title("Memory Fragmentation Analyzer")
tk.Label(root, text="Enter Memory Blocks (comma separated sizes):").pack()
self.blocks_entry = tk.Entry(root, width=50)
self.blocks_entry.pack()
tk.Label(root, text="Enter Process Sizes (comma separated):").pack()
self.process_entry = tk.Entry(root, width=50)
self.process_entry.pack()
tk.Label(root, text="Allocation Strategy:").pack()
self.strategy_var = tk.StringVar(value="First Fit")
tk.OptionMenu(root, self.strategy_var,
"First Fit", "Best Fit", "Worst Fit").pack()
tk.Button(root, text="Analyze", command=self.analyze).pack(pady=10)
def analyze(self):
try:
blocks = list(map(int, self.blocks_entry.get().split(",")))
processes = list(map(int, self.process_entry.get().split(",")))
strategy = self.strategy_var.get()
allocation = [-1] * len(processes)
remaining_blocks = blocks.copy()
for i, process in enumerate(processes):
index = -1
if strategy == "First Fit":
for j in range(len(remaining_blocks)):
if remaining_blocks[j] >= process:
index = j
break
elif strategy == "Best Fit":
best_size = float('inf')
for j in range(len(remaining_blocks)):
if remaining_blocks[j] >= process and remaining_blocks[j] < best_size:
best_size = remaining_blocks[j]
index = j
elif strategy == "Worst Fit":
worst_size = -1
for j in range(len(remaining_blocks)):
if remaining_blocks[j] >= process and remaining_blocks[j] > worst_size:
worst_size = remaining_blocks[j]
index = j
if index != -1:
allocation[i] = index
remaining_blocks[index] -= process
internal_frag = sum(
remaining_blocks[i] for i in range(len(blocks))
)
total_memory = sum(blocks)
used_memory = sum(processes[i] for i in range(len(processes)) if allocation[i] != -1)
utilization = (used_memory / total_memory) * 100
result = f"Strategy: {strategy}\n"
result += f"Total Memory: {total_memory}\n"
result += f"Used Memory: {used_memory}\n"
result += f"Memory Utilization: {utilization:.2f}%\n"
result += f"Internal Fragmentation: {internal_frag}"
messagebox.showinfo("Analysis Result", result)
self.plot_memory(blocks, remaining_blocks)
except Exception as e:
messagebox.showerror("Error", str(e))
def plot_memory(self, original, remaining):
plt.figure()
plt.bar(range(len(original)), original, label="Original")
plt.bar(range(len(remaining)), remaining, bottom=0, alpha=0.6, label="Remaining")
plt.title("Memory Blocks Visualization")
plt.xlabel("Block Index")
plt.ylabel("Memory Size")
plt.legend()
plt.show()
if __name__ == "__main__":
root = tk.Tk()
app = MemoryFragmentationAnalyzer(root)
root.mainloop()