斯坦福核心自然语言处理 - 主线程中的异常java.lang.OutOfMemoryError:Java堆空间不足。

4
我正在尝试运行此网站上提供的简单程序:https://stanfordnlp.github.io/CoreNLP/api.html
我的程序
import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.FileNotFoundException;  
import java.io.FileReader;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.io.PrintWriter;  
import java.util.List;  
import java.util.Properties;  

import edu.stanford.nlp.ling.CoreAnnotations.NamedEntityTagAnnotation;  
import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation;  
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;  
import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation;  
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;  
import edu.stanford.nlp.ling.CoreLabel;  
import edu.stanford.nlp.pipeline.Annotation;  
import edu.stanford.nlp.pipeline.StanfordCoreNLP;  
import edu.stanford.nlp.util.CoreMap;  

public class StanfordClass {

    public static void main(String[] args) throws Exception {
     Properties props = new Properties();
      props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse");

        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        String text = "What is the Weather in Mumbai right now?";
         Annotation document = new Annotation(text);
          pipeline.annotate(document);

        List<CoreMap> sentences = document.get(SentencesAnnotation.class);

       for(CoreMap sentence: sentences) {
          // traversing the words in the current sentence
          // a CoreLabel is a CoreMap with additional token-specific methods
          for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
            // this is the text of the token
            String word = token.get(TextAnnotation.class);
            // this is the POS tag of the token
            String pos = token.get(PartOfSpeechAnnotation.class);
            // this is the NER label of the token
            String ne = token.get(NamedEntityTagAnnotation.class);

            System.out.println(String.format("Print: word: [%s] pos: [%s] ne: [%s]",word, pos, ne));
          }
        }
    }
}  

但是,出现了“主线程中的异常 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ”。

我的尝试
1. 如果我从上面的代码中删除 ner(命名实体识别器)属性,即 props.setProperty("annotators", "tokenize, ssplit, pos, lemma, parse");,那么代码就可以正常运行。
2. 但是我需要 ner(命名实体识别器),因此我在 eclipse.ini 文件中增加堆大小到 1g,并确信这个大小足以支持该程序,也确信堆大小不是问题。我认为还有一些东西缺失,但是没有找到是什么。


你有检查哪些方法占用了更多的内存吗? - meditat
我应该如何检查? - jitendra korde
这适用于Java 5,但我正在使用Java 8,并且1GB的堆大小对于这个简单的程序来说太多了。 - jitendra korde
哦,对不起,我忘记使用这个 http://www.khelekore.org/jmp/tijmp/。 - meditat
我使用了Eclipse内存分析器,没有与内存相关的问题。 - jitendra korde
1个回答

1

在进行了大量搜索后,我在这里找到了答案使用Stanford CoreNLP

使用以下答案:
1. Windows -> Preferences
2. Java -> Installed JREs
3. 选择JRE并单击编辑
4. 在默认的VM参数字段中,键入“-Xmx1024M”。 (或者您的内存偏好,对于1GB的RAM,它是1024)
5. 单击完成或确定。


在它正常工作之前,我必须将堆空间设置为4 GB,即“-Xmx4096M”。但是,之后它就可以了! :) 在任务管理器(Windows 10)中观察Eclipse,内存占用量在演示程序完成之前达到了5.9 GB。毫不奇怪,自然语言处理需要大量的RAM。 - LionelGoulet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接