Skip to content

Instantly share code, notes, and snippets.

@Hello1024
Created March 3, 2021 18:26
Show Gist options
  • Save Hello1024/4902ff3bdba32a219adb4caa3563c23a to your computer and use it in GitHub Desktop.
Save Hello1024/4902ff3bdba32a219adb4caa3563c23a to your computer and use it in GitHub Desktop.
Quick script to find out how much RAM is being used inefficiently or wasted.
#!/bin/env python3
# Requires https://github.com/Hello1024/devmem-full-access
# Requires pip install
from bloom_filter import BloomFilter #
import re
import hashlib
def allRamPages():
with open('/proc/iomem', 'r') as iomemfile:
for iomemline in iomemfile:
# iomemline looks like "ca30c000-cd820fff : System RAM"
x = re.search(r"^([0-9a-f]*)-([0-9a-f]*) : System RAM$", iomemline)
if not x: continue
low = int(x.group(1), 16)
high = int(x.group(2), 16)
low = low
while low < high:
yield low
low = low + 4096
devmem = open('/dev/mem', 'rb')
bloom = BloomFilter(max_elements=2**34/2**12, error_rate=0.001)
same = 0
notsame = 0
fail = 0
zerocount=0
zerohash = hashlib.md5(b'\x00'*4096).digest()
for pagenum in allRamPages():
try:
devmem.seek(pagenum)
data = devmem.read(4096)
data = hashlib.md5(data).digest()
if (data==zerohash):
zerocount=zerocount+1
continue
if data in bloom:
same = same+1
continue
notsame = notsame+1
bloom.add(data)
except PermissionError:
fail = fail+1
if (pagenum%(1024*4096)==0): print(f'{pagenum:02X} done. {zerocount} zero, {same} same, {notsame} not the same, {fail} failed.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment