1

I apologise for big post, to explain my problem it is really required. Please bear with me.

I have been developing a C based commercial application in Linux (Ubuntu) for last 1+ year. My aim is to make it highly stable and must run for infinite time without any system error.

The major problem I face is memory shortage. The program runs for around 8 hours and after that I see it starts using swap memory. Then everything starts to execute bit slow.

The first thing that came to my mind is memory leak. I used Valgrind, code review to check for memory leak. I am putting heavy load on my application in Valgrind mode in a cyclic pattern for several hours, some tests pass, some fail, but I do not see memory leak or invalid access. I can confirm that there is no memory leak in my application.

I checked using top and free commands to see memory usage. The free -m command output looks as below.

             total       used       free     shared    buffers     cached
Mem:          7858       2146       5712        426         11        759
-/+ buffers/cache:       1375       6483
Swap:         8062          0       8062

This the initial state, where memory consumed is less. After I started the application, I did wait for 5 min. Then free -m output shows as below.

             total       used       free     shared    buffers     cached
Mem:          7858       3611       4247        563         71       1892
-/+ buffers/cache:       1647       6211
Swap:         8062          0       8062

If you see the difference, it is really big. Initially used memory was 2146 MB, and after 5 min run, the memory used is 3611 MB. The difference is 1465 MB, which is around 1GB+. I am sure currently my application is not using 1GB+ RAM. Probably major part of the memory is in cached section.

If you see cached column (last one), you can see the difference of 1133 MB.

Also you can see buffers (last but second column), buffers increased by 60.

These values keeps increasing and finally system starts using swap memory. Once it reaches that stage, then my application sustains for 1 more house and finally system speed becomes very slow, so I close the application.

My expectation is let system go and store allocated memory segments in cached section, but when my m/c RAM usage is say 90%, further OS should start using from cached memory, thereby 10% of RAM is always there for other applications. Is there any OS level configuration where I can put a cap beyond which cache/buffers should start yielding memory when RAM is about to exhaust.

Is there any configuration, where strictly I will say to OS do not use swap memory. Is it /proc/sys/vm/swappiness file, where I can change default value of 60 to 0, will it cause any issue.

Please guide me with your valuable input.

Austin
  • 1,709
  • 20
  • 40
  • This could be relevant? http://stackoverflow.com/questions/8367001/how-to-check-heap-size-for-a-process-on-linux – Mike Bessonov Dec 26 '16 at 08:27
  • 1
    Just because you're not leaking memory doesn't mean you're not holding too much in memory. For example, an ever expanding linked list does not leak, but it still consumes memory. So without details about your code, we can't answer. I'd suggest a good hard look at what algorithms you're using and where it might be consuming too much memory. – Schwern Dec 26 '16 at 08:47
  • Keeping this thing in mind, every 10 minutes I print the size of all linked lists used in my application. At some point I had this issue, and I have solved it. Probably to rule out this, I can check memory usage for process corresponding to my application. – Austin Dec 26 '16 at 09:06
  • 1
    You can count the number of nodes on your linked lists, but you cannot count the number of nodes **not** on your linked lists. As a check, either maintain a counter of all allocations and compare with that, or (statically) allocate a fixed number of nodes, and maintain your own free list(s). – wildplasser Dec 26 '16 at 10:46
  • Now, did you _check memory usage for process_? – Armali Jan 23 '19 at 08:50

0 Answers0