You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Carlos Rule <ca...@cabinetuk.com> on 2005/04/13 16:10:01 UTC

Tomcat memory alert

Hello all, newbie here.

Does anyone know if there is a reliable way of setting up an alert to tell
me whenever the Tomcat (5.0.25) process reaches a certain level of memory
usage? 

Many thanks!

Carlos

 


Re: Tomcat memory alert

Posted by Ronaldo Arrudas <rd...@gmail.com>.
Hi carlos:

I think a nice way is to deploy a simple TimerTask inside any webapp to 
do this work. You could use anything like the following to help you:

import java.util.*;
public class MemoryWatchDog extends TimerTask{

    private static Timer currentTimer = null;

    public static void startTimer() {
            if(currentTimer != null)
                return;
            int seconds = 60;
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new WatchDog(), 1, (seconds * 1000));
            currentTimer = timer;
        }
    }

    public static void stopTimer() {
        if(currentTimer == null)
            return;
        currentTimer.cancel();
        currentTimer = null;
    }

    private WatchDog() {
        // Just for hide the constructor
    }

    public void run() {
        int long lowMemory = 5242880; // 5Mb
        if(Runtime.getRuntime().freeMemory()  <= lowMemory){
            // Using Jakarta Commons-Email to do the hard work...
            SimpleEmail email = new SimpleEmail();
            email.setHostName("mail.myserver.com");
            email.addTo("you@yourdomain.org","John Doe");
            email.setFrom("tomcat@somewhere.org", "Tomcat");
            email.setSubject("Warning!!!");
            email.setMsg("Look at me! I won't have enough memory in few 
seconds!!!"
                                       + "\n Free memory (bytes): "
                                       + Runtime.getRuntime().freeMemory());
            email.send();
        }
    }
}

[]'s
Ronaldo Arrudas
rdarrudas@gmail.com


Carlos Rule wrote:

>Hello all, newbie here.
>
>Does anyone know if there is a reliable way of setting up an alert to tell
>me whenever the Tomcat (5.0.25) process reaches a certain level of memory
>usage? 
>
>Many thanks!
>
>Carlos
>
> 
>
>
>  
>