0

I'm trying to execute a java script on a large text file, but it gives me this error

     java.lang.OutOfMemoryError: Java heap space

I tried to do that on my netbeans project:

Project Properties -> Run -> VM Options -> -Xmx2048m -Xms1024m

I tried also :

System -> Programs -> Java -> Java -> View -> Execution Parameters -> -Xincgc -Xmx2048M

But i didn't solve the problem .. any suggestions please ?

Nad
  • 35
  • 2
  • 10
  • duplicate of this http://stackoverflow.com/questions/15460779/how-to-increase-the-java-heap-size-in-netbeans – Pavneet_Singh Sep 29 '16 at 07:38
  • i tried all solutions there but i have the same problem, i have a text file with 3 million sentences – Nad Sep 29 '16 at 07:40
  • read this http://stackoverflow.com/questions/1565388/increase-heap-size-in-java?rq=1 – Pavneet_Singh Sep 29 '16 at 07:43
  • Rewrite java script (what is java script by the way) in more efficient manner. – Mikhail Kuchma Sep 29 '16 at 07:43
  • I tried this program https://sites.google.com/site/nirajatweb/home/technical_and_coding_stuff/sentence-clustering-by-using-group-average-agglomerative-clustering – Nad Sep 29 '16 at 07:44
  • Did you increase the memory of NetBeans? (Not sure about what is exactly the case.) **OutOfMemory** can have several causes, so inspect the **stacktrace** to find a guilty party. Try less data, and NetBeans has a nice **profiler**. Also do **Source / Inspect** and let run **FindBugs** over your source. It can find **memory leaks** and similar issues too. And then there is showing code. – Joop Eggen Sep 29 '16 at 07:48
  • http://javascriptisnotjava.io – Bálint Sep 29 '16 at 07:49
  • I tried with less data, and it works – Nad Sep 29 '16 at 07:50
  • duplicate of http://stackoverflow.com/questions/37335/how-to-deal-with-java-lang-outofmemoryerror-java-heap-space-error-64mb-heap – Ketan G Sep 29 '16 at 08:24

2 Answers2

0

String storage can be quite expensive. Using random String of avarage ~ 80 chars long, 3 000 000 strings can cosume almoust 1GB

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    String base = RandomStringUtils.random(80);
    long i = 0;
    try {
        while (i < 3e6) {
            list.add(base + i++);
        }
    } finally {
        System.out.println("Count:" + list.size());
        System.out.println("Memory:" + Runtime.getRuntime().totalMemory() / 1024d / 1024d);
    }
}

Output: Count:3000000 Memory:981.5

Try to wrap your piece of code where you are reading your file with the same try-finally block i have made here. When error will popout, you will get approx ammount of memory you are consuming at that point, and what percentage of file you have managed already to read. This will get you the idea of how much more memory you will need.

Moreover you will know if your -Xmx directive works, because if it will crash around lets say 500mb it is either ommited by netbeans, or there is no mo available memory it the system.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

I would suggest altering your code, the other alternative is described in here: http://wiki.netbeans.org/FaqSettingHeapSize

PlsWork
  • 1,958
  • 1
  • 19
  • 31