【问题标题】:Stanford-CoreNLP doesn't empty Memory after running on ThreadsStanford-CoreNLP 在线程上运行后不会清空内存
【发布时间】:2017-09-24 13:39:58
【问题描述】:

在我的系统上运行 Stanford CoreNLP 时,它似乎并没有清空内存。 即使使用线程...
我有 2 个类 Testx.java (包含主线程)和 Testx2.java 其中 实现 Runnable。

我想做的是在 String no 上运行 Stanford CoreNLP 后完全清空内存。 1 如下代码所示...

我知道这是可以做到的!因为我之前在处理它时已经看到内存使用率下降(但我没有备份该代码!:/)
VM 参数是 -Xmx2048m

public class Testx {
    public static void main(String[] args) {

    String text = "If you had to guess the top city for entertainment & media your first thought would probably be LA.";

    Textx2 x = new Textx2(text);
    Thread t1 = new Thread(x);  
    t1.run();
    t1.interrupt();

Memory usage after t1 has finished
// 如何在继续下一个字符串之前完全清空此处的内存?

    String text2 = "Taylor Swift has a certain attachment to the number 1989 it's the year of her birth.";

    Textx2 x2 = new Textx2(text2);
    Thread t2 = new Thread(x2);  
    t2.run();
    t2.interrupt();
}

Testx2.java 代码。

String text;

public Textx2(String text) {
    this.text = text;
}

@Override
public void run() {

            Properties props = new Properties();
            Annotation document = new Annotation(text);
            props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, depparse, sentiment, mention, dcoref, natlog, relation, entitymentions, openie");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
            pipeline.annotate(document);

}

java memory usage

【问题讨论】:

    标签: java eclipse multithreading memory-management stanford-nlp


    【解决方案1】:

    在两个线程都完成后尝试运行此行:

    StanfordCoreNLP.clearAnnotatorPool();
    

    【讨论】:

    • 补充一点:注释器存储在SoftReferences 中,这意味着虽然它们会显示为已用内存,但它们会在程序遇到 OOM 错误之前被垃圾收集。
    • @StanfordNLPHelp StanfordCoreNLP.clearAnnotatorPool();将删除管道!再次实例化它会占用更多内存。尝试将 2 个线程放在 while(true) 循环中并检查内存使用量将如何增长...
    • @GaborAngeli 我正在从包含 1000 个(长)字符串的数据库中获取 text1text2。我将它们放入 while(true) 循环 并在每个循环上运行管道。当获得一个非常大的字符串时会发生什么?在t1.run(); t1.interrupt(); 之后,带注释的字符串会在启动线程 t2 之前被垃圾收集吗?我也在使用 16GB 的 RAM
    • 我尝试运行这个int i = 0; while(i<=4) { Run the 2 Threads i++; },在它完成 5 轮后,我检查了内存并且它保持在一个级别(比如 6GB 左右)我不明白为什么它没有回到原来的状态基线!?
    • 您看到的这种内存下降的大小几乎可以肯定是由于注释器正在收集垃圾,而不是来自您正在注释的文本。如果您运行clearAnnotatorPool()Runtime.getRuntime().gc(),您应该会看到内存使用量相对一致地下降。关于软引用:如果您的内存少于注释文档所需的内存+保留管道,则确实存在颠簸情况,但使用 4+ GB 您应该是安全的。
    猜你喜欢
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    相关资源
    最近更新 更多