嵌入式系統內存往往是有限制的(成本考慮),因此需要盡量支持更多的功能,同時盡量減少使用的內存。
一種方法是把需要支持的功能做成共享庫的形式,需要這個功能的時候加載共享庫,不需要的時候卸載共享庫,從而把內存釋放出來。
下面舉個例子說明。
$ gcc 1.c -shared -fpic -g -o lib1.so
$ gcc test.c -g -o test -ldl -l1 -L.
打開gdb,執行 gdb test,
在dlopen執行前,程序內存使用:
$ ps -p $(pidof test) -o %cpu,%mem,cmd,rss,rsz,vsz,trs
%CPU %MEM CMD RSS RSZ VSZ TRS
0.0 0.0 /home/charles/tmp/test 696 696 2084 1
dlopen()執行后:
$ ps -p $(pidof test) -o %cpu,%mem,cmd,rss,rsz,vsz,trs
%CPU %MEM CMD RSS RSZ VSZ TRS
0.0 1.0 /home/charles/tmp/test 42000 42000 43192 1
dlclose()后:
ps -p $(pidof test) -o %cpu,%mem,cmd,rss,rsz,vsz,trs
%CPU %MEM CMD RSS RSZ VSZ TRS
0.0 0.0 /home/charles/tmp/test 1092 1092 2216 1
可以看到,大部分用到的內存都釋放掉了。
然后如果執行下一次循環, dlopen()后:
$ ps -p $(pidof test) -o %cpu,%mem,cmd,rss,rsz,vsz,trs
%CPU %MEM CMD RSS RSZ VSZ TRS
0.0 1.0 /home/charles/tmp/test 42008 42008 43192 1
執行 dlclose()后:
$ ps -p $(pidof test) -o %cpu,%mem,cmd,rss,rsz,vsz,trs
%CPU %MEM CMD RSS RSZ VSZ TRS
0.0 0.0 /home/charles/tmp/test 1092 1092 2216
內存又恢復到上次的數值了。
-
嵌入式
+關注
關注
5092文章
19177瀏覽量
307653 -
內存
+關注
關注
8文章
3055瀏覽量
74327
發布評論請先 登錄
相關推薦
Linux嵌入式系統初學者的常見問題解決方法大全
優秀的嵌入式程序有什么特點?
單片機開發之節省內存大法
嵌入式linux+io+優化,嵌入式Linux系統內存優化使用方法研究
![<b class='flag-5'>嵌入式</b>linux+io+優化,<b class='flag-5'>嵌入式</b>Linux<b class='flag-5'>系統</b><b class='flag-5'>內存</b>優化使用<b class='flag-5'>方法</b>研究](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
如何通過設計模式來節省內存
![如何通過設計模式來<b class='flag-5'>節省內存</b>](https://file1.elecfans.com/web2/M00/A7/5B/wKgaomUjZgCAWn1RAAC_S1G6f2A033.jpg)
評論