Do you need a memory-aware implementation of Java?

Introduction

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).

Awareness of memory usage

About class loading

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.

(Question) Class loading, instantiation, etc ... Do I have to be so conscious of that?

(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?)

Even the end programmers should be aware of memory

Do not use static fields (static variables) more than necessary.

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.

Be aware that instantiation (new) is costly.

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.

(Under consideration for rewriting) ~~ Make static methods good ones ~~

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;
}
Be aware of immutable

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.

How to implement it in the future

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.

(Bonus) About GC

There are copy GC and full GC. You can get a GC log. (Vm option)

Further digression

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.

in conclusion

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.

Article link

@ 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.

reference

How to check Qiita --OutOfMemoryError

Recommended Posts

Do you need a memory-aware implementation of Java?
Do you need dotenv-rails?
[Java] Implementation of Faistel Network
Implementation of gzip in java
Implementation of tri-tree in Java
Do you really need that preload? ~ Utilization of delayed load ~
Implementation of a math parser with recursive descent parsing (Java)
Think of a Java update strategy
Why you need a bundle exec
Sort a List of Java objects
Can you do it? Java EE
A brief description of JAVA dependencies
Implementation of like function in Java
Do you use Stream in Java?
Let's create a TODO application in Java 4 Implementation of posting function
Let's create a TODO application in Java 6 Implementation of search function
Let's create a TODO application in Java 8 Implementation of editing function
Name a group of regular expressions (Java)
A story confirming the implementation of the SendGrid Java library when mail delivery fails
Do you use the for statement after all? Do you use a while statement? Proper use of for statement and while statement
Measure the size of a folder in Java
A note when you want Tuple in Java
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
[Java / Spring Boot] Spring security ④ --Implementation of login process
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
A collection of simple questions for Java beginners
A quick review of Java learned in class
Implementation of delete function (if you have foreign_key)
What to do if you get a wrong number of arguments error in binding.pry
What to do if you get a NoClassDefFoundError when trying to run eclipse on Java9
Implementation of GKAccessPoint
[Java] Overview of Java
[Read Effective Java] Chapter 3 Item 12 "Considering Implementation of Comparable"
Get a list of MBean information for Java applications
A quick review of Java learned in class part4
Implementation example of simple LISP processing system (Java version)
What to do if you accidentally create a model
Awesome Java: A number of great Java framework library software
Experience of passing Java Silver as a new graduate
Let's make a robot! "A simple demo of Java AWT Robot"
[Java] When writing the source ... A memorandum of understanding ①
A quick review of Java learned in class part3
A quick review of Java learned in class part2
A survey of the Kubernetes native Java framework Quarkus
What you need to do to open a file from the menu in the document-Based App macOS app
What do you need after all to create a web application using java? Explain the mechanism and what is necessary for learning