Do I need a static method in an environment where there is only one instance of the class?

Asked 2 years ago, Updated 2 years ago, 25 views

Assuming that the class I created is single-toned and only one instance exists as a single thread, and there is no initialization work without considering synchronization issues,

I'm not sure if I should make the private method static or non-static. Are there any advantages or other benefits of static methods in memory management?

java

2022-09-22 20:07

2 Answers

Java, as you know, has garbage collectors, so only garbage collectors can clean objects. In other words, developers cannot determine object lifetimes.

The static field is targeted for garbage collection when the class loader loaded for that class is removed. This means that the class loader is rarely removed during runtime, so it can be determined to be permanently alive.(Of course, if the class loader is replaced by Was's hot defoloy, it will be the target of gc.))

However, normal objects will be cleaned up when they are not used (when the reference count is 0) and promoted (when the generator algorithm is).


2022-09-22 20:07

http://stackoverflow.com/questions/3849634/static-allocation-in-java-heap-stack-and-permanent-generation/3849819#3849819

I read

It is said that the information about the class and static go into Heap, and the local variable goes into stack.

But the method goes into heap, static or not. Then wouldn't that make a difference?


2022-09-22 20:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.