频繁使用xpath中的方法会导致手机端uiautomator jar包起的服务出现内存泄漏

Author: bozai414Created Mar 13, 2026Updated Mar 13, 2026

例如xpath里面的click方法,会去调用
def click(self, timeout=None): """find element and perform click""" try: el = self.get(timeout=timeout) el.click() except XPathElementNotFoundError: if not self._fallback: raise logger.info("element not found, run fallback") return inject_call(self._fallback, d=self._d) 最终会调用 _do_dump_hierarchy函数 dump_hierarchy会使用socket进行通讯,传输当前设备的UI DOM树,导致每次xpath操作socket传递大量字节数据,如果使用while True函数:不断调用d.xpath(xpath字符串).exists或者click方法,长时间会导致设备端出现内存泄漏,设备端u2服务崩掉导致通信无法响应; 希望作者大大,可以考虑在手机和电脑端通信优化下内存增长这块的问题; 还有一个是d(text="字符串").click(),为什么要和xpath的click方法是分开的? 因为d(text="字符串").click(),没有调用_do_dump_hierarchy,直接获取组件的objInfo,通信的内容仅仅是组件的bounds信息;似乎从UI DOM树中获取组件信息是在java端进行的,但是xpath中的方法,是在python端解析组件的信息,这一点比较困惑; elif u2dev.xpath('//*[@text="Camera error"]').exists: File "D:\python\lib\site-packages\\uiautomator2\\xpath.py", line 434, in exists return len(self.all()) > 0 File "D:\python\lib\site-packages\\uiautomator2\\xpath.py", line 427, in all source = self._get_page_source() File "D:\python\lib\site-packages\\uiautomator2\\xpath.py", line 421, in _get_page_source return self._parent.get_page_source() File "D:\python\lib\site-packages\\uiautomator2\\xpath.py", line 199, in get_page_source return PageSource.parse(self.d.dump_hierarchy()) File "D:\python\lib\site-packages\\uiautomator2_init.py", line 107, in dump_hierarchy content = self._do_dump_hierarchy(compressed, max_depth) File "D:\python\lib\site-packages\decorator.py", line 235, in fun return caller(func, *(extras + args), **kw) File "D:\python\lib\site-packages\retry\api.py", line 73, in retry_decorator return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter, File "D:\python\lib\site-packages\retry\api.py", line 33, in _retry_internal return f() File "D:\python\lib\site-packages\\uiautomator2_init.py", line 122, in _do_dump_hierarchy content = self.jsonrpc.dumpWindowHierarchy(compressed, max_depth) File "D:\python\lib\site-packages\\uiautomator2\base.py", line 158, in call return self.server.jsonrpc_call(self.method, params, http_timeout) File "D:\python\lib\site-packages\\uiautomator2\core.py", line 311, in jsonrpc_call return _jsonrpc_call(self._dev, self._device_server_port, method, params, timeout, self._debug) File "D:\python\lib\site-packages\\uiautomator2\core.py", line 173, in _jsonrpc_call r = _http_request(dev, device_port, "POST", "/jsonrpc/0", payload, timeout=timeout, print_request=print_request) File "D:\python\lib\site-packages\\uiautomator2\core.py", line 140, in _http_request _response = conn.getresponse() File "D:\python\lib\http\client.py", line 1377, in getresponse response.begin() File "D:\python\lib\http\client.py", line 320, in begin version, status, reason = self._read_status() File "D:\python\lib\http\client.py", line 289, in _read_status raise RemoteDisconnected("Remote end closed connection without" http.client.RemoteDisconnected: Remote end closed connection without response