13


I have a small application that I was running right now and I wanted to check if I have any memory leaks in it so I put in this piece of code:

for (unsigned int i = 0; i<10000; i++) {
    for (unsigned int j = 0; j<10000; j++) {
        std::ifstream &a = s->fhandle->open("test");
        char temp[30];
        a.getline(temp, 30);
        s->fhandle->close("test");
    }
}

When I ran the application i cat'ed /proc//status to see if the memory increases. The output is the following after about 2 Minutes of runtime:

Name:   origin-test
State:  R (running)
Tgid:   7267
Pid:    7267
PPid:   6619
TracerPid:  0
Uid:    1000    1000    1000    1000
Gid:    1000    1000    1000    1000
FDSize: 256
Groups: 4 20 24 46 110 111 119 122 1000 
VmPeak:   183848 kB
VmSize:   118308 kB
VmLck:         0 kB
VmHWM:      5116 kB
VmRSS:      5116 kB
VmData:     9560 kB
VmStk:       136 kB
VmExe:        28 kB
VmLib:     11496 kB
VmPTE:       240 kB
VmSwap:        0 kB
Threads:    2
SigQ:   0/16382
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000002004
SigCgt: 00000001800044c2
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: ffffffffffffffff
Cpus_allowed:   3f
Cpus_allowed_list:  0-5
Mems_allowed:   00000000,00000001
Mems_allowed_list:  0
voluntary_ctxt_switches:    120
nonvoluntary_ctxt_switches: 26475

None of the values did change except the last one, so does the mean there are no memory leaks?

But what's more important and what I would like to know is, if it is bad that the last value is increasing rapidly (about 26475 switches in about 2 Minutes!).

I looked at some other applications to compare how much non-volunary switches they have:

  1. Firefox: about 200
  2. Gdm: 2
  3. Netbeans: 19

Then I googled and found out some stuff but it's to technical for me to understand. What I got from it is that this happens when the application switches the processor or something? (I have an Amd 6-core processor btw).

How can I prevent my application from doing that and in how far could this be a problem when running the application?

Thanks in advance, Robin.

Robin
  • 1,322
  • 1
  • 13
  • 23
  • 1
    Since you are probably on linux, why not use valgrind to help you find possible memory leak? – Palmik Dec 13 '10 at 15:48
  • I don't understand why you think that code would help you check for memory leaks. – Ferruccio Dec 13 '10 at 15:53
  • Well, I guess it could help him find memory leak in the code that allocate / destroy those input file stream. – Sylvain Defresne Dec 13 '10 at 15:57
  • Yes, the filehandler reads the file from a possible of three different sources (three different vfs's). The textfile is about one Mb, I just wanted to check if all those dealocate the memory. If not, the usage would rapidly increase wouldn't it? Anyway, I checked with valgrind and no leak was detected so I should be in the clear :) – Robin Dec 13 '10 at 16:00
  • X for this X Y: http://stackoverflow.com/questions/2595735/prevent-linux-thread-from-being-interrupted-by-scheduler – Ciro Santilli OurBigBook.com Sep 23 '15 at 19:56

3 Answers3

21

Voluntary context switch occurs when your application is blocked in a system call and the kernel decide to give it's time slice to another process.

Non voluntary context switch occurs when your application has used all the timeslice the scheduler has attributed to it (the kernel try to pretend that each application has the whole computer for themselves, and can use as much CPU they want, but has to switch from one to another so that the user has the illusion that they are all running in parallel).

In your case, since you're opening, closing and reading from the same file, it probably stay in the virtual file system cache during the whole execution of the process, and youre program is being preempted by the kernel as it is not blocking (either because of system or library caches). On the other hand, Firefox, Gdm and Netbeans are mostly waiting for input from the user or from the network, and must not be preempted by the kernel.

Those context switches are not harmful. On the contrary, it allow your processor to be used to fairly by all application even when one of them is waiting for some resource.≈

And BTW, to detect memory leaks, a better solution would be to use a tool dedicated to this, such as valgrind.

Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85
  • 3
    More than Unix, every preemptive multitasking operating system (that is, almost every operating system in common use). – Sylvain Defresne Dec 13 '10 at 15:54
  • Non-voluntary? Or did you mean voluntary? As I understand it, when you call the OS, you yield control to it, so if it decides to switch context, it is "voluntary". And when your application just consumes a lot of CPU (like in those loops), the OS may choose to forcibly suspend execution of an application and switch context "non-voluntarily". Am I wrong? – Sergei Tachenov Dec 13 '10 at 15:55
  • You're right, I mixed the too. I'll edit my answer. Thank you. – Sylvain Defresne Dec 13 '10 at 15:58
  • Also, AFAIK, voluntary includes a yield timeslice syscall. (ala sched_yield()) – Katastic Voyage Aug 06 '17 at 09:13
3

To add to @Sylvain's info, there is a nice background article on Linux scheduling here: "Inside the Linux scheduler" (developerWorks, June 2006).

Nickolay
  • 31,095
  • 13
  • 107
  • 185
Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
1

To look for a memory leak it is much better to install and use valgrind, http://www.valgrind.org/. It will identify memory leaks in the heap and memory error conditions (using uninitialized memory, tons of other problems). I use it almost every day.

gravitron
  • 3,514
  • 1
  • 20
  • 18