java 给我的 jar 文件更多的内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14596970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:46:48  来源:igfitidea点击:

Give more memory to my jar file

javamultithreadingweb-crawler

提问by Alireza Noori

I have a multithreaded crawler. In this program, if I load a lot of seeds, I get an error. I saw the java.lang.OutOfMemoryErrorand thought maybe the memory is not enough. I tried running the crawler.jarfile with these arguments: java -Xms512m -Xmx3G -jar crawler.jarbut so far, no luck.

我有一个多线程爬虫。在这个程序中,如果我加载了很多种子,就会出现错误。我看到了java.lang.OutOfMemoryError,想可能是内存不够。我尝试crawler.jar使用这些参数运行文件:java -Xms512m -Xmx3G -jar crawler.jar但到目前为止,没有运气。

This is the StackTrace of the program:

这是程序的 StackTrace:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:597)
        at com.sleepycat.je.utilint.DaemonThread.runOrPause(DaemonThread.java:99)
        at com.sleepycat.je.dbi.EnvironmentImpl.runOrPauseDaemons(EnvironmentImpl.java:772)
        at com.sleepycat.je.dbi.EnvironmentImpl.envConfigUpdate(EnvironmentImpl.java:717)
        at com.sleepycat.je.dbi.EnvironmentImpl.finishInit(EnvironmentImpl.java:579)
        at com.sleepycat.je.dbi.DbEnvPool.getEnvironment(DbEnvPool.java:204)
        at com.sleepycat.je.Environment.makeEnvironmentImpl(Environment.java:230)
        at com.sleepycat.je.Environment.<init>(Environment.java:212)
        at com.sleepycat.je.Environment.<init>(Environment.java:166)
        ...

Is this related to memory as I suspected? Does adding the -Xms512m -Xmx3Gwork when I'm running the jar file using java -jar?

这与我怀疑的记忆有关吗?-Xms512m -Xmx3G当我使用 运行 jar 文件时添加工作java -jar吗?

I ran task manager (I'm running on Windows Server) but after running the app, the memory doesn't go that far higher! Am I wrong?

我运行了任务管理器(我在 Windows Server 上运行)但是在运行应用程序后,内存并没有变得更高!我错了吗?

回答by ericson

The -Xms512m -Xmx3Goption only effect the heap size of JVM and would not solve your problem.

-Xms512m -Xmx3G选项只影响JVM的堆大小,不会解决你的问题。

However, the default thread number limits should be enough in most scenarios. You can increase the limit by tweaking JVM/System options, but no matter how many thread you create, the capacity of your system is bounded to your computer resource. e.g. cpu, memory, network, etc.

但是,默认线程数限制在大多数情况下应该足够了。您可以通过调整 JVM/系统选项来增加限制,但无论您创建多少线程,您的系统容量都受限于您的计算机资源。例如 CPU、内存、网络等。

I suggest to solve this problem from a different view:

我建议从不同的角度解决这个问题:

  • Try to share the Environment instances among the worker threads.
  • Try to control the concurrent level to a rate in which your system is busy and thread number limits is not touched.
  • 尝试在工作线程之间共享 Environment 实例。
  • 尝试将并发级别控制为系统繁忙且不触及线程数限制的速率。

回答by JayTee

These kind of errors can often be solved with some simple profiling. The JDK ships with jvisualvm.

这些类型的错误通常可以通过一些简单的分析来解决。JDK 附带 jvisualvm。

E.g. C:\Program Files\Java\jdk1.7.0_11\bin\jvisualvm.exe

例如 C:\Program Files\Java\jdk1.7.0_11\bin\jvisualvm.exe

You'll be able to see a graph of your heap size and it's also worth checking the PermGen space.

您将能够看到堆大小的图表,并且还值得检查 PermGen 空间。

If this dosen't reveal anything, try adding -XX:-HeapDumpOnOutOfMemoryError when you run your jar and then load the heap dump into Eclipse Memory Analyzer.

如果这没有显示任何内容,请尝试在运行 jar 时添加 -XX:-HeapDumpOnOutOfMemoryError,然后将堆转储加载到 Eclipse Memory Analyzer 中。

回答by mikeslattery

You are spawning more threads than Java can take. Try shrinking the stack size with this JVM parameter: -Xss128k

您产生的线程数超过了 Java 的承受能力。尝试使用此 JVM 参数缩小堆栈大小:-Xss128k

If that isn't it, maybe you are hitting on OS limit. If you are using Linux add a line like this to ~/.profile: ulimit -u 4096

如果不是这样,也许您正在达到操作系统限制。如果您使用的是 Linux,请将这样的一行添加到~/.profileulimit -u 4096

There are many reasonswhy this might occur depending on your code and the OS you use.

发生这种情况的原因很多,具体取决于您的代码和您使用的操作系统。