memsize_darwin.go 419 B

12345678910111213141516171819202122232425
  1. package main
  2. import (
  3. "errors"
  4. "os/exec"
  5. "strconv"
  6. "strings"
  7. )
  8. func memorySize() (uint64, error) {
  9. cmd := exec.Command("sysctl", "hw.memsize")
  10. out, err := cmd.Output()
  11. if err != nil {
  12. return 0, err
  13. }
  14. fs := strings.Fields(string(out))
  15. if len(fs) != 2 {
  16. return 0, errors.New("sysctl parse error")
  17. }
  18. bytes, err := strconv.ParseUint(fs[1], 10, 64)
  19. if err != nil {
  20. return 0, err
  21. }
  22. return bytes, nil
  23. }