<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[【FAQ】全志V853芯片 Tina Linux下网络ADB内存泄露如何修复？]]></title><description><![CDATA[<h2>1.主题</h2>
<p dir="auto">Tina Linux 网络ADB内存泄露修复</p>
<h2>2.问题背景</h2>
<p dir="auto">硬件：V853<br />
软件：Tina4.0 Linux-4.9<br />
背景：使用网络adb时，反复connect disconnect，会发生内存泄露的问题。</p>
<h2>3.问题描述</h2>
<h3>3.1复现步骤</h3>
<p dir="auto">1、首先使能网络ADB功能。</p>
<pre><code>killall adbd
export ADB_TRANSPORT_PORT=5555
/bin/adbd -D &gt; /dev/null &amp;
</code></pre>
<p dir="auto">2、连接无线网络，保持小机与测试电脑处于同一个局域网中。<br />
由于每款产品连接无线网络的命令不大一致，因此此处就不对网络连接进行赘述了。</p>
<p dir="auto">3、编写bat脚本，进行压测。</p>
<pre><code>:loop
adb disconnect
adb connect 192.168.0.101
adb disconnect
timeout /t 3
goto :loop
pause
</code></pre>
<p dir="auto">4、使用内存泄露工具查看内存是否泄露。</p>
<h3>3.2具体表现</h3>
<p dir="auto">使用内部一款内存泄露工具观察adbd工具内存占用情况：</p>
<pre><code>行 2499: 632        504        74         /bin/adbd
行 2539: 632        504        74         /bin/adbd
行 2581: 632        504        74         /bin/adbd
行 2629: 632        504        74         /bin/adbd
行 2672: 632        504        74         /bin/adbd
行 2711: 632        504        74         /bin/adbd
行 2753: 632        504        74         /bin/adbd
行 2796: 632        504        74         /bin/adbd
行 2841: 632        504        74         /bin/adbd
行 2881: 632        504        74         /bin/adbd
行 2921: 632        504        74         /bin/adbd
行 2961: 632        504        74         /bin/adbd
行 3008: 632        504        74         /bin/adbd
行 3047: 632        504        74         /bin/adbd
行 3091: 632        504        74         /bin/adbd
行 3131: 632        504        74         /bin/adbd
行 3181: 632        504        74         /bin/adbd
行 3221: 632        504        74         /bin/adbd
行 3265: 632        504        74         /bin/adbd
行 3309: 632        504        74         /bin/adbd
行 3349: 632        504        74         /bin/adbd
行 3399: 632        504        74         /bin/adbd
行 3439: 632        504        74         /bin/adbd
行 3483: 632        504        74         /bin/adbd
行 3523: 632        504        74         /bin/adbd
行 3574: 632        504        74         /bin/adbd
行 3614: 632        504        74         /bin/adbd
行 3654: 632        504        74         /bin/adbd
行 3694: 632        636        206        /bin/adbd
行 3740: 632        636        206        /bin/adbd
行 3780: 632        636        206        /bin/adbd
行 3820: 632        636        206        /bin/adbd
行 3860: 632        636        206        /bin/adbd
行 3900: 632        636        206        /bin/adbd
行 3946: 632        636        206        /bin/adbd
行 3986: 632        636        206        /bin/adbd
行 4026: 632        636        206        /bin/adbd
行 4066: 632        636        206        /bin/adbd
行 4117: 632        636        202        /bin/adbd
行 4159: 632        636        206        /bin/adbd
行 4203: 632        636        202        /bin/adbd
行 4245: 632        636        204        /bin/adbd
行 4292: 632        636        206        /bin/adbd
行 4333: 632        668        234        /bin/adbd
</code></pre>
<p dir="auto">发现确实存在内存泄露的情况。</p>
<h2>4.问题分析</h2>
<p dir="auto">使能网络adb时，将命令改成：</p>
<pre><code>killall adbd
export ADB_TRANSPORT_PORT=5555
/bin/adbd -D &amp;
</code></pre>
<p dir="auto">将adbd的log信息输出到控制台上，观察adbd的运行情况。</p>
<p dir="auto">根据log信息以及对比源码能够发现，在调用transport.c的transport_registration_func()函数时，当读到的action等于0，即退出adb时，就会free掉一些之前malloc的指针。</p>
<p dir="auto"><img src="/assets/uploads/files/1678068521926-df685f4937b043c89a794bccd42fc02c.jfif" alt="ADB1.jfif" class=" img-responsive img-markdown" width="undefined" height="undefined" /></p>
<h2>5.根本原因</h2>
<p dir="auto">通过在函数transport_registration_func()的free动作前后添加打印能够发现，在adb disconnect时，并没有调用到free的动作。</p>
<p dir="auto">再在register_socket_transport()中calloc结构体atransport前后添加打印发现，每次adb connect时，都会重新调用register_socket_transport()去注册传输所需要的资源，并且会重新申请一篇内存。</p>
<p dir="auto">那么就根据前后，就能知道在disconnect时有一篇内存没有释放，而后重新connect时又新申请内存，导致了内存的泄露。</p>
<p dir="auto">对比有线adb，在连接时会申请结构体atransport的内存，在拔出usb线时也会free掉对应的内存。</p>
<p dir="auto">再看回来函数transport_registration_func()，</p>
<p dir="auto"><img src="/assets/uploads/files/1678068545290-9e8b1713f0fc40959238bcf1356ae555.jfif" alt="ADB2.jfif" class=" img-responsive img-markdown" width="undefined" height="undefined" /></p>
<p dir="auto">在初始化时，这里注册了一个异步事情回调函数，当接收到事件的时候，就会调用transport_registration_func()去处理事件。对于拔出USB线来说，就会对gadget进行disconnect，然后composite gadget就会通过uevent通知应用层已经断开连接了，于是adbd就能够获取到事件从而去断开连接，释放资源。</p>
<p dir="auto">而对于网络adb来说，暂无这种机制。所以就需要依靠在output_thread中，在通信失败后主动触发断连的操作。</p>
<p dir="auto">在部分SDK中，存在这么一个补丁：</p>
<pre><code>diff --git a/utils/adb/src/transport.c b/utils/adb/src/transport.c
index 9fd6cc2..97a438b 100755
--- a/utils/adb/src/transport.c
+++ b/utils/adb/src/transport.c
@@ -285,7 +285,6 @@ static void *output_thread(void *_t)
         } else {
             D("%s: remote read failed for transport\n", t-&gt;serial);
             put_apacket(p);
-            break;
         }
     }
</code></pre>
<p dir="auto">补丁的作用时，当通信失败时，就会断连、释放资源。这个补丁引入是为了修复USB ADB在通信不佳的情况下，触发了通信失败然后导致了USB重新枚举。<br />
但是引入该补丁后，在使用网络ADB时，通信失败也不会被断连了，因此也造成了内存泄露的问题。</p>
<h2>6.解决办法</h2>
<p dir="auto">基于上述情况，在output_thread中加入限制的条件。在USB ADB时，取消break，让其一直在循环当中。对于网络ADB来说，则是执行break，当发生断连时，能够及时将资源释放掉。</p>
<p dir="auto">修复的补丁如附件所示。</p>
<p dir="auto"><a href="/assets/uploads/files/1678068563472-0001-adbd-only-transport_local-can-disconnect-in-outputth.patch">0001-adbd-only-transport_local-can-disconnect-in-outputth.patch</a></p>
]]></description><link>https://bbs.aw-ol.com/topic/3056/faq-全志v853芯片-tina-linux下网络adb内存泄露如何修复</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 19:07:47 GMT</lastBuildDate><atom:link href="https://bbs.aw-ol.com/topic/3056.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 06 Mar 2023 02:11:27 GMT</pubDate><ttl>60</ttl></channel></rss>