In uClinux, there are memory fragments (memory fragment), or less busybox

Source: Internet
Author: User
Reprinted please indicate the source and the author contact: http://blog.csdn.net/mimepp
Contact information: Yu Tao <yut616 at Sohu dot com>

Recently, in product development, some optimizations are required to make the product performance more stable. One of the major unstable factors involved is the misuse of busybox.
In uClinux, the stack and heap cannot be dynamically allocated because there is no MMU or VM.
Malloc/free in the program is a large memory pool based on uClinux kernel, which is equivalent to sharing a heap for all processes.
There is a problem here. After the system runs for a period of time, memory may be broken into fragments, resulting in a failure to reallocate a large block of memory, however, the total actual idle memory is large.
When running busybox, such as calling CP and other commands in applications, each call consumes about KB of memory, which becomes fragments after being released, soon when malloc is executed elsewhere in the Code, the out-of-memory will appear.

Root @ desktop # ls-L./bin/busybox
-Rwxr-XR-x 1 Root 140848 Aug 22./bin/busybox

Root @ desktop # arm-elf-flthdr./bin/busybox
Bin/busybox
Magic: bflt
Rev: 4
Build Date: Wed Aug 22 20:13:31 2007
Entry: 0x50
Data start: 0x37760
Data end: 0x3eaa0
BSS end: 0x44e10
Stack size: 0x8000
Reloc start: 0x3eaa0
Reloc count: 0 xedd
Flags: 0x5 (load-to-ram gzip-compressed)

The target file generated by compilation generally contains several sections:
. Text, or code, is the body segment, containing command code, is a read-only code area
. Data is a data segment that contains fixed data, such as constants and strings. It is a readable and writable data zone.
. BSS is the uninitialized data segment, including uninitialized variables and arrays. It is a readable and writable data zone without initialization.

From the above content, we can see that:
The data size is 29504.
The stack size is 32 KB.
BSS is 282128.
BSS is the abbreviation of block started by symbol, that is, the block starting with the symbol.
The BSS value is only a tag value. It does not really put this entire reserved space in the program. Instead, it first uses this number to indicate that the required reserved space is truly determined during loading.
Reloc is 3805: Relocation relocation, which is the intermediate data used for relocation.

From the BSS, We can generally know that the memory size required for running busybox is about 280 K, which is relatively large for an embedded system. if the remaining 10 MB of memory is used, more than 10 calls to busybox may result in malloc memory failure for other applications.

How to avoid:
1. Resident programs in the memory. Do not start the program repeatedly.
2. when writing APIs, some frequently used commands can be used in applications, such as copy, to implement some functions in their own code. Of course, this application should always be in the memory, it should not be started repeatedly.
3. Some External commands can be called during the system startup process, which has limited impact. However, in general operation, try to avoid starting other applications repeatedly, such as busybox.

The following are some information about the Internet:
The destination file segment: Text (or code), data, and BSS. The text (or code) area is a read-only code area. The data area is a readable and writable data area. For example, if you define a variable in a program and assign a value of 5 to it, the "5" is stored in the data area. The BSS zone is a readable and writable data zone without initialization. It stores arrays without any values. Note: The BSS zone is a virtual zone and does not exist in the binary image. However, when the binary image is loaded, it will exist in the memory.

Stack is automatically allocated by the system, mainly for function parameters and local variables
Heap is allocated by the programmer malloc. There is a byte in the header of the heap to indicate the size of the heap to be allocated. It will be used for release.
Stack stack is from high to low, and the size is fixed. It is a parameter specified during system compilation. Therefore, the space available from Stack is small.
Heap heap is low to high and can be discontinuous. A linked list is used to manage idle memory addresses. The heap size is limited by the system memory size, in this way, the heap can obtain a large amount of space and be flexible.
A classic online example: http://my.opera.com/javen/blog/show.dml/265058

Int A = 0; global initialization Zone
Char * P1; uninitialized globally
Main ()
...{
Int B; stack
Char s [] = "ABC"; stack
Char * P2; stack
Char * P3 = "123456"; 123456 is in the constant zone, and P3 is on the stack.
Static int C = 0; Global (static) initialization Zone
P1 = (char *) malloc (10 );
P2 = (char *) malloc (20 );
The allocated 10-byte and 20-byte areas are in the heap area.
Strcpy (P1, "123456"); 123456 is placed in the constant area, and the compiler may optimize it to the "123456" that P3 points.
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.