0

I read many posts on the internet and here on stackoverflow, but I'm still not able to increase Tomcat Heap Size. Probably I made an error that I can't recognize.

Following this guide I did these steps:

  1. I created the file setenv.bat inside the folder {tomcat}\bin
  2. Inside the file setenv.bat I added the string set "JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx1024m -XX:MaxPermSize=512m -server"

When I run Tomcat and then I launch my app (for instance localhost:8080/appName) and I try to know the heap informations this way:

long maxHeapSize = Runtime.getRuntime().maxMemory();        
long freeHeapSize = Runtime.getRuntime().freeMemory();  
long totalHeapSize = Runtime.getRuntime().totalMemory();    
System.out.println("Max Heap Size = " + (maxHeapSize/1024/1024)+ " MB");    
System.out.println("Free Heap Size = " + (freeHeapSize/1024/1024)+ " MB");  
System.out.println("Total Heap Size = " + (totalHeapSize/1024/1024)+ " MB");        

I get that the Max Heap Size is always 256 MB. Does anybody know what's my error?

trincot
  • 317,000
  • 35
  • 244
  • 286
MDP
  • 4,177
  • 21
  • 63
  • 119
  • Possible duplicate of [Increasing Tomcat Heap Size](https://stackoverflow.com/questions/25591063/increasing-tomcat-heap-size) – João Rebelo Jan 09 '19 at 10:03
  • Hi @JoãoRebelo I already read and followed that post. It didn't solve mt problem – MDP Jan 09 '19 at 11:01
  • Tomcat uses 'catalina.bat' (or) 'catalina.sh' for startup. Add the lines on that startup file. – Ramesh Subramanian Jan 09 '19 at 11:07
  • I am not sure what your issue is then. From the answer to the duplicate you may find that the JVM only requests more memory from the OS when the current heap size is not sufficient for its needs. When you set this flag up `-Xms128m` you are specifiyng that the JVM should start with an heap of 128 Mb and may allocate up to 1024 Mb when required as per using the flag `-Xmx1024m`. You might want to try to allocate more objects and place those calls in different parts of your program. – João Rebelo Jan 09 '19 at 11:28

2 Answers2

4

Please see detail:

  1. I use apache-tomcat-9.0.14-windows-x64.zip
  2. Sure use right java version
  3. Edit catalina.bat add set JAVA_OPTS=-Xms128m -Xmx1024m after setlocal
  4. Start Tomcat with startup.bat
  5. Check JVM with: start jconsole, select Tomcat, select insecure
  6. See XMS, XMX

If you want start Tomcat9w.exe, you need install service

  • Thank you man, now it works. The problem was that I didn't launch Tomcat calling catalina.bat, I was launching Tomcat as windows service. If I launch Tomcat calling catalina.bat it works both editing catalatina.bat and creating setenv.bat. Thank you :) – MDP Jan 10 '19 at 08:58
  • Instead of posting image links, add the code snippets – Rushikesh Gaidhani Nov 12 '21 at 04:37
0

Please set in catalina.bat file: Screen shot of catalina.bat file

  • Hi @Đức Trần Trung, I tried also your solution, but it still don't work. I'm able to change the heap memory by opening the program Tomcat9w.exe, but I'm not able to do it by editing catalina.bat – MDP Jan 09 '19 at 11:04
  • Instead of posting the image link of the file, you can add the necessary file content in the answer itself – Rushikesh Gaidhani Nov 12 '21 at 04:34