A memorandum of what I learned because I consulted about my thoughts on memory. As for the terms, I write the parts that I know as they are. On the contrary, if it seems to be used incorrectly, please point it out (and let me know).
At main execution (or when a class that has a main function before it is interpreted by the jvm), Load the related class into memory in a worm-like manner.
(It may be hit depending on how you think about it.) ** You don't have to be aware of that. Because the language Java is a language that implementers don't have to be aware of memory management (to that extent). This area is written on wikipedia. [Java # Garbage Collection-Wikipedia](https://ja.wikipedia.org/wiki/Java#%E3%82%AC%E3%83%99%E3%83%BC%E3%82%B8%E3% 82% B3% E3% 83% AC% E3% 82% AF% E3% 82% B7% E3% 83% A7% E3% 83% B3)
If you are conscious of memory management, you will be talking about implementing a JVM. (If you want to manage it completely, should you use C or C ++ in the first place?)
You should be aware of the right side of the static field. Static fields are created when the class is loaded. If you put heavy processing on the right side (initialization), it will cause memory pressure. Instance variables, static initializers, and initializers should be taken care of as well.
Instance creation is the act of allocating a heap and creating an object. (Execution of instance variables, instance methods, initializers) Obviously, it costs money every time it is generated.
If there are no side effects, static is better because it does not cost much to generate. The side effect mentioned here is whether it can be created independently of the state of the instance. I understand that the so-called Util class can be used positively. (Since the class is loaded when the main method is executed, no generation cost is required)
public static int plus(int a, int b){
retrun a + b;
}
There is an immutable controversy.
What is immutable? Immutable (immutable: immutable) and mutable (mutable: mutable) objects.
Typical Java is String
, LocalDateTime
, etc.
It seems that affirmatives and negatives are having hot discussions about this day and night.
** Affirmative: ** Keeping the values not updated makes the program safe. Also, even if you create a new instance, the original instance should be unnecessary, so it will be released by copy GC as appropriate.
** Negatives: ** Wasteful creation cost of the instance. (Considering rewriting) ~~ Danger of memory leak. ~~
As a developer, it would be nice if the same value could be implemented so that it looks at the same reference like String, but it would be difficult to drop it into logic so much, so I think it's better to be aware of immutable.
Even if you don't think so deeply, you can implement it so that memory leaks do not occur as before. For example, the following.
--Be aware of the scope of variables so that they can be picked up by GC. --Do not leave unused references. (Only on implementation) -Do not use static variables (unnecessarily)
Basically, as long as Java is used, memory management is left to Java.
There are copy GC and full GC. You can get a GC log. (Vm option)
I was shown the GC log of the system under development.
In the log, fullGC occurred several times at the start of startup.
(In the past, it seems that memory was specified at runtime, but for some reason it seems to have stopped now. It may be because it was a test environment.)
If I knew the lowest line to use, I thought I should specify the vm option -Xms
to prevent fullGC from happening.
I would like to study GC again.
I was worried about it for about two weeks, but I once converged on Java memory. I searched for information on the internet and asked various people, but it didn't come to my mind, so it took me a while to understand. I am very grateful to the leaders in the field for their polite explanations while I was busy.
@ j5c8k6m8 read this article and wrote a poem, so I've posted a link. Qiita --Is memory conscious programming (implementation) necessary? I read it, but it was a learning experience.
How to check Qiita --OutOfMemoryError
Recommended Posts