2008-08-21 03:24:22     How to decrease the memory occupied by a program?

Document created by Aaronwu Employee on Aug 7, 2013
Version 1Show Document
  • View in full screen mode

2008-08-21 03:24:22     How to decrease the memory occupied by a program?

Aaron Zhong (CHINA)

Message: 60788   

 

Hi all,

 

I find my program occupied much more memory. When the program run into main function,

the free memory decreased about 7MB. How to find out where these memory gone? And

how to optimize the memory use, by configuring kernel with CONFIG_NP2 and CONFIG_SLOB?

The CONFIG_NP2 option seemed not work. When I request 1MB memory, the system still

give me 2MB. I find the result by using "cat /proc/meminfo". This is my test program.

Thanks in advance for any help.

 

int main(void)

{

unsigned char *p;

p = malloc(1024*1024);

return 0;

}

QuoteReplyEditDelete

 

 

2008-08-21 03:55:05     Re: How to decrease the memory occupied by a program?

Yi Li (CHINA)

Message: 60791   

 

CONFIG_NP2 and CONFIG_SLOB cannot help to decrease the memory used by application. If you write the application, I think you would know how much memory your application consumed. And "cat /proc/maps" can provide information on data section, text section and stack size.

QuoteReplyEditDelete

 

 

2008-08-21 04:11:12     Re: How to decrease the memory occupied by a program?

Aaron Zhong (CHINA)

Message: 60792   

 

This is its result. Could you give me some guide on how to analyze it?

 

root:~> cat /proc/maps

002dc000-002dd000 rw-p 00000000 00:00 0

002f0000-00310000 rwxp 00000000 00:00 0

00314000-00315000 rw-p 00000000 00:00 0

0031c000-0031e000 rw-p 00000000 00:00 0

003d4000-003d5000 rw-p 00000000 00:00 0

003d8000-003ee000 rw-p 00000000 00:00 0

003fe000-003ff000 rw-p 00000000 00:00 0

00408000-0040a000 rw-p 00000000 00:00 0

00414000-00415000 rw-p 00000000 00:00 0

00428000-00438000 rwxp 00000000 00:00 0

00456000-00457000 rw-p 00000000 00:00 0

00458000-0045a000 rw-p 00000000 00:00 0

00460000-00461000 rw-p 00000000 00:00 0

00482000-00502000 rwxp 00000000 00:00 0

0051b000-0051c000 rw-p 00000000 00:00 0

0051c000-00520000 rwxp 00000000 00:00 0

00522000-00532000 rwxp 00000000 00:00 0

0053a000-0053b000 rw-p 00000000 00:00 0

0053c000-005bc000 rwxp 00000000 00:00 0

005bc000-005d2000 rw-p 00000000 00:00 0

005dc000-005de000 rw-p 00000000 00:00 0

00624000-00725000 rw-p 00000000 00:00 0

00824000-00834000 rwxp 00000000 00:00 0

00834000-0084a000 rw-p 00000000 00:00 0

00854000-008d4000 rwxp 00000000 00:00 0

00c5a000-00cda000 rwxp 00000000 00:00 0

 

QuoteReplyEditDelete

 

 

2008-08-21 05:46:09     Re: How to decrease the memory occupied by a program?

Yi Li (CHINA)

Message: 60817   

 

I think you are using FLAT format. There is some problem with this format, and you cannot see the file names. If you are using FDPID format, you may see output like this:

 

root:/> cat /proc/maps                                                        

00150000-0015b000 r-xs 00000000 00:01 399        /lib/libgcc_s.so.1           

00160000-00172000 rw-p 00000000 00:00 0                                       

00180000-001a0000 rwxp 00000000 00:00 0                                       

001a0000-001c0000 rwxp 00000000 00:00 0                                       

001c0000-001cd000 rw-p 00056000 00:01 100        /bin/busybox                 

001de000-001df000 rw-p 00002000 00:01 228        /bin/inetd                   

001df000-001e0000 rw-p 00000000 00:00 0                                       

001e0000-00200000 rwxp 00000000 00:00 0                                       

00400000-0040d000 rw-p 00056000 00:01 100        /bin/busybox                 

00410000-00412000 rw-p 00000000 00:00 0                                       

00414000-00418000 rw-p 00000000 00:00 0                                       

0041a000-0041b000 rw-p 00000000 00:00 0                                       

0041b000-0041c000 rw-p 00000000 00:00 0                                       

0041e000-00420000 rw-p 00006000 00:01 392        /lib/ld-uClibc-0.9.29.so     

00420000-00440000 rwxp 00000000 00:00 0                                       

00440000-0044d000 rw-p 00056000 00:01 100        /bin/busybox                 

00450000-00451000 rw-p 00000000 00:00 0                                       

00454000-00456000 rw-p 00000000 00:00 0                                       

00458000-0045c000 rw-p 00000000 00:00 0                                       

00460000-00480000 rwxp 00000000 00:00 0                                       

00600000-00657000 r-xs 00000000 00:01 100        /bin/busybox                 

00680000-006bd000 r-xs 00000000 00:01 420        /lib/libuClibc-0.9.29.so     

006c0000-006e0000 rwxp 00000000 00:00 0                                       

006e0000-006e2000 rw-p 00006000 00:01 392        /lib/ld-uClibc-0.9.29.so     

006e2000-006e4000 rw-p 00000000 00:00 0                                       

006e4000-006e7000 r-xs 00000000 00:01 395        /lib/libcrypt-0.9.29.so      

006e8000-006ec000 rw-p 00000000 00:00 0                                       

006ec000-006f0000 rw-p 00000000 00:00 0                                       

006f0000-006f7000 r-xs 00000000 00:01 392        /lib/ld-uClibc-0.9.29.so     

006fd000-006fe000 rw-p 00000000 00:00 0                                       

006fe000-006ff000 rw-p 00000000 00:00 0                                       

006ff000-00700000 rw-p 00000000 00:00 0                                       

00701000-00702000 rw-p 00002000 00:01 229        /bin/init                    

00703000-00704000 rw-p 00000000 00:00 0                                       

032f8000-032fa000 rw-p 00000000 00:00 0                                       

032fc000-032fe000 rw-p 00006000 00:01 392        /lib/ld-uClibc-0.9.29.so     

0335c000-0335f000 r-xs 00000000 00:01 229        /bin/init                    

03360000-0336d000 rw-p 00056000 00:01 100        /bin/busybox                 

03372000-03374000 r-xs 00000000 00:01 96         /bin/watchdogd               

03378000-0337c000 rw-p 00000000 00:00 0                                       

0337c000-0337d000 rw-p 00000000 00:00 0                                       

0337d000-0337e000 rw-p 00000000 00:00 0                                       

0337e000-03380000 rw-p 00000000 00:00 0                                       

03394000-03397000 r-xs 00000000 00:01 228        /bin/inetd                   

03398000-0339a000 rw-p 00006000 00:01 392        /lib/ld-uClibc-0.9.29.so     

0339c000-0339e000 rw-p 00000000 00:00 0                                       

033a0000-033c0000 rwxp 00000000 00:00 0                                       

033c0000-033c1000 rw-p 00000000 00:00 0                                       

033c3000-033c4000 rw-p 00001000 00:01 96         /bin/watchdogd               

033c4000-033c5000 rw-p 00000000 00:00 0                                       

033c5000-033c6000 rw-p 00000000 00:00 0                                       

033c6000-033c8000 rw-p 00006000 00:01 392        /lib/ld-uClibc-0.9.29.so     

033c8000-033ca000 rw-p 00000000 00:00 0                                       

033cc000-033d0000 rw-p 00000000 00:00 0                                       

033d0000-033d2000 rw-p 00006000 00:01 392        /lib/ld-uClibc-0.9.29.so     

033d2000-033d4000 rw-p 00006000 00:01 392        /lib/ld-uClibc-0.9.29.so     

033d4000-033d5000 rw-p 00000000 00:00 0                                       

033d8000-033da000 rw-p 00000000 00:00 0                                       

033da000-033db000 rw-p 00000000 00:00 0                                       

033dc000-033e0000 rw-p 00000000 00:00 0                                       

033e0000-033f6000 rw-p 00000000 00:00 0

 

For the meaning, I don't think there is document in linux/Documentation. You may refer to linux/fs/proc/nommu.c for details.

 

Also for a specific process:

 

root:/> cat /proc/97/maps                                                     

00150000-0015b000 r-xs 00000000 00:01 399        /lib/libgcc_s.so.1           

001de000-001df000 rw-p 00002000 00:01 228        /bin/inetd                   

001df000-001e0000 rw-p 00000000 00:00 0                                       

001e0000-00200000 rwxp 00000000 00:00 0                                       

00680000-006bd000 r-xs 00000000 00:01 420        /lib/libuClibc-0.9.29.so     

006f0000-006f7000 r-xs 00000000 00:01 392        /lib/ld-uClibc-0.9.29.so     

006fd000-006fe000 rw-p 00000000 00:00 0                                       

03378000-0337c000 rw-p 00000000 00:00 0                                       

03394000-03397000 r-xs 00000000 00:01 228        /bin/inetd                   

03398000-0339a000 rw-p 00006000 00:01 392        /lib/ld-uClibc-0.9.29.so     

0339c000-0339e000 rw-p 00000000 00:00 0

 

In fact, /proc/maps and /proc/<pid>/maps is dump of the vma list. For the memory used on kernel side (slab, buddy, page cache), you will have to use /proc/meminfo, /proc/buddyinfo, /proc/slabinfo.

QuoteReplyEditDelete

 

 

2008-08-21 08:52:23     Re: How to decrease the memory occupied by a program?

Robin Getz (UNITED STATES)

Message: 60828   

 

Aaron/Yi:

 

Even for flat - you can look in /proc/pid/maps (where pid is the pid number, - ie /proc/5/maps).

 

-Robin

QuoteReplyEditDelete

 

 

2008-08-21 11:22:02     Re: How to decrease the memory occupied by a program?

Mike Frysinger (UNITED STATES)

Message: 60847   

 

the maps file format is documented in the proc(5) man page

QuoteReplyEditDelete

 

 

2008-08-22 03:29:49     Re: How to decrease the memory occupied by a program?

Aaron Zhong (CHINA)

Message: 60867   

 

Thanks for all of you.

This is my information for /proc/pid/maps:

 

...

00454000-0045a000 rw-p 00000000 00:00 0

00460000-0046b000 rw-p 00000000 00:00 0

00472000-00476000 rw-p 00000000 00:00 0

00484000-00884000 rwxp 00000000 00:00 0

00978000-00983000 rw-p 00000000 00:00 0

...

 

My problem is why the momory, for example from 454000 to 45a000 which size is 0x6000, is allocated and what is its content?  I think if I could find out the reason, maybe the momory could be decreased by optimizing the program.

QuoteReplyEditDelete

 

 

2008-08-22 09:49:34     Re: How to decrease the memory occupied by a program?

Mike Frysinger (UNITED STATES)

Message: 60911   

 

use readelf on your binary to view the section and program headers then

 

otherwise you need to rebuild things as FDPIC like Yi said to get file section break downs

Attachments

    Outcomes