Dear Daniweb Team,

I face memory leaking problem while sending mail automatically when user advertisement expire date(i.e,26 days) was expire..Mail functionality was working when it expiry days come to 26.But it shows following error.

It shows an Error Like:: pergem space out of memory leaking.

I request u people past one week on words i faced this problem.due to this again and again My server was going down.

Any one of them try to under standard my problem please give me any suggestion.This my code please see my code and give me solution for this ASAP...

public class SendAutoEmail extends TimerTask {

    public SendAutoEmail(ServletContext context) {
        this.context = context;
       }
    //*************************************************************************************************************************************
    public void run() {
        System.out.println("okkk");
        Calendar cal = Calendar.getInstance();
        System.out.println("Calender Instances:" + cal);
        if (!isRunning) {
            advertiseUs a = new advertiseUs();
            List lis = a.displayActiveAdvertiseWithUs();
            Iterator itr = lis.iterator();
            while (itr.hasNext()) {
                Vbadvertiseus ad = (Vbadvertiseus) itr.next();
                to = ad.getEmail();
                name = ad.getFirstName();
                upDateday = ad.getUpdateDate();
                //milis1 = cal.getTimeInMillis();
                //long diff = milis1 - milis2;
                //diffDays = diff / (24 * 60 * 60 * 1000);
                //System.out.println("diffDays"+diffDays);
                System.out.println("to" + to);
                System.out.println("Diference in days" + (cal.getTimeInMillis() - upDateday.getTime()) / (24 * 60 * 60 * 1000));
                context.log("############ Not equal to specified Date ##############");
                context.log("############ cal.get(Calendar.min) = " + cal.get(Calendar.HOUR_OF_DAY) + "#########");
                long val = (cal.getTimeInMillis() - upDateday.getTime()) / (24 * 60 * 60 * 1000);
                //System.out.println("val is "+val);
                if (val == 26)
                {
                    System.out.println("Mail Was Sending" + val);
                    context.log("########## Equal to specified Date ##############");
                    isRunning = true;
                    //**************************Write Email Functionality Here*************************************
                    Properties props = null;
                    Transport t;
                    result = 0;
                    
                    try {
                        /******************* DO NOT MAKE ANY CHANGES ON THIS JAVA CLASS ******************/
                        String host = "";//here am give my host name
                        String port = "";//here am give my port number
                        String from = "XXX@YYY.com";
                        String username = "mailer@YYY.com";
                        String password = "rg^fg8sc";
                        String body = "Dear" + name;
                        String subject = "Expiry Date Is Over";
                        props = System.getProperties();
                        props.put("mail.smtp.host", host);
                        props.put("mail.smtp.port", port);
                        props.put("mail.smtp.auth", "true");
                        props.put("mail.smtp.user", username);
                        props.put("mail.smtp.password", password);
                        props.put("mail.debug", "true");
                        Session session = Session.getInstance(props, null);
                        MimeMessage message = new MimeMessage(session);
                        Calendar rightNow = Calendar.getInstance();
                        message.setSentDate(rightNow.getTime());
                        if (to != null) {
                            Address[] toUser = InternetAddress.parse(to);
                            message.addRecipients(Message.RecipientType.TO, toUser);
                        }
                        message.setFrom(new InternetAddress(from));
                        message.setSubject(subject);

                        message.setContent(body, "text/html");
                        message.saveChanges();

                        t = session.getTransport("smtp");
                        t.connect(host, username, password);
                        t.sendMessage(message, message.getAllRecipients());

                        if (t != null) {
                            result = 1;
                        } else {
                            result = 0;
                        }
                    } catch (Exception e) {
                        System.out.println("Error sending ExpiryDate mail : " + e);
                    }
                    context.log("######## Expiry Status=" + result + "##########");
                    isRunning = false;
                }
            }
        } else {
            context.log("the prevenient task is still running!");
        }
    }

Try increasing your permgen space. (Google that and see your server documentation as to where to make the change.)

Another thing to look at is to read the documentation for the libraries you're using and see if they intern strings, and if so what strings. If the strings they intern are things that will constantly be changing in your program then you are going to want to not load those libraries on the application classloader, but rather to create a classloader where those are used and unload that classloader after they have been used (which will then remove those interned strings). This will affect your performance some (not as much as you might think unless you are sending many thousands of mails per minute), but it will also preserve your permgen space.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.