gpt4 book ai didi

java - 限制具有指定内存量的java程序

转载 作者:行者123 更新时间:2023-11-30 06:36:47 25 4
gpt4 key购买 nike

如何设置java程序不消耗大量内存? System.out.println 是否不断增加内存消耗?

我使用 VisualVM 进行了分析,我不太了解如何修复部分代码。

最佳答案

使用 -Xmxn 标志,例如java -Xmx100m foofoo 限制为 100MB。

-Xmxn

Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is chosen at runtime based on system configuration. For more information, see HotSpot Ergonomics

Examples:

-Xmx83886080

-Xmx81920k

-Xmx80m

值得一读:http://javahowto.blogspot.com/2006/06/6-common-errors-in-setting-java-heap.html

经常使用 System.out.println() 不应增加内存使用量。每个调用都独立于下一个调用,它们不会建立已用内存的存储空间。

关于java - 限制具有指定内存量的java程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4355205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com