Giter Site home page Giter Site logo

Comments (12)

AndroidDeveloperLB avatar AndroidDeveloperLB commented on July 22, 2024

Or maybe it's possible to load a compressed file from the start?

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

It is always required as it loads the timezone database.

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

And it's already compressed

from threetenabp.

AndroidDeveloperLB avatar AndroidDeveloperLB commented on July 22, 2024

Why do I need the timezone, though?
I mean, the LocalDate doesn't have a timezone...
And, is it possible to use the built in one of Android API 26, instead of the one of the library, when it's the current one that the app runs on ?
This could remove the need of this initialization...

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

from threetenabp.

AndroidDeveloperLB avatar AndroidDeveloperLB commented on July 22, 2024

That's too bad.
About compression, I think it can be better compressed, even with zip.
Look :
image

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

from threetenabp.

AndroidDeveloperLB avatar AndroidDeveloperLB commented on July 22, 2024

But isn't loading from storage the slower part?
I mean, RAM&CPU usage is always faster than disk read, no?

Or maybe it depends on the file that's loaded?
I guess if the compression is too complex, it could in theory become slower, but is it really the case here?

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

from threetenabp.

AndroidDeveloperLB avatar AndroidDeveloperLB commented on July 22, 2024

I've tested the next codes, and compared the time it took for both:

    private fun originalLoading() {
        val provider: TzdbZoneRulesProvider
        var inputStream: InputStream? = null
        try {
            inputStream = resources.openRawResource(R.raw.original)
            provider = TzdbZoneRulesProvider(inputStream)
        } catch (e: IOException) {
            throw IllegalStateException("TZDB.dat missing from assets.", e)
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close()
                } catch (ignored: IOException) {
                }
            }
        }
        ZoneRulesProvider.registerProvider(provider)
    }
    private fun newLoading() {
        val provider: TzdbZoneRulesProvider
        var zin: InputStream? = null
        try {
            zin = ZipInputStream(resources.openRawResource(R.raw.tzdb))
            val ze = zin.nextEntry
            provider = TzdbZoneRulesProvider(zin)
            zin.closeEntry();
        } catch (e: IOException) {
            throw IllegalStateException("TZDB.dat missing from assets.", e)
        } finally {
            if (zin != null) {
                try {
                    zin.close()
                } catch (ignored: IOException) {
                }
            }
        }
        ZoneRulesProvider.registerProvider(provider)
    }

I'm not sure if I did the correct unzip-loading, but newLoading took around 90 ms, while the originalLoading took around 60 ms.

So I think you are right here.
Did I implement it correctly though?
And why did you put the file in the assets folder? Just to keep its original file name?
I think using assets folder is almost the same as the raw resource folder, no?

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

from threetenabp.

AndroidDeveloperLB avatar AndroidDeveloperLB commented on July 22, 2024

An explanation to what? The assets decision? This :

For some reason getResourceAsStream() does some rather extreme caching and takes up a ton of memory if you use it**.

?

You mean it take much more RAM when using res/raw ? But why is it this way? Is it true for all Android versions? Is it always recommended to avoid using res/raw, and use assets folder instead? If so, shouldn't Google suggest such a thing in the IDE then?

But they talk about taking it from Jar, not from resource folder. Or is it the same?
And, is there any way to make it use the timezones set by the OS, if it's API 26 and above, so that it won't need to load it from a file at all?

I've now tested on my device (Nexus 5x with Android 8.1), and here are the results of testing assets vs raw :

using original call of AndroidThreeTen.init(this) :
Before call: used 1.4 MB
After call: used 2.4 MB

using function of originalLoading :
Before call:1.5 MB
After call: 2.5 MB

I don't see much difference here. On some cases I've ran it, one of them took a tiny bit more, and on others the other. Maybe it got optimized on Android 8.1 or somewhere below?
Maybe it's one of the optimizations they did, that make it now hard to notice, like what they did with Bitmaps (I wrote about it here) ?
Then again, my way of measuring might not be so accurate, as it's tricky to measure memory on Java world...

Here's my POC to test it:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val handler = Handler()
        System.gc()
        handler.postDelayed( {
            Log.d("AppLog", "heap mem before:" + getHeapMemStats(this) )
//        AndroidThreeTen.init(this)
            originalLoading()
            System.gc()
            handler. postDelayed({
                Log.d("AppLog", "heap mem after:" + getHeapMemStats(this) )
            }, 1000)
        }, 1000)
    }

fun getHeapMemStats(context: Context): String {
    val runtime = Runtime.getRuntime()
    val maxMemInBytes = runtime.maxMemory()
    val availableMemInBytes = runtime.maxMemory() - (runtime.totalMemory() - runtime.freeMemory())
    val usedMemInBytes = maxMemInBytes - availableMemInBytes
    val usedMemInPercentage = usedMemInBytes * 100 / maxMemInBytes
    return "used: " + Formatter.formatShortFileSize(context, usedMemInBytes) + " / " +
            Formatter.formatShortFileSize(context, maxMemInBytes) + " (" + usedMemInPercentage + "%)"
}

Which Android version do you have? Can you test it on older Android versions, maybe?

from threetenabp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.