Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put tomcat7 always online?

I have Ubuntu 14.04 cloud server with 512MB RAM on Digital Ocean and installed tomcat7 in order to accept my Java applications, also there is a wordpress site running on it with little accesses. So, I created a REST Web Service that needs to always be online because there are accesses by Android Apps. The problem is when I don't use the WS for sometime it goes down and I have to manually start tomcat again.

When I ask for tomcat' status I have the answer below:

Tomcat Servlet engine is not running, but pid file exists.

Here is a memory log of server in normal state:

         total       used       free     shared    buffers     cached
Mem:       490        480          9         64          6        119
-/+ buffers/cache:    354        135
Swap:        0          0          0

Top command:

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 8835 www-data  20   0  363904  65472  37244 S 16.6 13.0   0:31.02 php5-fpm
12625 www-data  20   0  361052  63896  35704 S  8.3 12.7   0:13.30 php5-fpm
24655 mysql     20   0  891176  56332   1576 S  1.7 11.2  72:04.31 mysqld
11509 www-data  20   0  361696  65796  37168 S  1.3 13.1   0:16.99 php5-fpm
    7 root      20   0       0      0      0 S  0.3  0.0   4:31.17 rcu_sched
   28 root      20   0       0      0      0 S  0.3  0.0   0:44.41 kswapd0
  123 root      20   0       0      0      0 S  0.3  0.0   3:26.29 jbd2/vda1-8
  744 www-data  20   0   91112   2400    540 S  0.3  0.5   0:53.93 nginx
13305 tomcat7   20   0 1126588 144516   5792 S  0.3 28.8   0:44.17 java
14557 root      20   0   24820   1508   1100 R  0.3  0.3   0:00.07 top
    1 root      20   0   33504   1504    120 S  0.0  0.3   1:59.18 init
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.29 kthreadd
    3 root      20   0       0      0      0 S  0.0  0.0   0:03.83 ksoftirqd/0
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
    8 root      20   0       0      0      0 R  0.0  0.0   4:37.10 rcuos/0
    9 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
   10 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/0

Using the jmap -heap in tomcat process i have these details:

using thread-local object allocation.
Concurrent Mark-Sweep GC

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 134217728 (128.0MB)
   NewSize          = 1310720 (1.25MB)
   MaxNewSize       = 44695552 (42.625MB)
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 174063616 (166.0MB)
   G1HeapRegionSize = 0 (0.0MB)

Heap Usage:
New Generation (Eden + 1 Survivor Space):
   capacity = 2424832 (2.3125MB)
   used     = 280872 (0.26786041259765625MB)
   free     = 2143960 (2.0446395874023438MB)
   11.583152977195946% used
Eden Space:
   capacity = 2162688 (2.0625MB)
   used     = 242168 (0.23094940185546875MB)
   free     = 1920520 (1.8315505981445312MB)
   11.197546756628787% used
From Space:
   capacity = 262144 (0.25MB)
   used     = 38704 (0.0369110107421875MB)
   free     = 223440 (0.2130889892578125MB)
   14.764404296875% used
To Space:
   capacity = 262144 (0.25MB)
   used     = 0 (0.0MB)
   free     = 262144 (0.25MB)
   0.0% used
concurrent mark-sweep generation:
   capacity = 34521088 (32.921875MB)
   used     = 26207256 (24.993186950683594MB)
   free     = 8313832 (7.928688049316406MB)
   75.91665708798054% used
Perm Generation:
   capacity = 50319360 (47.98828125MB)
   used     = 43680848 (41.65730285644531MB)
   free     = 6638512 (6.3309783935546875MB)
   86.8072407916158% used

16661 interned Strings occupying 2074936 bytes.

Does anybody know how to always put it online?

like image 670
Marcelo Machado Avatar asked Nov 27 '25 06:11

Marcelo Machado


1 Answers

OK, if you have a 512 MB RAM server, and you have MySQL and PHP5 running, the JVM will probably have crashed with an OutOfMemory exception.

In the jmap output, the only important number is the free memory of the concurrent mark sweep generation, where you have only 7.9 MB free, which sounds very small for a web service.

Before it crashes, the JVM will also spend a lot of time trying to garbage collect, which could lead to the process becoming non-responsive, even before it crashes completely.

You could add 1GB of swap (IIRC, linux admins recommend swap = 2 x ram). See e.g. http://www.prowebdev.us/2012/05/amazon-ec2-linux-micro-swap-space.html for AWS, will probably work on Digital Ocean, too.

The MySQL and PHP5 processes can probably swap out a lot of unused allocated memory. If that slows your applications down too much, you'll probably need some more RAM, or move the PHP and MySQL to different servers.

like image 171
GeertPt Avatar answered Nov 28 '25 20:11

GeertPt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!