Java Heap Size

Posted on 2012-09-10
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
    LVL 1

    Expert Comment

    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

    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/

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

    Author Comment

    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.
    LVL 41

    Expert Comment

    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

    I like to hardcode in the script.

    the Name of the server is Websphere_Portal.
    LVL 41

    Accepted Solution

    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 server )
            jvm = AdminConfig.list( 'JavaVirtualMachine', server )
    #       print 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

    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,*] WASX7025E: Error found in String ""; cannot create ObjectName
    wsadmin>set jvm [$AdminControl completeObjectName type=JVM,process=server1,*]
    LVL 41

    Expert Comment

    What output is generated by executing:

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

    Author Comment

    the output is getting blank line

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

    LVL 41

    Expert Comment

    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

    Thanks for the grade & points.

    Good luck & have a great day.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Configure Web Service (server application) I. Configure security for Web Services methods First, we need to protect Session bean which implements the service: 1. Open EJB deployment descriptor (ejb-jar.xml) in the EJB project that contains you…
    Verbose logging is used to diagnose garbage collector problems. By default, -verbose:gc output is written to either native_stderr.log or native_stdout.log.   It is also possible to redirect the logs to a user-specified file. This article will de…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now