问题描述
我正在编写一个项目(用Java编写),其中教授说我们不允许使用超过200m
我将堆栈内存限制为50m(只是为了绝对肯定)使用-Xmx50m,但根据顶部,它仍然使用300m
I have a project I'm writing (in Java) for a class where the prof says we're not allowed to use more than 200mI limit the stack memory to 50m (just to be absolutely sure) with -Xmx50m but according to top, it's still using 300m
我试过运行它只报告26m
I tried running Eclipse Memory Analyzer and it reports only 26m
这可能都是堆栈中的内存吗?我很确定我再也不会超过大约300个方法调用深度(是的,它是一个递归DFS搜索),所以这将意味着每个堆栈帧使用几乎一兆字节似乎很难相信。
Could this all be memory on the stack?, I'm pretty sure I never go further than about 300 method calls deep (yes, it is a recursive DFS search), so that would have to mean every stack frame is using up almost a megabyte which seems hard to believe.
该程序是单线程的。有谁知道我可能减少内存使用的任何其他地方?另外,我如何检查/限制堆栈使用的内存量?
The program is single-threaded. Does anyone know any other places in which I might reduce memory usage? Also, how can I check/limit how much memory the stack is using?
更新:我现在使用以下JVM选项但没有效果(仍然约300米)到顶部): -Xss104k -Xms40m -Xmx40m -XX:MaxPermSize = 1k
UPDATE: I'm using the following JVM options now with no effect (still about 300m according to top): -Xss104k -Xms40m -Xmx40m -XX:MaxPermSize=1k
另一个更新:实际上,如果我让它运行一点点(带有所有这些选项)大约一半的时间它在4或5秒后突然下降到150米(另一半不会下降)。这真的很奇怪的是我的程序没有随机(正如我所说的那样是单线程)所以没有理由它在不同的运行中表现不同
Another UPDATE: Actually, if I let it run a little bit longer (with all these options) about half the time it suddenly drops to 150m after 4 or 5 seconds (the other half it doesn't drop). What makes this really strange is that my program has no stochastic (and as I said it's single-threaded) so there's no reason it should behave differently on different runs
可能吗与我正在使用的JVM有关?
Could it have something to do with the JVM I'm using?
java version "1.6.0_27"
OpenJDK Runtime Environment (IcedTea6 1.12.3) (6b27-1.12.3-0ubuntu1~10.04)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
根据java -h,默认的JVM是-server。我尝试添加-cacao和现在(所有其他选项)它只有59米。所以我想这解决了我的问题。谁能解释为什么这是必要的?另外,我应该知道有什么缺点吗?
According to java -h, the default JVM is -server. I tried adding -cacao and now (with all the other options) it's only 59m. So I suppose this solves my problem. Can anyone explain why this was necessary? Also, are there any drawbacks I should know about?
还有一个更新:与服务器相比,可可真的很慢。这是一个糟糕的选择
One more update: cacao is really really slow compared to server. This is an awful option
推荐答案
Top命令反映了Java应用程序使用的总内存量。其中包括:
Top command reflects the total amount of memory used by the Java application. This includes among other things:
- JVM本身的基本内存开销
- 堆空间(以-Xmx为界)
- 永久生成空间(-XX:MaxPermSize - 并非所有JVM中的标准)
- 线程堆栈空间(-Xss)每个堆栈)可能会根据线程数量显着增长
- 本机分配使用的空间(使用ByteBufer类或JNI)
- A basic memory overhead of the JVM itself
- the heap space (bounded with -Xmx)
- The permanent generation space (-XX:MaxPermSize - not standard in all JVMs)
- threads stack space (-Xss per stack) which may grow significantly depending on the number of threads
- Space used by native allocations (using ByteBufer class, or JNI)
这篇关于Java使用的内存远远多于使用-Xmx分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!