简体   繁体   English

在 Eclipse 中增加堆空间:(java.lang.OutOfMemoryError)

[英]Increasing heap space in Eclipse: (java.lang.OutOfMemoryError)

try {
    // CompareRecord record = new CompareRecord();
    Connection conn = new CompareRecord().getConection("eliteddaprd","eliteddaprd","192.168.14.104","1521");
    ResultSet res = null;

    if (conn != null){
        Statement stmt = conn.createStatement();
        res = stmt.executeQuery("select rowindx,ADDRLINE1 from dedupinitial order by rowindx");
    }

    Map<Integer,String> adddressMap = new LinkedHashMap<Integer, String>();
    if (res != null){
        System.out.println("result set is not null ");
        while(res.next()){
            adddressMap.put(res.getInt(1),res.getString(2));
        }
    }

    System.out.println("address Map size =========> "+adddressMap.size());
    Iterator it = adddressMap.entrySet().iterator();
    int count = 0;
    int min = 0;

    while (it.hasNext()){
        Map.Entry pairs = (Map.Entry)it.next();
        Pattern p = Pattern.compile("[,\\s]+");
        Integer outerkey = (Integer)pairs.getKey();
        String outerValue = (String)pairs.getValue();
        //System.out.println("outer Value ======> "+outerValue);

        String[] outerresult = p.split(outerValue);
        Map.Entry pairs2 = null;
        count++;
        List<Integer> dupList = new ArrayList<Integer>();

        Iterator innerit = adddressMap.entrySet().iterator();
        boolean first = true;

        while (innerit.hasNext()){
            //System.out.println("count value ===> "+count);
            int totmatch = 0;
            if(first){
                if(count == adddressMap.size()){
                    break;
                }
                for(int i=0;i<=count;i++){
                    pairs2 = (Map.Entry)innerit.next();
                }
                first  = false;
            }
            else{
                pairs2 = (Map.Entry)innerit.next();
            }
            Integer innterKey = (Integer)pairs2.getKey();
            String innerValue = (String)pairs2.getValue();
            //System.out.println("innrer value "+innerValue);
            String[] innerresult = p.split(innerValue);

            for(int j=0;j<outerresult.length;j++){
                for(int k=0;k<innerresult.length;k++){
                    if(outerresult[j].equalsIgnoreCase(innerresult[k])){
                        //System.out.println(outerresult[j]+" Match With "+innerresult[k]);
                        totmatch++;
                        break;
                    }
                }
            }

            min = Math.min(outerresult.length, innerresult.length);
            if(min != 0 && ((totmatch*100)/min) > 50) {
                //System.out.println("maching inner key =========> "+innterKey);
                dupList.add(innterKey);
            }
        }
        //System.out.println("Duplilcate List Sisze ===================> "+dupList.size()+"   "+outerkey);
    }

    System.out.println("End  =========> "+new Date());
}
catch (Exception e) {
    e.printStackTrace();
}

Here ResultSet have processed around 500000 records, but it will give me error like:这里 ResultSet 已经处理了大约 500000 条记录,但它会给我这样的错误:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.HashMap.resize(HashMap.java:508)
    at java.util.LinkedHashMap.addEntry(LinkedHashMap.java:406)
    at java.util.HashMap.put(HashMap.java:431)
    at spite.CompareRecord.main(CompareRecord.java:91)

I know this error comes because of VM memory, but don't know how to increase it in Eclipse?我知道这个错误是由于 VM 内存引起的,但不知道如何在 Eclipse 中增加它?

What do I do if I have to process even more than 500,000 records?如果我必须处理超过 500,000 条记录,我该怎么办?

In Run->Run Configuration find the Name of the class you have been running, select it, click the Arguments tab then add:在 Run->Run Configuration 中找到您一直在运行的类的名称,选择它,单击 Arguments 选项卡,然后添加:

-Xms512M -Xmx1524M -Xms512M -Xmx1524M

to the VM Arguments section到 VM 参数部分

In the Eclipse download folder make the entries in the eclipse.ini file :在 Eclipse 下载文件夹中,在eclipse.ini文件中输入条目:

--launcher.XXMaxPermSize
512M
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms512m
-Xmx1024m

or what ever values you want.或者你想要的任何价值观。

See http://blog.headius.com/2009/01/my-favorite-hotspot-jvm-flags.htmlhttp://blog.headius.com/2009/01/my-favorite-hotspot-jvm-flags.html

-Xms and -Xmx set the minimum and maximum sizes for the heap. -Xms 和 -Xmx 设置堆的最小和最大大小。 Touted as a feature, Hotspot puts a cap on heap size to prevent it from blowing out your system.作为一项功能,Hotspot 设置了堆大小上限,以防止它耗尽您的系统。 So once you figure out the max memory your app needs, you cap it to keep rogue code from impacting other apps.所以一旦你确定了你的应用程序需要的最大内存,你就可以限制它以防止恶意代码影响其他应用程序。 Use these flags like -Xmx512M, where the M stands for MB.使用这些标志,例如 -Xmx512M,其中 M 代表 MB。 If you don't include it, you're specifying bytes.如果不包含它,则指定了字节。 Several flags use this format.有几个标志使用这种格式。 You can also get a minor startup perf boost by setting minimum higher, since it doesn't have to grow the heap right away.您还可以通过将最小值设置得更高来获得较小的启动性能提升,因为它不必立即增加堆。

-XX:MaxPermSize=###M sets the maximum "permanent generation" size. -XX:MaxPermSize=###M 设置最大“永久代”大小。 Hotspot is unusual in that several types of data get stored in the "permanent generation", a separate area of the heap that is only rarely (or never) garbage-collected. Hotspot 的不寻常之处在于,多种类型的数据存储在“永久代”中,这是堆的一个单独区域,很少(或从不)被垃圾收集。 The list of perm-gen hosted data is a little fuzzy, but it generally contains things like class metadata, bytecode, interned strings, and so on (and this certainly varies across Hotspot versions). perm-gen 托管数据的列表有点模糊,但它通常包含类元数据、字节码、内部字符串等内容(这肯定会因 Hotspot 版本而异)。 Because this generation is rarely or never collected, you may need to increase its size (or turn on perm-gen sweeping with a couple other flags).因为这一代很少或从不收集,您可能需要增加其大小(或使用其他几个标志打开永久代扫描)。 In JRuby especially we generate a lot of adapter bytecode, which usually demands more perm gen space.特别是在 JRuby 中,我们生成了大量的适配器字节码,这通常需要更多的永久空间。

How to give your program more memory when running from Eclipse从 Eclipse 运行时如何为程序提供更多内存

Go to Run / Run Configurations.转到运行/运行配置。 Select the run configuration for your program.为您的程序选择运行配置。 Click on the tab "Arguments".单击“参数”选项卡。 In the "Program arguments" area, add a -Xmx argument, for example -Xmx2048m to give your program a max.在“程序参数”区域中,添加-Xmx参数,例如-Xmx2048m为您的程序提供最大值。 of 2048 MB (2 GB) memory. 2048 MB (2 GB) 内存。

How to prevent this memory problem如何防止这个内存问题

Re-write your program in such a way that it does not need to store so much data in memory.以不需要在内存中存储这么多数据的方式重新编写您的程序。 I haven't looked at your code in detail, but it looks like you're storing a lot of data in a HashMap ;我没有详细查看您的代码,但看起来您在HashMap存储了大量数据; more than fits in memory when you have a lot of records.当你有很多记录时,超过内存。

You may want to close open projects in your Project Explorer.您可能希望关闭 Project Explorer 中打开的项目。 You can do this by right-clicking an open project, and selecting "Close Project".您可以通过右键单击打开的项目并选择“关闭项目”来完成此操作。

This is what worked for me after I noticed that I had to periodically increase the heap size in my_directory\\eclipse\\eclipse.ini.在我注意到我必须定期增加 my_directory\\eclipse\\eclipse.ini 中的堆大小后,这对我有用。

Hope this helps!希望这可以帮助! Peace!和平!

In eclipse.ini file, make below entries在 eclipse.ini 文件中,输入以下条目

-Xms512m -Xms512m

-Xmx2048m -Xmx2048m

-XX:MaxPermSize=1024m -XX:MaxPermSize=1024m

To increase the Heap size in eclipse change the eclipse.ini file.要增加 eclipse 中的堆大小,请更改 eclipse.ini 文件。

refer to参考

http://wiki.eclipse.org/FAQ_How_do_I_increase_the_heap_size_available_to_Eclipse%3F http://wiki.eclipse.org/FAQ_How_do_I_increase_the_heap_size_available_to_Eclipse%3F

You can increase the size of the memory through the use of commandline arguments.您可以通过使用命令行参数来增加内存的大小。

See this link .请参阅此链接

eclipse -vmargs -Xmx1024m

Edit: Also see see this excellent question编辑:另请参阅此优秀问题

I am not pro in Java but your problem can be solved by "blockingqueue" if you use it wisely.我不是 Java 专家,但如果你明智地使用它,你的问题可以通过“阻塞队列”来解决。

Try to retrieve a chunk of records first, process them, and iterate the process until you complete your processing.尝试首先检索一组记录,处理它们,然后迭代该过程,直到完成处理。 This may help you to get rid of the OutOfMemory Exceptions .这可能会帮助您摆脱OutOfMemory Exceptions

In Eclipse goto Run->Run Configuration find the Name of the class you have been running, select it, click the Target tab then in "Additional Emulator Command Line Options" add:在 Eclipse 中转到 Run->Run Configuration 找到您一直在运行的类的名称,选择它,单击 Target 选项卡,然后在“Additional Emulator Command Line Options”中添加:

-Xms512M -Xmx1524M -Xms512M -Xmx1524M

then click apply.然后点击应用。

转到“窗口 -> 首选项 -> 常规 -> C/C++ -> 代码分析”并禁用“语法和语义错误 -> 无法实例化抽象类”

Sometimes the exception will not stop after you increase the memory in eclipse ini file.有时在eclipse ini文件中增加内存后异常不会停止。 then try below option然后尝试以下选项

Go to Window >> Preferences >> MyEclipse >> Java Enterprise Project >> Web Project >> Deployment Tab Under -> Under Library Deployment Policies UnCheck -> Jars from User Libraries转到窗口 >> 首选项 >> MyEclipse >> Java Enterprise Project >> Web Project >> Deployment Tab Under -> Under Library Deployment Policies UnCheck -> Jars from User Libraries

The accepted solution of modifying a Run Configuration wasn't ideal for me as I have a few different run configurations and could easily forget to do this when adding further ones in future.修改运行配置的公认解决方案对我来说并不理想,因为我有几个不同的运行配置,并且在将来添加更多配置时很容易忘记这样做。 Also I wanted the setting to apply whenever running anything, eg when running JUnit tests by right-clicking and selecting "Run As" -> "JUnit Test".此外,我希望在运行任何东西时都应用该设置,例如通过右键单击并选择“运行方式”->“JUnit 测试”来运行 JUnit 测试时。

The above can be achieved by modifying the JRE/JDK settings instead:-以上可以通过修改 JRE/JDK 设置来实现:-

  1. Go to Window -> Preferences.转到窗口 -> 首选项。
  2. Select Java -> Installed JREs选择 Java -> 已安装的 JRE
  3. Select your default JRE (which could be a JDK) and click on "Edit..."选择您的默认 JRE(可能是 JDK)并单击“编辑...”
  4. Change the "Default VM arguments" value as required, eg -Xms512m -Xmx4G -XX:MaxPermSize=256M根据需要更改“默认 VM 参数”值,例如-Xms512m -Xmx4G -XX:MaxPermSize=256M

Please make sure your code is fine.请确保您的代码没问题。 I too got stuck in this problem once and tried the solution accepted here but in vain.我也曾经陷入这个问题,并尝试了此处接受的解决方案,但徒劳无功。 So I wrote my code again.所以我再次写了我的代码。 Apparently I was using a custom array list and adding the values from an array.显然我正在使用自定义数组列表并添加数组中的值。 I tried changing the ArrayList to accept the primitive values only and it worked.我尝试将 ArrayList 更改为仅接受原始值并且它起作用了。

Enter the MAT folder.进入 MAT 文件夹。 Click on the MemoryAnalyzer.ini file and change it from单击MemoryAnalyzer.ini文件并将其从

-vmargs
-Xmx1024m

to

-vmargs
-Xmx4096m

and restart the MAT application.并重新启动 MAT 应用程序。 It will resolve your problem.它将解决您的问题。

What to do if i have to processed even more that 500000 records ?如果我必须处理超过 500000 条记录怎么办?

There are a few ways, to increase the java heap size for your app where a few have suggested already.有几种方法可以增加应用程序的 Java 堆大小,其中一些方法已经提出了建议。 Your app need to remove the elements from your adddressMap as your app add new element into it and so you won't encounter oom if there are more records coming in. Look for producer-consumer if you are interested.您的应用程序需要从您的 adddressMap 中删除元素,因为您的应用程序将新元素添加到其中,因此如果有更多记录进入,您将不会遇到 oom。如果您有兴趣,请寻找生产者-消费者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM