lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jiří Kuhn <k...@fg.cz>
Subject OutOfMemory example
Date Mon, 13 Sep 2004 13:06:31 GMT
Hi,

	I think I can reproduce memory leaking problem while reopening an index. Lucene version tested
is 1.4.1, version 1.4 final works OK. My JVM is:

$ java -version
java version "1.4.2_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
Java HotSpot(TM) Client VM (build 1.4.2_05-b04, mixed mode)

	The code you can test is below, there are only 3 iterations for me if I use -Xmx5m, the 4th
fails.

Jiri.

package test;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Run this test with Lucene 1.4.1 and -Xmx5m
 */
public class ReopenTest
{
    private static long mem_last = 0;

    public static void main(String[] args) throws IOException
    {
        Directory directory = create_index();

        for (int i = 1; i < 100; i++) {
            System.err.println("loop " + i);
            search_index(directory);
        }
    }

    private static void search_index(Directory directory) throws IOException
    {
        IndexReader reader = IndexReader.open(directory);
        Searcher searcher = new IndexSearcher(reader);

        print_mem("search 1");
        SortField[] fields = new SortField[2];
        fields[0] = new SortField("date", SortField.STRING, true);
        fields[1] = new SortField("id", SortField.STRING, false);
        Sort sort = new Sort(fields);
        TermQuery query = new TermQuery(new Term("text", "\"text 5\""));

        print_mem("search 2");
        Hits hits = searcher.search(query, sort);
        print_mem("search 3");

        for (int i = 0; i < hits.length(); i++) {
            Document doc = hits.doc(i);
            System.out.println("doc " + i + ": " + doc.toString());
        }
        print_mem("search 4");
        searcher.close();
        reader.close();
    }

    private static void print_mem(String log)
    {
        long mem_free = Runtime.getRuntime().freeMemory();
        long mem_total = Runtime.getRuntime().totalMemory();
        long mem_max = Runtime.getRuntime().maxMemory();

        long delta = (mem_last - mem_free) * -1;

        System.out.println(log + "= delta: " + delta + ", free: " + mem_free + ", used: "
+ 
	(mem_total-mem_free) + ", total: " + mem_total + ", max: " + mem_max);

        mem_last = mem_free;
    }

    private static Directory create_index() throws IOException
    {
        print_mem("create 1");
        Directory directory = new RAMDirectory();

        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(), true);
        for (int i = 0; i < 365 * 30; i++) {
            Document doc = new Document();

            doc.add(Field.Keyword("date", df.format(new Date(c.getTimeInMillis()))));
            doc.add(Field.Keyword("id", "AB" + String.valueOf(i)));
            doc.add(Field.Text("text", "Tohle je text " + i));
            writer.addDocument(doc);

            c.add(Calendar.DAY_OF_YEAR, 1);
        }
        writer.optimize();
        System.err.println("index size: " + writer.docCount());
        writer.close();

        print_mem("create 2");
        return directory;
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Mime
View raw message