fixed bugs in linux_ram:getLinuxRamInfo

This commit is contained in:
lin committed 2026-10-05 14:53:31 +02:00
1 parent ea50417715
commit ad2a44d36d
1 file changed
+14 -9
+14 -9
View File
@@ -18,35 +18,40 @@ func getLinuxRamInfo() (MemoryInformation, error) {
if err != nil { if err != nil {
return MemoryInformation{}, fmt.Errorf("Could not find /proc/meminfo. Are you sure you are on linux?") return MemoryInformation{}, fmt.Errorf("Could not find /proc/meminfo. Are you sure you are on linux?")
} }
defer memoryFile.Close()
fileReader := bufio.NewScanner(memoryFile) fileReader := bufio.NewScanner(memoryFile)
var TotalMemory uint64 var TotalMemory uint64
var AvailMemory uint64 var AvailMemory uint64
for fileReader.Scan() { for fileReader.Scan() {
if fileReader.Err() != nil {
return MemoryInformation{}, fmt.Errorf("There was an error while reading /proc/meninfo. Error: %v", fileReader.Err())
}
line := fileReader.Text() line := fileReader.Text()
if strings.Contains(line, "MemTotal") { if strings.HasPrefix(line, "MemTotal:") {
TotalMemory, err = strconv.ParseUint(numberRegex.FindString(line), 10, 64) TotalMemory, err = strconv.ParseUint(numberRegex.FindString(line), 10, 64)
if err != nil { if err != nil {
return MemoryInformation{}, fmt.Errorf("Could not parse total memory from /proc/meminfo") return MemoryInformation{}, fmt.Errorf("Could not parse total memory from /proc/meminfo")
} }
} else if strings.Contains(line, "MemAvailable") { } else if strings.HasPrefix(line, "MemAvailable:") {
AvailMemory, err = strconv.ParseUint(numberRegex.FindString(line), 10, 64) AvailMemory, err = strconv.ParseUint(numberRegex.FindString(line), 10, 64)
if err != nil { if err != nil {
return MemoryInformation{}, fmt.Errorf("Could not parse available memory from /proc/meminfo") return MemoryInformation{}, fmt.Errorf("Could not parse available memory from /proc/meminfo")
} }
} }
if TotalMemory > 0 && AvailMemory > 0 {
break
}
}
if fileReader.Err() != nil {
return MemoryInformation{}, fmt.Errorf("There was an error while reading /proc/meninfo. Error: %v", fileReader.Err())
} }
return MemoryInformation{ return MemoryInformation{
SystemMemory: TotalMemory, SystemMemory: TotalMemory * 1024,
AvailableMemory: AvailMemory, AvailableMemory: AvailMemory * 1024,
}, nil }, nil
} }