Когда делает сборку "мусора", происходят для класса со статическими данными

В круге 360 градусов и 2 пи радиана. Таким образом, чтобы преобразовать градусы в радианы, разделите на 360 и умножьте на 2 * пи (прибл. 6.28).

Или, что то же самое, разделить на 180 и умножить на pi.

5
задан dfa 17 June 2009 в 09:00
поделиться

9 ответов

you can try to make it final, and recompile the code in order to see if some other class CHANGES the reference to null:

public class Global {

    public final static List<String> data = new ArrayList<String>();

}

this allow to write:

Global.data.add("foo");

but not:

Global.data = null;
9
ответ дан 18 December 2019 в 06:51
поделиться

Исходный вопрос:

Когда выполняется сборка мусора происходит для статического класса?

Ответ: Когда ваше приложение завершает работу.

3
ответ дан 18 December 2019 в 06:51
поделиться

I have a few variables in a static class, which get written to... at several different points.

As you are confessing yourself, so a null can be assigned to the variable at one or more of those different points. :)

3
ответ дан 18 December 2019 в 06:51
поделиться

As Svetlio said, the GC only runs on objects on the heap which aren't referenced anywhere. This may take x number of cycles depending on which GC-strategy and VM you're using. Since static classes are indefinitely referenced, they'll never be GC'd.

So some issues remain:

  1. Could it be part of your program setting class to null? Can you set a debug flag to scream whenever your arraylist is set null?
  2. Does this happen at a specific time? At startup? After a while?
  3. A dumb question, but you don't run new on your static class at any time?

Lasty, as you said...static variables suck for anything other than constants :P A singleton configuration is really cheap and a lot better.

3
ответ дан 18 December 2019 в 06:51
поделиться

If you can call the function than the object won't be garbage collected away because there's still a reference to it. Are you storing a pointer to the arraylist or the object itself?

2
ответ дан 18 December 2019 в 06:51
поделиться

Сборка мусора для классов происходит, если его загрузчик классов получает сборщик мусора. См., Например, этот вопрос . В более ранней версии Java (я думаю, в 1.2) было время сортировки, когда статические поля не учитывались сборщиком мусора, и большое количество выгрузки / перезагрузки классов приводило к пустым полям.

2
ответ дан 18 December 2019 в 06:51
поделиться

Static classes aren't like static variables. It's just yet another use for the keyword "static". In this case, it indicates the class is top-level but declared within another class.

So that means you can instantiate more than one object of this class, unlike say a static variable, for which there is only one copy.

Are you instantiating more than one of these and expecting them to be the same one?

The GC doesn't sound like it is the issue. If the object were no longer there, you couldn't retrieve the ArrayList from it at all.

1
ответ дан 18 December 2019 в 06:51
поделиться

Сборщик мусора не будет удалять статический класс сам по себе. Сборщик мусора может стать активным только в том случае, если нет ссылки на класс. Пока вы можете вызывать сам класс, ссылка есть. Also you cannot definitely say when the garbage collector is getting active. It runs as some kind if idle-task in the background.

I would recommend you to add some debug trace messages to your static class, so that you can see when it's called. There must be some side effects changing your ArrayList at runtime.

0
ответ дан 18 December 2019 в 06:51
поделиться

Статическое значение может быть нулевым, если оно находится в другом потоке. В противном случае он всегда должен быть там, если вы не скажете ему что-нибудь еще.

0
ответ дан 18 December 2019 в 06:51
поделиться
Другие вопросы по тегам:

Похожие вопросы: