我在文件中存储了一张格式为 Map 的 map 。 此文件有超过 100,000 条记录。
每条条目值(value)近10k。
我将 1000 条记录加载到内存中的 map 中,处理它们,然后清除 map 并加载接下来的 1000 条记录。
我的问题是:
由于字符串存储在 permgen 中的字符串池中 内存区域,当我清除 map 时,字符串将成为垃圾 Collection 了?
如果它们没有被垃圾回收,有没有办法强制 他们要被垃圾收集起来?
是否可以保证如果程序内存不足 ,JVM 会在抛出 OutOfMemory 之前清理 permGen 内存 异常?
最佳答案
好的..让我们开始....
Since the strings are stored in String pool which is in permgen memory area , when i clear the map will the Strings be garbage collected ?
所有字符串都不存储在字符串常量池中。只有 interned Strings 和 String literals 进入 String 常量池。 java-8 中没有permgen 的概念。 Metaspace 已经(几乎优雅地)取代了 Permgen。
如果您从文件(未驻留)中读取字符串,那么是的,您的字符串将被 GC 处理。如果你有字符串文字(如果你有,上帝保佑你......:P),当加载你定义这些字符串文字的类的类加载器被 GC 时,它们将被 GC。
Incase if they are not garbage collected is there any way to force them to be garbage collected?
好吧,您始终可以显式调用 System.gc()
(在生产环境中不是一个好主意)。如果您使用的是 java-8,请使用 G1Gc
并启用 String deduplication
。
Is there any guarantee that if the program is running out of memory , JVM would clean the permGen memory before throwing OutOfMemory Exception
GC 会尽最大努力尽可能多地清理。不,不能保证会发生这种情况。
关于java - 字符串和 Permgen 内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32247033/