netcat
netcat

Reputation: 1335

Memory Fragmentation from reading file in Embedded System

I have a configuration text file on an embedded system with a Linux OS. Requirements are that the file is text, and the embedded system has 32 Megabytes of dynamic ram. The app. that will read the file is coded in C++.

The file could be read using a method like this.

#include <string>
#include <fstream>

ifstream infile ("config_file_path");    
if (infile.good())
{
    string line;   
    // Set capacity to length of the longest line.
    const unsigned char maxLen = 100;

    line.reserve (maxLen);

    while (std::getline (infile, line))
    { 
        // Process the data in the line.
        processData (line);
    }
}

Would heap fragmentation be an issue for this implementation? The file can have up to about 150 lines of text to read.

Upvotes: 2

Views: 254

Answers (2)

Ziezi
Ziezi

Reputation: 6467

Would heap fragmentation be an issue for this implementation?

In the displayed sample I can't identify a line that would indicate heap fragmentation.

You are using std::string object whose internal mechanisms are considered generally unpredictable, i.e. how big the object will grow and when it will deallocate memory and that's it.

  • As a first step consider std::string::capacity to ensure more compact memory.

  • As a second step consider providing custom allocators, specific alternatives to the general free store (heap) are: Pool and Stack.

  • As a final step consider using less abstract code, i.e. pointers, arrays, etc, which is the inevitable solution when you are closer to the hardware. especially when you are using the object just to extract input and pass as parameter, i.e. no use of any of the std::string specific member functions, etc.

Upvotes: 0

Alexander
Alexander

Reputation: 758

it is hard to say if your app suffers from memory fragmentation from your code. (your code might add some fragmentation but i do not know how critical it is) you can try to use non-standard malloc libraries - jemalloc nedmalloc tcmalloc they might give you better objects layout as well as an ability to dump the memory layout

general approach:

check if the app can get "not enough memory". stress test might help. check how much free memory your app has and how fragmented it is. if the fragmentation is the issue - try the following: heap loves LIFO principle (delete the last created block). try to keep variables on stack. use specialized allocators.

in case of your function:

in order to minimize heap pressure, you can try to read lines into a stack buffer (e.g. using fgets)

Upvotes: 1

Related Questions