Skip to content

OutOfMemoryError #1190

Open
Open

Description

@TolstoyDotCom

I'm starting a simple spider with about 800 megabytes of heap space, but, after running for a day or so, it throws a series of OutOfMemoryError. Example:

Exception in thread "pool-1-thread-97" java.lang.OutOfMemoryError: Java heap space
Exception in thread "pool-1-thread-92" java.lang.OutOfMemoryError: Java heap space
Exception in thread "pool-1-thread-99" java.lang.OutOfMemoryError: Java heap space
Exception in thread "pool-1-thread-88" java.lang.OutOfMemoryError: Java heap space
Exception in thread "pool-1-thread-101" java.lang.OutOfMemoryError: Java heap space
Exception in thread "pool-1-thread-100" java.lang.OutOfMemoryError: Java heap space
Exception in thread "pool-1-thread-103" java.lang.OutOfMemoryError: Java heap space
Exception in thread "pool-1-thread-94" java.lang.OutOfMemoryError: Java heap space
Exception in thread "pool-1-thread-105" java.lang.OutOfMemoryError: Java heap space

Here's a version of the code I'm using. The else branch is a little more complicated but it only involves System.out.println, it doesn't write to a database or similar.

import us.codecraft.webmagic.*;
import us.codecraft.webmagic.processor.*;

public class App implements PageProcessor {
	private Site site = Site.me().setRetryTimes( 3 ).setSleepTime( 1000 );

	@Override
	public void process( Page page ) {
		List<String> links = page.getHtml().links().all();
		page.addTargetRequests( links );
		page.putField( "generator", page.getHtml().xpath( "/html/head/meta[@name=\"generator\"]/@content" ).toString() );
		String generator = page.getResultItems().get( "generator" );

		if ( generator == null ) {
			page.setSkip( true );
		}
		else {
			System.out.println( generator );
		}
	}

	@Override
	public Site getSite() {
		return site;
	}

	public static void main( String[] args ) {
		System.setProperty( "slf4j.internal.verbosity", "WARN" );
		Spider.create( new App() ).addUrl( "https://github.com/code4craft/webmagic/" ).thread( 5 ).run();
	}
}

Activity

  1. TolstoyDotCom commented on May 11, 2025

    @TolstoyDotCom
    Author

    Also, this is on AWS and for java -version I get:

    openjdk version "11.0.27" 2025-04-15
    OpenJDK Runtime Environment (build 11.0.27+6-post-Ubuntu-0ubuntu122.04)
    OpenJDK 64-Bit Server VM (build 11.0.27+6-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)
    
  2. sutra commented on May 12, 2025

    @sutra
    Collaborator

    Increase the value of the -Xmx JVM argument.

  3. TolstoyDotCom commented on May 12, 2025

    @TolstoyDotCom
    Author

    I'm already setting it to 812 megabytes, how much does this need?

    This is the command line:

    nohup java -Xmx812m -jar spider-1.0-SNAPSHOT-jar-with-dependencies.jar > log.log &

    It's writing things to log.log which I process using PHP. The Java app isn't connecting to a database or an external API or anything, it's just writing to a file.

  4. sutra commented on May 12, 2025

    @sutra
    Collaborator

    It depends, have you invoked us.codecraft.webmagic.scheduler.Scheduler.push(Request, Task) to add new task? It should use us.codecraft.webmagic.scheduler.QueueScheduler which stores task in BlockingQueue uses the memory by default.

  5. TolstoyDotCom commented on May 12, 2025

    @TolstoyDotCom
    Author

    I'm using the code above and I'd rather not change it because if it gets stuck I can just start over with a different starting point.

    I raised the Xmx value to 1.5 gigabytes and it hasn't run out of memory yet.

    However, before that it had stopped for a few hours after the last message here:

    May 12, 2025 12:21:18 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {s}->https://workspaceupdates.googleblog.com:443
    May 12, 2025 12:25:28 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {s}->https://www.google.com:443: The target server failed to respond
    May 12, 2025 12:25:34 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {s}->https://www.google.com:443
    May 12, 2025 12:27:30 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://goo.gl:443: Broken pipe (Write failed)
    May 12, 2025 12:27:54 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {s}->https://goo.gl:443
    May 12, 2025 12:32:47 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://support.google.com:443: Broken pipe (Write failed)
    May 12, 2025 12:32:47 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://domains.google.com:443: Broken pipe (Write failed)
    May 12, 2025 12:33:13 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://developers.google.com:443: Broken pipe (Write failed)
    May 12, 2025 12:33:37 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {s}->https://support.google.com:443
    May 12, 2025 12:33:37 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {s}->https://domains.google.com:443
    May 12, 2025 12:33:48 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {s}->https://developers.google.com:443
    May 12, 2025 12:38:18 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://support.google.com:443: Broken pipe (Write failed)
    May 12, 2025 12:38:44 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {s}->https://support.google.com:443
    May 12, 2025 12:38:44 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://workspaceupdates.googleblog.com:443: Broken pipe (Write failed)
    May 12, 2025 12:38:44 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://workspaceupdates.googleblog.com:443: Broken pipe (Write failed)
    May 12, 2025 12:38:44 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://workspaceupdates.googleblog.com:443: Broken pipe (Write failed)
    May 12, 2025 12:38:53 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {s}->https://workspaceupdates.googleblog.com:443
    May 12, 2025 12:38:53 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {s}->https://workspaceupdates.googleblog.com:443
    May 12, 2025 12:38:53 PM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {s}->https://workspaceupdates.googleblog.com:443
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions