JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
Java


JAVA.CLASS.UI : Inefficient Instantiation (Java)

要旨

The construction of an object might be replaced by a literal or by a call to a factory method.

It is often convenient to use a literal or a unique object instead of explicitly constructing new instances, when the same object is used many times, since distinct objects are created at each new statement, which affects the performance of the code. Moreover, objects used instead of literals are often short-lived and consequently must be claimed back from the garbage-collector.

プロパティ

クラス名 Inefficient Instantiation (Java)
日本語クラス名 Inefficient Instantiation (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.CLASS.UI
カテゴリー
CWE CWE:400 Uncontrolled Resource Consumption
CERT-Java CERT-Java:MSC05-J Do not exhaust heap space
  CERT-Java:SER10-J Avoid memory and resource leaks during serialization
対応言語 Available for Java and Kotlin.
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Inefficient Instantiation (Java)"

import java.util.HashMap;
import java.util.Map;

public class InefficientConstruction {
  public static void foo() {
      String s = new String("hello"); /* "Inefficient Instantiation (Java)" warning issued here 
                                         * This String instantiation can be replaced by a string literal. 
                                         */ 
      Map<Integer, String> map = new HashMap<Integer, String>();
      map.put(new Integer(13) , s);   // "Inefficient Instantiation (Java)" warning issued here 
                                         * This Integer object is recreated at every execuition of foo(). 
                                         */ 
  }
}

The programmer could modify the program to address the problem as follows.

  public static void foo() {
    String s = "hello";
    Map<Integer, String> map = new HashMap<Integer, String>();
    map.put(Integer.valueOf(13), s);
                    /* Integer.valueOf(13) uses a cache to yield the same object 
                     * each time foo() is executed, at least for small values. 
                     */ 
  }      

The automatic boxing mechanism uses the same optimization, so we can further optimize to:

  public static void foo() {
    String s = "hello";
    Map<Integer, String> map = new HashMap<Integer, String>();
    map.put(13, s);
  }      

解決法

Use string literals instead of the creation of constant strings. Use factory methods or the automatic boxing mechanism instead of the instantiation of a primitive wrapper class.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。

 

To report problems with this documentation, please visit https://support.codesecure.com/.