Hi, this is a blog comparing the reference in rust and java. I really love java and I have spend some time learning the framework like spring and others. After taking COMP6991, I have got this think: How java manage the reference in my program? Why can I use a string in java for unlimited time? Is it as fast as Rust?
After searching some resources of JVM, I got these result below.
With the help of the JVM, Java will automatically manage and release memory, the JVM provides a set of memory management mechanism, which is really powerful. When a Java object is no longer used, the JVM will recycle it and release the memory. Most of the time, we do not need to manage the memory or reference by ourselves. And, in java, we have 4 types of references, this will help to manage the reference better.
The four types of references in Java are, in descending strength order: Strong > Soft > Weak > Dummy.
Strong Reference
A reference type like Object o = new Object() in our usual code can be called a strong reference when broken down.
Strong references are the most common references. When an object is a strong reference, the garbage collector will never reclaim it. When out of memory, the JVM would rather throw an OutOfMemoryError error than reclaim an object with a strong reference to solve the out-of-memory problem.
So, like this:
package com.skywxp.mysite.controller; public class test { public static void main(String[] args) { String a = "123"; test test = new test(); test.getString(a); test.getString1(a); test.getString5(a); } public void getString(String a) { System.out.println(a); } public void getString1(String a) { System.out.println(a); } public void getString5(String a) { System.out.println(a); } }
We can get use of the String for unlimited time without other operation(like & in rust)! JVM will always maintain it. We do not need to worry about that we need to add a “&” for second or more time of use!
Soft References
Unlike strong references, soft references can be reclaimed for some reason. and the JVM will try to reclaim them when it thinks there is not enough memory, Of course, if there is enough memory, then it will not be reclaimed easily.
package com.skywxp.mysite.controller; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; import java.util.ArrayList; import java.util.List; public class test { public static void main(String[] args) { ReferenceQueue<Object> queue = new ReferenceQueue<>(); SoftReference<Object> reference = new SoftReference<>(new Object(), queue); System.out.println(reference); try{ List<String> list = new ArrayList<>(); while (true) list.add(new String("123")); }catch (Throwable t){ System.out.println("memory overload!"+t.getMessage()); System.out.println("soft reference object:"+reference.get()); System.out.println(queue.poll()); } } }
emmm, actually I have forgot how to limit the maximum heap memory, so the soft reference has not been reclaime
But when I wait for some time, it is reclaimed. Haha.
Other reference are weak references and dummy references. We often do not need to pay too much attention to these two kinds of references:
Weak references
A weak reference has a shorter life cycle than a soft reference, and its memory will be reclaimed when garbage collection is performed, regardless of whether or not there is enough current memory space.
Dummy references
Dummy references are equivalent to no references and can be reclaimed at any time.
And, from the lectures we have before, the rust take use of “ownership system” to manage the reference.
Absolutely this will require us to write the code very carefully, if we just simply use a String for 2 times, there goes error. There will not be a GC helping you managing the memory and reference!
If we want to take use of the String for more than one time, we need to take use of the & in rust, called borrow, this will make the String available for multi-use.
This is easy to understand, but, I am interested in the speed of the program in java and rust(although I know that rust must be faster, I still want to have an actual data). How faster will the ownership system be than the JVM system? JVM makes us free from the reference management, it helps us to keep the object and its reference, and how much will this cost? I plan to do a simple test.
For rust: 10 reference, test the speed of the program, and also for java.
Result:
Java:
Rust:
All right, rust is really faaaaaaast!
Thank you for your viewing!
标签:management,Java,String,reference,will,references,memory,java,Rust From: https://www.cnblogs.com/skywxp/p/18055851