Finding Memory Usage in Java

后端 未结 8 2512
野趣味
野趣味 2021-02-09 13:52

Following is the scenario i need to solve. I have struck with two solutions.

I need to maintain a cache of data fetched from database to be shown on a Swing GUI. Wheneve

8条回答
  •  忘掉有多难
    2021-02-09 14:10

    The usual way to handle this sort of thing is to use WeakReferences and SoftReferences. You need to use both - the weak reference means you are not holding multiple copies of things, and the soft references mean that the GC will hang onto things until it starts running out of memory.

    If you need to do additional cleanup, then you can add references to queues, and override the queue notification methods to trigger the cleanup. It's all good fun, but you do need to understand what these classes do.

提交回复
热议问题