LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Script for Memory Usage?? (https://www.linuxquestions.org/questions/linux-server-73/script-for-memory-usage-631812/)

ajeetraina 03-31-2008 12:46 AM

Script for Memory Usage??
 
I have a JAVA Processes running and all I need to find out what total memory it is utilising.If its usage exceed 500 MB (My memory is 1GB)it should throw error or report the % memory Usage.
IS there any script which can display the same??

shadowsnipes 03-31-2008 02:03 AM

You can get the memory usage of a process with a command like
Code:

ps -C command-name -o rss=

livetoday 03-31-2008 03:07 AM

and percentage of 'used physical memory' by

Quote:

ps aux | grep command | awk '{print $4,$NF}'

ajeetraina 03-31-2008 03:40 AM

The command threw:
vjs@vjs:~$ ps -C java -o rss=
393504
What is this???What does rss means?

LEmme me tel you issue in detail.
I am tring to write a script which shouldnt allow Java application to exceed 500MB irrespective of total RAM Size.
It Means if RAM is 30GB also,it should retrict it to 500MB.
How can I do this?
All i tried a attempt:
Code:

mem=`ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr | grep java |cut -c1-4`;

echo "-------------------------------------------------------------------------";

echo "% MEMORY CONSUMED BY JAVA PROCESSES";

echo "--------------------------------------------------------------------------";

echo "$mem";

limit=50;

if [ $mem -ge $limit ]; then

echo "Memory has reached its limit"        // you can add the favourable condition like mail, restarting applications etc.

else

echo "Memory still not crossed the limit"

fi

Output
Code:

vjs@vjs:~$ ./memscript.sh
------------------------------------
% MEMORY CONSUMED BY JAVA
------------------------------------
38.6
./memscript.sh: line 9: [: mem: integer expression expected
Memory still not crossed the limit
vjs@vjs:~$

This is not what I am attempting at.It simpl restrict to 50% but i want it to restrict to 500MB only.
Pls help

shadowsnipes 03-31-2008 09:18 AM

Quote:

Originally Posted by ajeetraina (Post 3105650)
The command threw:
vjs@vjs:~$ ps -C java -o rss=
393504
What is this???What does rss means?

resident set size - The amount of real RAM the process is using. That number is in KB so divide by 1024 if you want the MB equivalent. Set that value for your mem variable.

unSpawn 03-31-2008 09:22 AM

If you want to restrict memory in Java processes, shouldn't you use the -Xms/-Xmc startup parameters instead of scripting something?

shadowsnipes 03-31-2008 09:27 AM

Quote:

Originally Posted by unSpawn (Post 3105930)
If you want to restrict memory in Java processes, shouldn't you use the -Xms/-Xmc startup parameters instead of scripting something?

that'd do it. Good call. Excert from the java man page:

Code:

            -Xmsn
                Specify the initial size, in bytes, of the memory allocation
                pool. This value must be a multiple of 1024 greater than 1MB.
                Append the letter k or K to indicate kilobytes, or m or M to
                indicate megabytes. The default value is chosen at runtime
                based on system configuration. For more information, see
                HotSpot Ergonomics
                Examples:

                      -Xms6291456
                      -Xms6144k
                      -Xms6m

            -Xmxn
                Specify the maximum size, in bytes, of the memory allocation
                pool. This value must a multiple of 1024 greater than 2MB.
                Append the letter k or K to indicate kilobytes, or m or M to
                indicate megabytes. The default value is chosen at runtime
                based on system configuration. For more information, see
                HotSpot Ergonomics
                Examples:

                      -Xmx83886080
                      -Xmx81920k
                      -Xmx80m



All times are GMT -5. The time now is 05:50 AM.