Dead store

From Wikipedia, the free encyclopedia

In Computer programming, if assigning a value to a local variable, but the value is not read by any subsequent instruction, then it is referred to as a Dead Store. Dead Stores are wasteful of processor time and memory, and may be detected through the use of static program analysis.

Java example of a Dead Store:

// DeadStoreExample.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class DeadStoreExample {
 public static void main(String[] args) {
   List<String> list = new ArrayList<String>(); // This is a Dead Store, as the ArrayList is never read. 
   list = getList();
   System.out.println(list);
 }
 
 private static List<String> getList() {
   return new ArrayList<String>(Arrays.asList("Hello"));
 }
}

In the above code an ArrayList<String> object was instantiated but never used. Instead, in the next line the variable which references it is set to point to a different object. The ArrayList which was created when list was declared will now need to be de-allocated, for instance by a Garbage Collector.

JavaScript example of a Dead Store:

function func(a, b) {
    var x;
    var i = 300;
    while (i--) {
        x = a + b; // dead store
    }
}

"The code in the loop repeatedly overwrites the same variable, so it can be reduced to only one call."[1]

References

  1. "HTML5, and Real World Site Performance: Seventh IE9 Platform Preview Available for Developers". 


This article is issued from Wikipedia. The text is available under the Creative Commons Attribution/Share Alike; additional terms may apply for the media files.