Java Heap Size

Posted on 2012-09-10
Medium Priority
Last Modified: 2012-10-03
Hi Experts,

I am looking for a any java/ jython/python code to get the current heap usage, max, used in Websphere Application Server.
Question by:07592161981m
  • 6
  • 4

Expert Comment

ID: 38385461
You can use following peace of java code

public class TestMemory {
    public static void main(String [] args) {
        int mb = 1024*1024;
        //Getting the runtime reference from system
        Runtime runtime = Runtime.getRuntime();
        System.out.println("##### Heap utilization statistics [MB] #####");
        //Print used memory
        System.out.println("Used Memory:"
            + (runtime.totalMemory() - runtime.freeMemory()) / mb);
        //Print free memory
        System.out.println("Free Memory:"
            + runtime.freeMemory() / mb);
        //Print total available memory
        System.out.println("Total Memory:" + runtime.totalMemory() / mb);
        //Print Maximum available memory
        System.out.println("Max Memory:" + runtime.maxMemory() / mb);

Open in new window

LVL 41

Expert Comment

ID: 38386357
Q: What version of WebSphere is being used?

Q:  What's in the "Installed product" section of the output of the versionInfo command script?
      - Windows:
        > D:\IBM\WebSphere\AppServer\bin\versionInfo
      - *ix
        # /opt/IBM/WebSphere/AppServer/bin/versionInfo.sh

Q: Are you talking about a specific server, or all JVMs in the cell?

Author Comment

ID: 38387988
Name                     IBM WebSphere Application Server - ND
ID                       ND
Build Level              cf231218.02
Build Date               4/30/12

This is for a specific server in a cell.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 41

Expert Comment

ID: 38388584
Do you want to hard code the name of the server in the script, or provide it on the script command line?

e.g., something like:

heapInfo server1


healInfo -n node01 -s server1

Author Comment

ID: 38388840
I like to hardcode in the script.

the Name of the server is Websphere_Portal.
LVL 41

Accepted Solution

HonorGod earned 2000 total points
ID: 38389111
Something like this perhaps?
serverName = 'Websphere_Portal'

formatStr = '''
     serverName: %(serverName)s
initialHeapSize: %(iHS)s
maximumHeapSize: %(mHS)s
      %(message)s: %(maxMemory)s

def heapSizes( jvm ) :
    return (
        AdminConfig.showAttribute( jvm, 'initialHeapSize' ),
        AdminConfig.showAttribute( jvm, 'maximumHeapSize' )

def beanName( processName ) :
    return AdminControl.queryNames( 'type=JVM,process=%s,*' % processName )

for server in AdminConfig.list( 'Server' ).splitlines() :
    if AdminConfig.showAttribute( server, 'name' ) == serverName :
#       print AdminConfig.show( server )
        jvm = AdminConfig.list( 'JavaVirtualMachine', server )
#       print AdminConfig.show( jvm )
        iHS, mHS = heapSizes( jvm )
        bean = beanName( serverName )
        if bean :
            message = 'maxMemory'
            maxMemory = AdminControl.invoke( bean, 'getMaxMemory')
        else :
            message = '   Status'
            maxMemory = 'Specified JVM not available'
        print formatStr % locals()
else :
    print 'Specified server "%s" not found.' % serverName

Open in new window


Author Comment

ID: 38407684
I am getting errors while i ran the script. I have two processes running server1 and WebSphere_Portal . with the wsadmin script it always point to server1.

wsadmin>$set jvm [$AdminControl completeObjectName type=JVM,process=WebSphere_Portal,*]
com.ibm.ws.scripting.ScriptingException: WASX7025E: Error found in String ""; cannot create ObjectName
wsadmin>set jvm [$AdminControl completeObjectName type=JVM,process=server1,*]
LVL 41

Expert Comment

ID: 38409077
What output is generated by executing:

puts [$AdminControl queryNames type=JVM,process=WebSphere_Portal,*]

Author Comment

ID: 38410814
the output is getting blank line

wsadmin>puts [$AdminControl queryNames type=JVM,process=WebSphere_Portal,*]

LVL 41

Expert Comment

ID: 38411086
meaning that no JVM matching that pattern is currently active.

What do you get when you execute:

puts [$AdminControl queryNames type=JVM,*]

You should get one line (MBean name) for each active JVM
LVL 41

Expert Comment

ID: 38460668
Thanks for the grade & points.

Good luck & have a great day.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

-Xmx and -Xms are the two JVM options often used to tune JVM heap size.   Here are some common mistakes made when using them:   Assume BigApp is a java class file for the below examples. 1.         Missing m, M, g or G at the end …
This exercise is about for the following scenario: Dmgr and One node with 2 application server. Each application server contains it owns application. Application server name as follows server1 contains app1 server2 contains app1 Prereq…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question