Memory.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. export default class Memory {
  2. static used() {
  3. const runtime = Runtime.getRuntime()
  4. return runtime.totalMemory() - runtime.freeMemory()
  5. }
  6. static format(mem) {
  7. if (mem < 2 * Memory.KB) return mem + ' bytes'
  8. if (mem < 2 * Memory.MB) return Memory.round(mem / Memory.KB) + ' KB'
  9. if (mem < 2 * Memory.GB) return Memory.round(mem / Memory.MB) + ' MB'
  10. return Memory.round(mem / Memory.GB) + ' GB'
  11. }
  12. static freeString() {
  13. return Memory.format(Memory.free())
  14. }
  15. static total() {
  16. const runtime = Runtime.getRuntime()
  17. return runtime.totalMemory()
  18. }
  19. static usedTotalString() {
  20. return 'Used: ' + Memory.usedString() + ' Total: ' + Memory.totalString()
  21. }
  22. static usedString() {
  23. return Memory.format(Memory.used())
  24. }
  25. static allString() {
  26. return 'Used: ' + Memory.usedString() + ' Free: ' + Memory.freeString() + ' Total: ' + Memory.totalString()
  27. }
  28. static round(d) {
  29. return Math.ceil(d * 100) / 100
  30. }
  31. static totalString() {
  32. return Memory.format(Memory.total())
  33. }
  34. static free() {
  35. const runtime = Runtime.getRuntime()
  36. return runtime.freeMemory()
  37. }
  38. }
  39. Memory.KB = 1024
  40. Memory.MB = 1048576
  41. Memory.GB = 1073741824