rspec/rules/S3474/java/rule.adoc

38 lines
1.0 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
A cache is a long-lived object that holds references to shorter-lived objects. To prevent a cache from growing indefinitely, old objects the cache should be removed when they're no longer used. This can be done with the use of soft references, which allow the garbage collector to remove unused cache entries when memory needs to be freed.
This rule raises an issue when a ``++static++`` collection does not use ``++SoftReference++``.
*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is lower than 7.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public class MyClass {
private static List<MyClass> cache = new ArrayList<>(); // Noncompliant
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public class MyClass {
private static List<SoftReference<MyClass>> cache = new ArrayList<>();
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]