Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fb-contrib:PMB_POSSIBLE_MEMORY_BLOAT should not appear where there is no option to mutate the collection #472

Open
mistriel opened this issue Aug 14, 2024 · 3 comments

Comments

@mistriel
Copy link

my case has two issues related to this check,

  1. The docs appears to refer to collection or StringBuffer but not to a Map
  2. The warning should not appear where there is no option to bloat.

In the following scenario if putUnique was public there were an option to bloat the Map,
yet since putUnique is private and roleMapping is private then roleMapping field is immutable.

public class Example{

    private static final Map<String, String> roleMapping = new HashMap<>();

    static {
        putUnique("Jhon", "User");
        putUnique("Kate", "Manager");
        putUnique("Gorge", "Supervisor");
       
    }

    public String getRole(String first) {
     
        return roleMapping.get(first);
    }

    
    private static void putUnique(String first, String last ) {
        String previous = roleMapping.put(first, last);
        if (previous != null) {
            throw new IllegalArgumentException("Duplicate key");
        }
    }
}
@mebigfatguy
Copy link
Owner

true. the issue is putUnique could be called elsewhere, so the question is how to determine where to stop? private? protected, package private??? etc. what if another method in this class called putUnique? What if a chain of 5 methods in this class called putUnique, and the last one was public? I'm not arguing you are correct, just that it's kind of untractable.

We could say a private method that is only called from a static block is ok. That's probably doable.

@mistriel
Copy link
Author

Hi,
So lets agree on the final statement, with addition to constructor as well.
WDYT ?

@mebigfatguy
Copy link
Owner

mebigfatguy commented Oct 31, 2024

sure, assuming the field is an instance field

mebigfatguy added a commit that referenced this issue Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants