java内存释放,如何在Java中释放内存?

本文解释了Java中如何避免手动内存释放,通过new操作分配内存,垃圾回收机制确保内存管理。System.gc()仅建议而非强制GC,对象置null依赖于自动回收。

Is there a way to free memory in Java, similar to C's free() function? Or is setting the object to null and relying on GC the only option?

解决方案

Java uses managed memory, so the only way you can allocate memory is by using the new operator, and the only way you can deallocate memory is by relying on the garbage collector.

This memory management whitepaper (PDF) may help explain what's going on.

You can also call System.gc() to suggest that the garbage collector run immediately. However, the Java Runtime makes the final decision, not your code.

Calling the gc method suggests that

the Java Virtual Machine expend effort

toward recycling unused objects in

order to make the memory they

currently occupy available for quick

reuse. When control returns from the

method call, the Java Virtual Machine

has made a best effort to reclaim

space from all discarded objects.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值