Reference management in Java and Rust, and, how faster Rust can be?
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.
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); } }

Soft References
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.

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.

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.




Rust:

All right, rust is really faaaaaaast! The Rust ownership system do have a great performance!
Thank you for your viewing!

浙公网安备 33010602011771号