We wrote a very simple C++ program to isolate a bug. The app takes a number as an argument and creates that number of threads and sends all those threads into an event loop. If we run app with >3 threads (including main thread) top shows it taking 100+MB in virtual memory. However, if we run it with <=3 threads, it runs with about 36MB virtual memory. We strace
'd the app and found out that in the first scenario there is a mmap
of about 65MB that is mapped anonymously that does not get unmapped. The problem is the memory usage goes up as the number of threads go up. And we have a large number of binaries which have large number of threads so there seems to be a lot of wasted space. Why does this happen? SLES11 6开发者_如何学JAVA4bit.
Each thread gets by default a stack of around 8Mb. You can set the default when you create a thread with pthread_attr_setstacksize. Make sure you are always either: pthread_join()'ing threads that have ended. Or; create them as detached threads, otherwise you'll leak memory when a thread ends.
Having a big virtual memorry usage is usually not a problem though, unless you really are using all that space, it's just virtual memory - and you'll hardly run out of that on a 64 bit machine.
精彩评论