← All posts

How To: Force Garbage Collection using WLST

Trigger cmo.runGC() when the JVM won't clean itself

Unni Pillai
Unni Pillai

Many times JVM misbehaves, and we may have to force GC. The best way to do it is using WLST.

Although the script doesn’t guarantee that GC would run, but many cases a force GC runs and a lot of memory gets freed up.

I expect you to have:

1. A Weblogic Server Domain

2. Name of the server for which you want to perform GC

3. User name and password for the server

The Script:

1. Open a text editor and Copy – paste the text given below into the editor.

-

Written by Unni on Sat 7 Feb 19:57(IST) download

  1. # WLST script which calls GC.

  2. from java.util import *

  3. from javax.management import *

  4. import javax.management.Attribute

  5. print ‘starting the script … ’

  6. # please replace userid and password with your AdminServer userid and password

  7. # plz change the IP adresss and port number accordingly

  8. connect(‘userid’,‘password’,url=‘t3://localhost:port’)

  9. state(‘AdminServer’)

  10. # For Force GC …

  11. domainRuntime()

  12. cd(‘/ServerRuntimes/AdminServer/JVMRuntime/AdminServer’)

  13. print ’ Performing Force GC…’

  14. cmo.runGC()

  15. disconnect()

  16. print ‘End of script …’

  17. exit()

-

2. Save the file as ‘forceGC.py’

3. Open a new command window à Run ‘setDomainEnv.cmd/.sh’ localted in the ‘domain\bin’ directory

4. Run the python script using WLST.

java weblogic.WLST forceGC.py

5. This should have freed up some memory on your server.

6. The above script runs ‘GC’ on ‘AdminServer’ to run the script of managered server replace the ‘AdminServer’ from the script with the name of the ‘ManagedServer’.

Hope this helps :)

Unni

unni@unnikrishnanpillai.tk