- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
强引用(Strong Reference)是我们使用最多的一种对象引用,当一个对象被关键字 new 实例化出来的时候, JVM 会在堆(heap)内存中开辟一个内存区域,用于存放与该实例对应的数据结构。JVM 垃圾回收器线程会在达到 GC 条件的时候尝试回收(Full GC,Young GC)堆栈内存中的数据,强引用的特点是只要引用到 Root 根的路径可达,无论怎样的 GC 都不会将其释放,而是宁可出现 JVM 内存溢出。
Cache 是一种用于提高系统性能,提高数据检索效率的机制,而 LRU(Least recently used,最近最少使用)算法和 Cache 的结合是最常见的一种 Cache 实现。
LRU 是数据冷热治理的一种思想,不经常使用的数据被称为冷数据,经常使用的则被称为热数据,对冷数据分配提前释放,可以帮助我们节省更多的内存资源,LRUCache 的实现方式有很多种,在这里使用双向链表+hash表的方式来实现。
package concurrent.lrucache.strongRefercence;
/**
* @className: Reference
* @description: 当 Reference 对象被实例化后,会在堆内存中创建 1M 的内存空间
* @date: 2022/4/28
* @author: cakin
*/
public class Reference {
// 1M
private final byte[] data = new byte[2 << 19];
/**
* 功能描述:会在垃圾回收的标记阶段被调用,垃圾回收器在回收一个对象之前,首先会进行标记,标记的过程会调用该对象的 finalize 方法
* 所以千万不要认为该方法被调用之后,就代表对象已被垃圾回收器回收,对象在 finalize 方法中是可以”自我救赎“的。
*
* @author cakin
* @date 2022/4/28
* @description:
*/
@Override
protected void finalize() throws Throwable {
System.out.println("the reference will be GC");
}
}
package concurrent.lrucache.strongRefercence;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**
* @className: LRUCache
* @description: 最近最少使用缓存
* @date: 2022/4/28
* @author: cakin
*/
public class LRUCache<K, V> {
// 用于记录 key 值的顺序
private final LinkedList<K> keyList = new LinkedList<>();
// 用于存放数据
private final Map<K, V> cache = new HashMap<>();
// cache 的最大容量
private final int capacity;
// 提供了一种加载数据的方式
private final CacheLoader<K, V> cacheLoader;
public LRUCache(int capacity, CacheLoader<K, V> cacheLoader) {
this.capacity = capacity;
this.cacheLoader = cacheLoader;
}
public void put(K key, V value) {
// 当元素数量超过容量时,将最老的数据清除
if (keyList.size() >= capacity) {
K eldestkey = keyList.removeFirst(); // eldest data
cache.remove(eldestkey);
}
// 如果数据已经存在,则从 key 的队列中删除
if (keyList.contains(key)) {
keyList.remove(key);
}
// 将 key 存放到队尾
keyList.addLast(key);
cache.put(key, value);
}
public V get(K key) {
V value;
// 先将 key 从 key list 中删除
boolean success = keyList.remove(key);
// 如果删除失败则表明该数据不存在
if (!success) {
// 通过 cacheLoader 对数据进行加载
value = cacheLoader.load(key);
// 通过 put 方法 cache 数据
this.put(key, value);
} else {
// 如果删除成功,则从 cache 中返回数据,并且将 key 再次放到队尾
value = cache.get(key);
keyList.addLast(key);
}
return value;
}
@Override
public String toString() {
return this.keyList.toString();
}
}
package concurrent.lrucache.strongRefercence;
@FunctionalInterface
public interface CacheLoader<K, V> {
// 定义加载数据的方法
V load(K k);
}
package concurrent.lrucache.strongRefercence;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) {
test1();
test2();
}
private static void test1() {
LRUCache<String, Reference> cache = new LRUCache<>(5, key -> new Reference());
cache.get("1");
cache.get("2");
cache.get("3");
cache.get("4");
cache.get("5");
cache.get("6");
System.out.println(cache.toString());
}
private static void test2() {
LRUCache<Integer, Reference> cache1 = new LRUCache<>(200, key -> new Reference());
for (Integer i = 0; i < Integer.MAX_VALUE; i++) {
cache1.get(i);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("The " + i + " reference stored at cache.");
}
}
}
[2, 3, 4, 5, 6]
取缓存时,把最老的数据 1 给踢掉
-Xmx128M -Xms64M -XX:+PrintGCDetails
......
The 93 reference stored at cache.
The 94 reference stored at cache.
The 95 reference stored at cache.
The 96 reference stored at cache.
The 97 reference stored at cache.
The 98 reference stored at cache.
[Full GC (Ergonomics) [PSYoungGen: 15677K->15362K(18944K)] [ParOldGen: 87227K->87226K(87552K)] 102904K->102589K(106496K), [Metaspace: 4683K->4683K(1056768K)], 0.0071742 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Allocation Failure) [PSYoungGen: 15362K->15362K(18944K)] [ParOldGen: 87226K->87226K(87552K)] 102589K->102589K(106496K), [Metaspace: 4683K->4683K(1056768K)], 0.0028789 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 18944K, used 16002K [0x00000000fd580000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 97% used [0x00000000fd580000,0x00000000fe520860,0x00000000fe580000)
from space 2560K, 0% used [0x00000000fe580000,0x00000000fe580000,0x00000000fe800000)
to space 14336K, 0% used [0x00000000ff200000,0x00000000ff200000,0x0000000100000000)
ParOldGen total 87552K, used 87226K [0x00000000f8000000, 0x00000000fd580000, 0x00000000fd580000)
object space 87552K, 99% used [0x00000000f8000000,0x00000000fd52ea90,0x00000000fd580000)
Metaspace used 4715K, capacity 4882K, committed 4992K, reserved 1056768K
class space used 523K, capacity 559K, committed 640K, reserved 1048576K
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at concurrent.lrucache.strongRefercence.Reference.<init>(Reference.java:11)
at concurrent.lrucache.strongRefercence.Test.lambda$test2$1(Test.java:23)
at concurrent.lrucache.strongRefercence.Test$$Lambda$1/159413332.load(Unknown Source)
at concurrent.lrucache.strongRefercence.LRUCache.get(LRUCache.java:51)
at concurrent.lrucache.strongRefercence.Test.test2(Test.java:25)
at concurrent.lrucache.strongRefercence.Test.main(Test.java:8)
强引用产生了内存溢出问题。
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: template pass by value or const reference or…? 以下对于将函数
我用相同的参数列表重载了一个运算符两次。但返回类型不同: T& operator()(par_list){blablabla} const T& operator()(par_list){bla
假设我有实现接口(interface) I 的 Activity A。我的 ViewModel 类 (VM) 持有对实现接口(interface) I 的对象的引用: class A extends
PHP 如何解释 &$this ?为什么允许? 我遇到了以下问题,这看起来像是 PHP 7.1 和 7.2 中的错误。它与 &$this 引用和跨命名空间调用以及 call_user_func_arr
谁能解释一下下面“&”的作用: class TEST { } $abc =& new TEST(); 我知道这是引用。但是有人可以说明我为什么以及什么时候需要这样的东西吗?或者给我指向一个对此有很好解
引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字。一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量。 C++ 引用 vs 指针 引用很容易与指针混淆,它们之间有三
目录 引言 背景 结论 引言 我选择写C++中的引用是因为我感觉大多数人误解了引用。而我之所以有这个感受是因为我主持过很多C++的面试,并且我很少
Perl 中的引用是指一个标量类型可以指向变量、数组、哈希表(也叫关联数组)甚至函数,可以应用在程序的任何地方 创建引用 定义变量的时候,在变量名前面加个 \,就得到了这个变量的一个引用 $sc
我编写了一个将从主脚本加载的 Perl 模块。该模块使用在主脚本中定义的子程序(我不是维护者)。 对于主脚本中的一个子例程,需要扩展,但我不想修补主脚本。相反,我想覆盖我的模块中的函数并保存对原始子例
我花了几个小时试图掌握 F# Quotations,但我遇到了一些障碍。我的要求是从可区分的联合类型中取出简单的函数(只是整数、+、-、/、*)并生成一个表达式树,最终将用于生成 C 代码。我知道使用
很多时候,问题(尤其是那些标记为 regex 的问题)询问验证密码的方法。似乎用户通常会寻求密码验证方法,包括确保密码包含特定字符、匹配特定模式和/或遵守最少字符数。这篇文章旨在帮助用户找到合适的密码
我想通过 MIN 函数内的地址(例如,C800)引用包含文本的最后一个单元格。你能帮忙吗? Sub Set_Formula() ' ----------------------------- Dim
使用常规的 for 循环,我可以做类似的事情: for (let i = 0; i < objects.length; i++) { delete objects[i]; } 常规的 for-
在 Cucumber 中,您定义了定义 BDD 语法的步骤;例如,您的测试可能有: When I navigate to step 3 然后你可以定义一个步骤: When /^I navigate t
这是什么UnaryExpression的目的,以及应该怎样使用? 最佳答案 它需要一个 Expression对象并用另一个 Expression 包裹它.例如,如果您有一个用于 lambda 的表达式
给出以下内容 $("#identifier div:first, #idetifier2").fadeOut(300,function() { // I need to reference jus
我不知道我要找的东西的正确术语,但我要找的是一个完整的引用,可以放在双引号之间的语句,比如 *, node()、@* 以及所有列出的 here加上任何其他存在的。 我链接到的答案提供了一些细节,但还
This question's answers are a community effort。编辑现有答案以改善此职位。它当前不接受新的答案或互动。 这是什么? 这是常见问答的集合。这也是一个社区Wi
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
考虑下一个代码: fn get_ref(slice: &'a Vec, f: fn(&'a Vec) -> R) -> R where R: 'a, { f(slice) } fn m
我是一名优秀的程序员,十分优秀!