Home > Blockchain >  The program I built consumes a lot of RAM
The program I built consumes a lot of RAM

Time:02-14

i only use one webviwe and on ImagviWe but my app use 150 Mb ram plese help me what can i do for this problem? my apk size 5MB i use R8.fullmode = true

CodePudding user response:

First, look at how often you create new instances of the class. If you have duplicate lines in your code, then make a separate method for them. Study the topic of application optimization, there are many videos on this topic on YouTube and show the code of your application

CodePudding user response:

package com.example.myapplication

import android.annotation.SuppressLint
import android.content.ComponentCallbacks2

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.webkit.WebView

import android.widget.ImageView
import android.widget.ProgressBar

import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val web : WebView = findViewById(R.id.web)
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE
        ComponentCallbacks2.TRIM_MEMORY_MODERATE



        web.settings.javaScriptEnabled = true
        web.settings.domStorageEnabled =true
        web.loadUrl("url")








        Handler().postDelayed({

            web.visibility = View.VISIBLE

            val image : ImageView = findViewById(R.id.img1)
            image.visibility = View.INVISIBLE
            val pr : ProgressBar = findViewById(R.id.progressBar)
            pr.visibility = View.INVISIBLE

        }, 1700)



     
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.menu, menu)
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE
        ComponentCallbacks2.TRIM_MEMORY_MODERATE
        return true
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val web : WebView = findViewById(R.id.web)
        web.settings.javaScriptEnabled = true
        web.settings.domStorageEnabled =true
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE
        ComponentCallbacks2.TRIM_MEMORY_MODERATE

        val image : ImageView = findViewById(R.id.img1)
        return when (item.itemId) {

            R.id.live -> {
                val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("url"))
                startActivity(browserIntent)
                true
            }
            R.id.josve -> {



                web.loadUrl("url")

                true
            }
            R.id.about -> {
                web.clearCache(true)
                val ina = Intent(this@MainActivity, about::class.java)

                startActivity(ina)

                true
            }
            R.id.update -> {
                web.loadUrl("url")
                ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
                ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL
                ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE
                ComponentCallbacks2.TRIM_MEMORY_MODERATE
                true
            }
            R.id.homework -> {
                web.loadUrl("url")

                true
            }
            R.id.exam ->{
                web.loadUrl("url")
                true
            }
            R.id.shimi -> {
                web.loadUrl("url")
                web.visibility =View.INVISIBLE
                image.setImageResource(R.drawable.t5)
                image.visibility = View.VISIBLE
                Handler().postDelayed({

                    web.visibility = View.VISIBLE


                    image.visibility = View.INVISIBLE


                }, 500)

                true
            }
            R.id.phisik -> {
                web.loadUrl("url")
                web.visibility =View.INVISIBLE
                image.setImageResource(R.drawable.ph)
                image.visibility = View.VISIBLE
                Handler().postDelayed({

                    web.visibility = View.VISIBLE


                    image.visibility = View.INVISIBLE


                }, 500)
                true
            }
            R.id.math -> {
                web.loadUrl("url")
                web.visibility =View.INVISIBLE
                image.setImageResource(R.drawable.rcvtfcvojc20)
                image.visibility = View.VISIBLE
                Handler().postDelayed({

                    web.visibility = View.VISIBLE


                    image.visibility = View.INVISIBLE


                }, 500)
                true
            }
            R.id.hendese -> {
                web.loadUrl("url")
                web.visibility =View.INVISIBLE
                image.setImageResource(R.drawable.h)
                image.visibility = View.VISIBLE
                Handler().postDelayed({

                    web.visibility = View.VISIBLE


                    image.visibility = View.INVISIBLEenter code here


                }, 500)
                true
            }


            else -> super.onOptionsItemSelected(item)
        }
    }
    @SuppressLint("SetJavaScriptEnabled")
    override fun onBackPressed() {
        val web : WebView = findViewById(R.id.web)
        web.settings.javaScriptEnabled = true
        web.settings.domStorageEnabled =true
        web.loadUrl("url")

        }
  • Related