0

I have an applet that checks a website for updates, it does so by loading the website's HTML into a String object, then searching that String for a pattern, however because the website takes a considerable amount of space I'm already noticing that the App is growing in its memory usage with every scan. I assume this is because rather than rewriting the contents of the old string it is creating a new one and doing I don't know what with the old one, but not deleting it.

Here's the code I wrote

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Scanner;

/**
 * Created by zaers on 15-Apr-16.
 */
public class PageSearch {
    public static void main(String[] args){
        while(true) {
            try {
                Thread.sleep(60000);
           } catch (InterruptedException e) {
                System.exit(1); //OS screwed our program
            }
            try {
                String out = new Scanner(new     URL("http://blog.dota2.com/").openStream(), "UTF-8").useDelimiter("\\A").next();
                if(out.contains("6.87")){
                    java.awt.Desktop.getDesktop().browse(new     URI("http://www.youtube.com/watch?v=-kcOpyM9cBg&t=7m21s"));
                    break;
                }
                else continue;
            } catch (IOException e) {
                System.exit(2);   //Blog is kill
            } catch (URISyntaxException e) {
                System.exit(3); //YouTube is kill
            }
        }
    }
}

I noticed this because on first run the app takes 8.5mb, however it is currently at 27.8 and growing with every check.

Scy
  • 245
  • 1
  • 12

1 Answers1

0

Set the XmX to something you are comfortable with. The Xmx=B flag tells your JVM to use a maximum amount of B memory, it will trigger the GC when you hit that amount of memory. Be careful with this, if you, through some kind of miracle, actually need more than Xmx and the JVM cannot allocate new memory, it will crash with an out of memory exception.

Only use this if you are already properly cleaning up after yourself!

More info:

Xmx and Xms

TWT
  • 476
  • 3
  • 9