Ady Wicaksono Daily Activities

Archive for July 10th, 2007

I can not create more than 255 threads on Linux. What is the solutions?

with 4 comments

On kernel 2.4, you could only use up to 2 Gbytes (or actually 2^31 bytes ~ 2147483648 bytes) of Linux Virtual memory, even if you have 4 Gbytes memory or more :D.

If you want more than 2 Gbytes, you need to change the split of userspace/kernelspace
from 2G/2G (as default) of common Linux distro to something else like 3G/1G or maybe
4G/4G if you have 8 Gbytes RAM.

On kernel 2.6, you could use up to 2.8 Gbytes. Bigger right? But I need more.

I try to create a simple thread application, that simply count how much thread i could create?
Uh, i could only create up to 255 thread…….. What Is Problem?

From, I found that by default, Linux 2.4 set the stack size of each thread to 8Mbytes (equiv to 8 * 1024 * 1024 = 8388608 bytes = 2^23 bytes).
You could use “ulimit -a” command to check the amount of stack size like this
———- Cut Here ———-
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
———- Cut Here ———-

When you have 8Mbytes for each thread stack size, then you will hit this limit 2^(31-23) ~ 2^8 ~ 256 thread.

So, the solutions is REDUCE the THREAD STACK SIZE !!!. However reducing the stack size is dangerous, if any thread of a program overflows its stack the program will malfunction and probably crash.
Worse, there is no general way to know what the required stack size is!
You have to carefully design your program to use a bounded amount of stack in each thread.

How to do that? Again you could use :
1. “ulimit” command, for example ulimit -s 192 will reduce the stack size from 8192 kbytes to 192kbytes.

2. internally on your C application call pthread_attr_setstacksize().

3. internally on your Java Application you may use “-Xss” to set the stack size.

Now, how many thread could be created? For example you set thread stack size to 196608 bytes, and you have

up to 2147483648 bytes virtual memory (2Gbytes) then you can create 2147483648/196608 ~ 10922 threads.

But, be warn with maximum open files 🙂 of each user (default is 1024), set it using
“ulimit -n ” first or you only have 1024 thread 😀 .

Written by adywicaksono

July 10, 2007 at 5:04 pm

Posted in Linux

Swap 2 variables without temporary variables?

with 3 comments

This is the solution:

#include <stdio.h>

void swap(int *x, int *y){
  *x -= *y;
  *y += *x;
  *x  = (*y - *x);

int main(){
   int a = 100;
   int b = 200;
   printf("a = %dnb = %dn",a,b);
   printf("a = %dnb = %dn",a,b);
   return 0;

Written by adywicaksono

July 10, 2007 at 4:55 pm

Posted in Programming

Mario Bros…. Classic Guitar

leave a comment »

Another hacker on music industry, I will learn it 🙂

Written by adywicaksono

July 10, 2007 at 4:51 pm

Posted in Music

This man is awesome

with 2 comments

I like his guitar style 🙂

Written by adywicaksono

July 10, 2007 at 4:41 pm

Posted in Music, Uncategorized

32 Bit CPU with memory more than 4 Gbytes

leave a comment »

One day, I got a Dell server based on Intel Xeon EM64T 32 bit with 8 Gbytes RAM. As far as I know, 32 bit CPU could only map 2^32 or 4 Gbytes RAM, so with this 8 Gbytes RAM will it be useless?Or should I use 64 bit CPU which could map 2^64 or 18446744073709551616 bytes memory.

I found this paper

and found that Intel technology already found solution for this problem. Intel CPU 32 bit could read memory more than 4 Gbytes using INTEL® EXTENDED MEMORY 64 TECHNOLOGY (EM64T) . Intel EM64T could process memory up to 64Gbytes.

Written by adywicaksono

July 10, 2007 at 4:23 pm

Posted in Linux