question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Very dangerous and unoptimized code inside library!

See original GitHub issue
private static Bitmap getImageResized(Context context, Uri selectedImage) {
        int[] sampleSizes = new int[]{5, 3, 2, 1};
        int i = 0;

        Bitmap bm;
        do {
            bm = decodeBitmap(context, selectedImage, sampleSizes[i]);
            ++i;
        } while(bm != null && (bm.getWidth() < minWidthQuality || bm.getHeight() < minHeightQuality) && i < sampleSizes.length);

        Log.i(TAG, "Final bitmap width = " + (bm != null?Integer.valueOf(bm.getWidth()):"No final bitmap"));
        return bm;
    }

This code is absolutely HORRIBLE for these 3 reasons:

  1. Wastes CPU resources by creating up to 4 different bitmaps unnecessarily.
  2. Risks OutOfMemory crash by potentially loading a too-large Bitmap into RAM (you don’t check the size from file before loading Bitmap, it might be 100 megapixels as far as I’m concerned)
  3. Creates huge memory churn (again, by creating 4 unnecessary Bitmaps) which are then unnecessarily left for Garbage Collector to handle…

Please watch this video to learn how to load images properly from files: https://www.youtube.com/watch?v=HY9aaXHx8yA

I won’t use your library for these reasons.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Mariovccommented, May 23, 2017

I was just saying that you are free to give a better solution if you think there is a better way to do it. I will take it into account and will try to improve it in the next days/weeks.

Thank you for your comment and your help. And sorry if I was too rude, it wasn’t my intention.

1reaction
aranda-adapptorcommented, May 29, 2017

@gajicm93 not sure if it will get merged but you can use my branch to get around the issue if you need to.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is optimization dangerous? [closed] - Stack Overflow
Optimising code takes time from the developers that they could instead use to add new features or polish their product.
Read more >
Build optimizer breaks third party libraries #12128 - GitHub
Scaffold a project that uses code (e.g. a third party library) that contains code that cannot be properly "optimized" using uglifyjs.
Read more >
Dangerous Optimizations and the Loss of Causality
THIS MATERIAL OF CARNEGIE MELLON UNIVERSITY AND ITS SOFTWARE ENGINEERING. INSTITUTE IS FURNISHED ON AN “AS-IS" BASIS.
Read more >
Optimizing an Elm Library - crowdstrike.com
The library I optimized is one we use to calculate the Levenshtein edit distance between two strings. The final tally of speedups are ......
Read more >
Is it bad practice to write code that relies on compiler ...
In the particular case of C++'s (N)RVO, yes, relying on this optimisation is perfectly valid. This is because the C++17 standard specifically ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found