浅谈虚拟机内存区
A direct byte buffer may be created by invoking the allocateDirect factory method of this class. The buffers returned by this method typically have somewhat higher allocation and deallocation costs than non-direct buffers. The contents of direct buffers may reside outside of the normal garbage-collected heap, and so their impact upon the memory footprint(内存占用) of an application might not be obvious. It is therefore recommended that direct buffers be allocated primarily for large, long-lived buffers that are subject to the underlying system's native I/O operations. In general it is best to allocate direct buffers only when they yield a measureable gain in program performance. 但是由于直接内存使用allocateDirect创建,它比申请普通的堆内存需要耗费更高的性能。不过它不会占用应用的堆内存。所以,当你有大量数据要缓存时,并且它的生命周期又比较长,那么使用直接内存是个不错的选择。但如果该选择不能带来显著的性能提升,推荐使用堆内存。在JDK1.4的NIO中,ByteBuffer有个方法是:
另外直接内受限于本机总内存(包括RAM及SWAP区或者分页文件)的大小及处理器寻址空间的限制。 服务器管理员配置虚拟机参数时,一般会根据实际内存设置-Xmx等参数信息,但经常会忽略掉直接内存,使得各个内存区域的总和大于物理内存限制(包括物理上的和操作系统级的限制),从而导致动态扩展时出现OutOfMemoryError异常。 2.7.2 直接内存的特点
2.7.3 直接内存的异常 动态扩展时出现OutOfMemoryError异常 2.7.4 直接内存的作用 基于通道(Channel)与缓冲区(Buffer)的I/O方式,它可以使用Native函数库直接分配堆外内存,然后通过一个存储在Java堆里面的DirectByteBuffer对象作为这块内存的引用进行操作。这样能在一些场景中显著提高性能,因为避免了在Java堆和Native堆中来回复制数据。 2.7.5 直接内存的运用 XX:MaxDirectMemorySize=10M 2.7.6 直接内存的使用场景 例如在IO处理时,比如网络发送大量数据时,直接内存会具有更高的效率。 本文基于jdk1.6、1.7 【编辑推荐】
点赞 0 (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |