Когда собирать мусор

У меня есть кусок кода, который загружает очень большое изображение в память. Так что казалось разумным позвонить

System.gc();

перед загрузкой изображения. Из того, что я могу сказать, это работает без проблем.

Вчера я решил использовать довольно полезную часть программного обеспечения под названиемFindBugs который сканирует ваш код и сообщает о проблемах, которые могут вызвать ошибки или вообще не рекомендованные стратегии. Проблема в том, что об этом фрагменте кода, о котором я упоминал, сообщили. Описание таково:

... forces garbage collection; extremely dubious except in benchmarking code

И это продолжает уточнять:

Code explicitly invokes garbage collection. Except for specific use in benchmarking, this is very dubious.

In the past, situations where people have explicitly invoked the garbage collector in routines such as close or finalize methods has led to huge performance black holes. Garbage collection can be expensive. Any situation that forces hundreds or thousands of garbage collections will bring the machine to a crawl.

Итак, мой вопрос:Is it NOT OK to programmatically call the garbage collector in such a case? My code only calls it once and the method that it is in gets used rarely. And if it is not OK to call it then what should you do in a case where you need as much memory as possible before doing a very memory intensive operation and you need to free as much memory as posible prior to it?

Ответы на вопрос(8)

Ваш ответ на вопрос