Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

Delete unused variable's memory in 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!

question from:https://stackoverflow.com/questions/11330812/delete-unused-variables-memory-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...