首页 > 编程语言 >Reference management in Java and Rust, and, how faster Rust can be?

Reference management in Java and Rust, and, how faster Rust can be?

时间:2024-03-06 10:02:22浏览次数:28  
标签:management Java String reference will references memory java Rust

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

相关文章

  • java.util.Arrays 快速学习教程
    在Java中,java.util.Arrays类提供的多种数组操作功能,可以有效地执行各种数组相关的操作,使得数组处理变得简单和高效。打印数组String[]arr=newString[]{"a","b","c","d"};System.out.println(Arrays.toString(arr));//输出[a,b,c,d]Arrays.toString(arr),不过......
  • Java核心内容面试题详解
    前言随着经济的复苏,市场逐渐回暖,曾经的金三银四,金九银十也慢慢回归,在这个节骨眼上,我们要努力学习,做好知识储备,准备随时接收这泼天的offer。而我利用摸鱼(不是,是工作之余)时间也整理了一份关于Java核心知识的面试题,大家有兴趣,有需要的可以看看,希望能够给大家提供一些帮助Java基础面......
  • 关于Java并发多线程的一点思考
    写在开头在过去的2023年双11活动中,天猫的累计访问人次达到了8亿,京东超60个品牌销售破10亿,直播观看人数3.0亿人次,订单支付频率1分钟之内可达百万级峰值,这样的瞬间高并发活动,给服务端带来的冲击可想而知,就如同医院那么多医生,去看病挂号时,有时候都需要排队,对于很多时间就是金钱的场......
  • 【转】[Java]接口的 VO 使用内部类的写法
    参考:https://www.cnblogs.com/hyperionG/p/15602642.html以下代码段是向阿里的通义灵码提问得到的:importlombok.Data;@DatapublicclassOuterVO{//外部类的属性privateStringouterAttribute;//定义内部类并添加@Data注解@Datapublicst......
  • Java中的对象克隆
    对象克隆复制一个一模一样的新对象出来浅克隆拷贝出的新对象,与原对象中的数据一模一样(引用类型拷贝的只是地址)深克隆对象中基本类型的数据直接拷贝。对象中的字符串数据拷贝的还是地址。对象中包含的其他对象,不会拷贝地址,会创建新对象packagecom.aiit.itcq;imp......
  • Java进制之间的转换
    进制:我们生活中使用的是十进制计算机中使用的是二进制在Java中的进制的分类?十进制:逢十进一二进制:逢二进一八进制:逢八进一十六进制:逢十六进一10->A11->B12->C13->D14->E15->F在计算机中,数据......
  • Java 源码,反码和补码
    计算机在对数据进行运算的原理?3-2=13+(-2)=1先将3这个十进制,变成二进制的原码形式,然后变成反码形式,最后变成补码形式先将-2这个十进制,变成二进制的原码形式,然后变成反码形式,最后变成补码形式将这两个数二......
  • Java11改进的垃圾回收器
       传统的C/C++等编程语言,需要程序员负责回收已经分配的内存。显示进行垃圾回收是一件比较困难的事情,因为程序员并不总是知道内存应该何时被释放。如果一些分配出去的内存得不及时回收,就会引起系统运行速度下降,甚至导致程序瘫痪,这种现象被称为内存泄漏。总体而言,显示进行垃圾......
  • Java 抽象类与方法:实现安全性与代码重用
    Java内部类简介在Java中,可以嵌套类(即类内部的类),称为内部类。嵌套类的目的是将属于一起的类分组,从而使您的代码更可读和可维护。访问内部类要访问内部类,请创建外部类的对象,然后创建内部类的对象:classOuterClass{intx=10;classInnerClass{inty=5;}......
  • Java集合
    Java集合Java分为单列数据集合和双列数据集合单列数据集合一次存取一个元素双列数据集合一次存取一对元素单列数据集合单列集合的祖宗(Collection)List系列集合:有序(按照添加的顺序存放)、可重复、有索引Set系列集合:无序、不可重复、无索引Collection接口方法其中......