在java中删除未使用的变量的内存 [英] Delete unused variable's memory in java

查看:167
本文介绍了在java中删除未使用的变量的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Java有自己的垃圾收集,但有时我想手动删除垃圾。有没有办法做这样的工作?并且考虑到我有一个很长或很长的函数它声明了很多变量,最好在使用后立即删除未使用的内存而不是自动收集。功能?如果我手动删除垃圾,是否会影响我的申请速度?感谢您的帮助!

I know that Java have its own garbage collection, but sometimes I want to delete the garbage manually. Is there any way to do the work like that? And considering that I have a long or very long function which declares many variables, is this a good idea to delete the unused memory immediately after using instead of auto collection at the end of the function? If I delete garbage manually, does it affect the speed of my application? Thanks for helping!

推荐答案

在java中没有直接和直接的释放内存的方法。您可能会尝试说服垃圾收集器使用众所周知的东西拿走一些对象:

There is no direct and immediate way to free memory in java. You might try to persuade the garbage collector to take away some object using the well known:

Object obj = new Object();
// use obj
obj = null;
System.gc();

但不保证这会立即释放内存。

but there is no guarantee that this will actually free memory immediately.

虽然这适用于堆内存,但只有在函数返回时才能释放堆栈分配的项目。

While this applies to heap memory, stack allocated items can only be freed when the function returns.

这篇关于在java中删除未使用的变量的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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