嵌入式系統內存往往是有限制的(成本考慮),因此需要儘量支持更多的功能,同時儘量減少使用的內存。
一種方法是把需要支持的功能做成共享庫的形式,需要這個功能的時候加載共享庫,不需要的時候卸載共享庫,從而把內存釋放出來。
下面舉個例子說明。
$ cat 1.c
#include <stdio.h>
static int g_array[1024 * 1024 * 5];
int *g_p = 0;
__attribute__((constructor)) void init1(void) {
printf("library lib1.so loaded!\n");
g_p = (int *)malloc(sizeof(int) * 1024 * 1024 * 5);
memset(g_p, 0xff, sizeof(int) *1024 * 1024*5);
memset(g_array, 0xff, sizeof(g_array));
}
__attribute__((destructor)) void fini1(void) {
printf("library lib1.so unloaded!\n");
free(g_p);
}
char * str1 = "this is a long text string for testing";
char * str2 = "this is a long text string for playing";
char * str3 = "this is a long text string for eating";
char * str4 = "this is a long text string for laughing";
char * str5 = "this is a long text string for working";
void foo(void)
{
printf("foo\n");
printf("%s\n", str1);
printf("%s\n", str2);
printf("%s\n", str3);
printf("%s\n", str4);
printf("%s\n", str5);
}
$ gcc 1.c -shared -fpic -g -o lib1.so
$ cat test.c
#include <dlfcn.h>
typedef void (*foo_func)(void );
int main(void)
{
while( 1)
{
void *handle = dlopen("lib1.so", RTLD_LAZY);
if(handle == 0)
{
printf("fail to open lib\n");
return -1;
}
foo_func ff = (foo_func)dlsym(handle, "foo");
if( ff == 0)
{
printf("fail to find ff\n");
return -1;
}
ff();
dlclose(handle);
}
return 0;
}
$ 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
內存又恢復到上次的數值了。