Stack Memory

I have a java process that piles up the stack memory.

 ulimit -a
core file size        (blocks, -c) unlimited
data seg size         (kbytes, -d) unlimited
file size             (blocks, -f) unlimited
open files                    (-n) 1024
pipe size          (512 bytes, -p) 10
stack size            (kbytes, -s) 8192
cpu time             (seconds, -t) unlimited
max user processes            (-u) 29995
virtual memory        (kbytes, -v) unlimited

The max stack memory is highlighted in bold.

But i need to know what is the current HEAP memory used by my java process PID.

Regards,
Mohtashim

What system are you using?

That's your data segment. It's set to 'unlimited'.

Thats my OS
SunOS mypc 5.10 Generic_144488-07 sun4v sparc SUNW,SPARC-Enterprise-T5220

With my java code it throws an error as it breaches stack size (kbytes, -s) 8192 of stack memory ater 12-15 mins time.

I wish to monitor the current stack memory used by my PID (java program) so that i can alert before it breaches the limit which is 8192K.

Kindly help.

Check:

pmap PID

Hey thanks a ton for you directed me to the right place. PMAP helps.
But i need a little more push to get going :D.

Based on tests i believe that the stack memory is shared amoung processes.

so if there are 4 instances of the same process running parellely and the total stack memory of the OS is approx 8000k, each process will get 2000k before running out of stack. True pmap helps the current stack memory consumption for that PID, but if I wish to alert before the stack runs out of memory for that PID, I need to know what is the current free stack memory left on the operating system. Can you help me the know how to achieve this alert mechanism?

PID=2756
Output:

 
pmap 2756 | grep stack
B13CE000       8K rw--R    [ stack tid=34 ]
B157E000       8K rw--R    [ stack tid=35 ]
B167C000      16K rw--R    [ stack tid=33 ]
B177C000      16K rw--R    [ stack tid=32 ]
B1FCE000       8K rw--R    [ stack tid=30 ]
B27C8000      32K rw--R    [ stack tid=29 ]
B2FC8000      32K rw--R    [ stack tid=28 ]
B307E000       8K rw--R    [ stack tid=31 ]
B317E000       8K rw--R    [ stack tid=27 ]
B327E000       8K rw--R    [ stack tid=26 ]
B337E000       8K rw--R    [ stack tid=25 ]
B347E000       8K rw--R    [ stack tid=24 ]
B357E000       8K rw--R    [ stack tid=23 ]
B367E000       8K rw--R    [ stack tid=22 ]
B377E000       8K rw--R    [ stack tid=21 ]
B387E000       8K rw--R    [ stack tid=20 ]
B397E000       8K rw--R    [ stack tid=19 ]
B3A7E000       8K rw--R    [ stack tid=18 ]
B3B7E000       8K rw--R    [ stack tid=17 ]
B3C7E000       8K rw--R    [ stack tid=16 ]
B3D7E000       8K rw--R    [ stack tid=15 ]
B3E7E000       8K rw--R    [ stack tid=14 ]
B3F7E000       8K rw--R    [ stack tid=13 ]
B407E000       8K rw--R    [ stack tid=12 ]
B417E000       8K rw--R    [ stack tid=11 ]
B427E000       8K rw--R    [ stack tid=10 ]
B437E000       8K rw--R    [ stack tid=9 ]
F897E000       8K rw--R    [ stack tid=8 ]
F8A7E000       8K rw--R    [ stack tid=7 ]
F8B7E000       8K rw--R    [ stack tid=6 ]
FAD7E000       8K rw--R    [ stack tid=5 ]
FAE7E000       8K rw--R    [ stack tid=4 ]
FAF7E000       8K rw--R    [ stack tid=3 ]
FB8FE000       8K rw--R    [ stack tid=2 ]
FF7D4000     176K rw---    [ stack ]
FF800000    4096K rw---    [ stack ]

pmap 2756 | grep stack | awk '{print $2}'

8K
8K
16K
16K
8K
32K
32K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
8K
1536K

Also wanted to know if I need to total this or the last value is the total stack meory consumption for that PID.

I'm pretty sure ulimit is controlling sizes of different structures for each process separately, not whole system. As for your output, I guess adding those values would be correct :wink:

Bartus agreed and thanks !!! Is it correct to say everything listed under ulimit -a is for each process seperately?

ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 10
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 29995
virtual memory (kbytes, -v) unlimited

Also, can you confirm that the stack for a PID is the sum of the complete pmap output shared above or just the last two lines, becoz for a process not doing much the sum of the stack shows Total used stack memory is:4680

Below is my code:

stackmem="$(pmap $1 | grep stack | awk '{print $2}'| tr -d ' K')"
n=0
for stack in $stackmem
do
n=`expr $n + $stack`
done
echo "Total used stack memory is:$n"

I guess we may have to discard the stack with the tids [ stack tid=2 ]
while summing the total stack memory used by a PID.

It will be great if you could confirm/comment.

Regards,
Mohtashim

You can't discard stack segments with tid values as they are related to threads running as part of your process and as such are part of the process stack segments. I think you should increase the value in ulimit to allow your process to run with more stack segments. Another option is to redesign your application :wink:

From the getrlimit() man page: