|
16 | 16 | import win32com.test.util
|
17 | 17 | import win32timezone
|
18 | 18 | import winerror
|
| 19 | +from win32api import CloseHandle, GetCurrentProcessId, OpenProcess |
19 | 20 | from win32com.client import (
|
20 | 21 | VARIANT,
|
21 | 22 | CastTo,
|
|
24 | 25 | constants,
|
25 | 26 | register_record_class,
|
26 | 27 | )
|
| 28 | +from win32process import GetProcessMemoryInfo |
27 | 29 |
|
28 | 30 | importMsg = "**** PyCOMTest is not installed ***\n PyCOMTest is a Python test specific COM client and server.\n It is likely this server is not installed on this machine\n To install the server, you must get the win32com sources\n and build it using MS Visual C++"
|
29 | 31 |
|
@@ -137,6 +139,16 @@ def TestConstant(constName, pyConst):
|
137 | 139 | ), f"Constant value wrong for {constName} - got {comConst}, wanted {pyConst}"
|
138 | 140 |
|
139 | 141 |
|
| 142 | +def GetMemoryUsage(): |
| 143 | + pid = GetCurrentProcessId() |
| 144 | + PROCESS_QUERY_INFORMATION = 0x0400 |
| 145 | + PROCESS_VM_READ = 0x0010 |
| 146 | + hprocess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, False, pid) |
| 147 | + mem_info = GetProcessMemoryInfo(hprocess) |
| 148 | + CloseHandle(hprocess) |
| 149 | + return mem_info["WorkingSetSize"] |
| 150 | + |
| 151 | + |
140 | 152 | # Simple handler class. This demo only fires one event.
|
141 | 153 | class RandomEventHandler:
|
142 | 154 | def _Init(self):
|
@@ -601,6 +613,13 @@ def TestGenerated():
|
601 | 613 | TestApplyResult(o.SetLongLongSafeArray, (ll,), len(ll))
|
602 | 614 | TestApplyResult(o.SetULongLongSafeArray, (ll,), len(ll))
|
603 | 615 |
|
| 616 | + # check freeing of safe arrays |
| 617 | + mem_before = GetMemoryUsage() |
| 618 | + o.GetByteArray(50 * 1024 * 1024) |
| 619 | + mem_after = GetMemoryUsage() |
| 620 | + delta = mem_after - mem_before |
| 621 | + assert delta < 1024 * 1024, f"Memory not freed - delta {delta / (1024 * 1024)} MB" |
| 622 | + |
604 | 623 | # Tell the server to do what it does!
|
605 | 624 | TestApplyResult(o.Test2, (constants.Attr2,), constants.Attr2)
|
606 | 625 | TestApplyResult(o.Test3, (constants.Attr2,), constants.Attr2)
|
|
0 commit comments