Java Heap Size

Hi Experts,

I am looking for a any java/ jython/python code to get the current heap usage, max, used in Websphere Application Server.
Who is Participating?
HonorGodSoftware EngineerCommented:
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

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

HonorGodSoftware EngineerCommented:
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?
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

07592161981mAuthor Commented:
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.
HonorGodSoftware EngineerCommented:
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
07592161981mAuthor Commented:
I like to hardcode in the script.

the Name of the server is Websphere_Portal.
07592161981mAuthor Commented:
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,*]
HonorGodSoftware EngineerCommented:
What output is generated by executing:

puts [$AdminControl queryNames type=JVM,process=WebSphere_Portal,*]
07592161981mAuthor Commented:
the output is getting blank line

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

HonorGodSoftware EngineerCommented:
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
HonorGodSoftware EngineerCommented:
Thanks for the grade & points.

Good luck & have a great day.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.