vendredi 9 novembre 2018

enum.INSTANCE vs Static class

I’m compiling a group of Android performance best practices, and one question came up about a common design pattern.

Imagine that you have an Android app that uses the following two distinct patterns:

Enum class:

public enum MyEnumSingleton {
    INSTANCE;

    ...
}

Static class:

public final MyUtilsClass {

    private Utils() {
        //no instance
    }

    ...
}

These two classes are used essentially with the same purpose: caching and utility methods.

In terms of memory usage:

  1. What is the most memory friendly pattern?
  2. Is the OS going to load all the enums into memory when the app starts?
  3. Is the OS only load the static class into memory when it is used by the app?
  4. Is there any use case were one pattern should strongly be used against another?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire