Smart Garbage Collection?

后端 未结 6 1755
别那么骄傲
别那么骄傲 2021-01-20 17:24

You can garbage collect in Java simply by calling System.gc() but sometimes this \"stalls\" the application. Is it a bad idea to garbage collect like this and a

6条回答
  •  梦谈多话
    2021-01-20 17:33

    I can't beat @haylem answer's clarity and force, sorry. But let me add that there are a lot of (better) ways to manage memory in Java. There is, for example, WeakReference and collections which handle these, like WeakHashMap. These are deterministic ways to handle memory, though explicit calls to GC are not, as per the javadoc

    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.
    

提交回复
热议问题