Advertisement
Guest User

Untitled

a guest
Sep 25th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 1.60 KB | None | 0 0
  1. import winlean
  2.  
  3. type
  4.   PROCESS_MEMORY_COUNTERS = object
  5.     cb: int32
  6.     PageFaultCount: int32
  7.     PeakWorkingSetSize: uint
  8.     WorkingSetSize: uint
  9.     QuotaPeakPagedPoolUsage: uint
  10.     QuotaPagedPoolUsage: uint
  11.     QuotaPeakNonPagedPoolUsage: uint
  12.     QuotaNonPagedPoolUsage: uint
  13.     PagefileUsage: uint
  14.     PeakPagefileUsage: uint
  15.  
  16. # Only works on Windows - https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getprocessmemoryinfo
  17. proc getProcessMemoryInfo*(process: Handle, ppsmemCounters: var PROCESS_MEMORY_COUNTERS, cb: DWORD): WINBOOL {.stdcall, dynlib: "Psapi", importc: "GetProcessMemoryInfo".}
  18.  
  19. proc fillSeq(y: int, x: var seq[seq[int8]]) =
  20.   var
  21.     w: seq[int8] = @[1i8, 2, 3, 4, 5, 6]
  22.     z = 1
  23.  
  24.   while z <= y:
  25.     add(x, w)
  26.     inc(z)
  27.  
  28. var
  29.   c = getCurrentProcess()
  30.   p: PROCESS_MEMORY_COUNTERS
  31.   cap = 1310720
  32.   ram1, ram2, ram3: uint
  33.  
  34.  
  35. echo "Ram usage by process before creating sequence"
  36. discard getProcessMemoryInfo(c, p, sizeof(p).DWORD)
  37. ram1 = p.WorkingSetSize
  38. echo ram1, " bytes\n"
  39.  
  40. var x = newSeqOfCap[seq[int8]](cap) # sizeof(seq[seq[int8]]) = 8 -> 8 * 1310720 = 10485760
  41.  
  42. echo "Ram usage by process after creating sequence with length 0 and capacity ", cap
  43. discard getProcessMemoryInfo(c, p, sizeof(p).DWORD)
  44. ram2 = p.WorkingSetSize
  45. echo ram2, " bytes\n"
  46.  
  47. fillSeq(cap, x)
  48.  
  49. echo "Ram usage by process after filling sequence"
  50. discard getProcessMemoryInfo(c, p, sizeof(p).DWORD)
  51. ram3 = p.WorkingSetSize
  52. echo ram3, " bytes\n"
  53.  
  54. echo len(x), " len seq\n"
  55.  
  56. echo "ram2 - ram1 = ", ram2 - ram1
  57. echo "ram3 - (ram1 + ram2) = ", ram3 - (ram1 + ram2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement