need of Static variables and their overhead on jvm

前端 未结 3 935
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 00:03

As per the concept about static members, they are created/loaded into the memory when there is first call made to its class. And they are common among all instances of that

3条回答
  •  离开以前
    2021-01-07 00:20

    1) Static members are garbage collected only when the class that defines them is itself collected; this in turn can only happen if the defining ClassLoader is collected. This is common in web application containers and plugin architectures.

    2) Yes, defining a large amount of static data can be a bad idea. But it's like a lot of other things: it's good if you need it, and bad if you abuse it. Just use common sense.

    3) Again, an interface that defined an array of a thousand Strings would be a bad idea, but of course that's not normally what people do. Just use common sense. There's no (memory-related) reason to avoid static variables in general.

提交回复
热议问题