Java堆溢出,强制垃圾回收 [英] Java Heap Overflow, Forcing Garbage Collection

查看:211
本文介绍了Java堆溢出,强制垃圾回收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有一系列子代的trie树.当删除一个单词时,我将子代设置为null,这将假定删除该节点(delete是一个相对术语).我知道null不会删除该子项,只是将其设置为null,这在使用大量单词时会导致堆溢出.

I've create a trie tree with an array of children. When deleting a word, I set the children null, which I would assume deletes the node(delete is a relative term). I know that null doesn't delete the child, just sets it to null, which when using a large amount of words it causes to overflow the heap.

在linux上运行一个顶层,我可以看到我的内存使用量很快达到1gb,但是如果在删除(Runtime.gc())之后强制执行垃圾回收,则内存使用量将达到50mb,并且永远不会超过该值.据我所知,java默认情况下会在堆溢出发生之前运行垃圾回收,但是我看不到会发生这种情况.

Running a top on linux, I can see my memory usage spike to 1gb pretty quickly, but if I force garbage collection after the delete (Runtime.gc()) the memory usage goes to 50mb and never above that. From what I'm told, java by default runs garbage collection before a heap overflow happens, but I can't see to make that happen.

推荐答案

您是指内存没有释放给操作系统吗-即top,类似的程序表明Java进程占用了1GB的内存?即使Java的垃圾回收器从其堆中释放了内存,它仍然可以保留内存,因此将来的分配不需要从OS请求更多的内存.

Are you are referring to the memory not being freed to the OS - i.e. top and similar programs show that the Java process takes 1GB of memory? Even though Java's garbage collector frees the memory from its heap, it can still keep hold of the memory so that future allocations don't need to request for more memory from the OS.

要查看Java对象实际使用了多少堆空间,请使用 VisualVM 或类似的Java专用工具.如果您的计算机有很多内存,那么JVM将使用它(IIRC,尤其是服务器VM已调整为保留更多内存),但是您始终可以使用-Xmx和其他JVM选项对其进行限制.

To see how much heap space is actually used by the Java objects, use VisualVM or a similar Java-specific tool. If your machine has lots of memory, then the JVM will use it (IIRC, especially the Server VM is tuned to reserve more memory), but you can always limit it with the -Xmx and other JVM options.

这篇关于Java堆溢出,强制垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆