sandeep03edu

Clear runtime memory

Jun 14th, 2022
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package com.sanedu.memorytesting;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.IntentFilter;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.  
  16.     private static final String TAG = "MainActivityTag";
  17.     Button clearMemory, increaseMemory;
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.  
  24.         clearMemory = findViewById(R.id.clear_memory);
  25.         increaseMemory = findViewById(R.id.inc_memory);
  26.  
  27.  
  28.         // Clear memory button
  29.         clearMemory.setOnClickListener(new View.OnClickListener() {
  30.             @Override
  31.             public void onClick(View view) {
  32.                 System.runFinalization();
  33.                 Runtime.getRuntime().gc();
  34.                 System.gc();
  35.                 printRuntimeMemory();
  36.             }
  37.         });
  38.         // Button used to increase memory
  39.         increaseMemory.setOnClickListener(new View.OnClickListener() {
  40.             @Override
  41.             public void onClick(View view) {
  42.                 incMemory();
  43.                 printRuntimeMemory();
  44.             }
  45.         });
  46.     }
  47.  
  48. // Function used to print runtime memory
  49.     private void printRuntimeMemory(){
  50.         Runtime runtime = Runtime.getRuntime();
  51.         long runtimeMax = -1;
  52.         long runtimeTotal = -1;
  53.         long runtimeFree = -1;
  54.         long runtimeUsed = -1;
  55.  
  56.         if (runtime != null) {
  57.             runtimeMax = runtime.maxMemory();
  58.             runtimeTotal = runtime.totalMemory();
  59.             runtimeFree = runtime.freeMemory();
  60.             runtimeUsed = runtimeTotal - runtimeFree;
  61.         }
  62.         Log.d(TAG, "printRuntimeMemory: Free: " + runtimeFree +" Used: " + runtimeUsed);
  63.     }
  64.  
  65. // Function used to increase memory usage
  66.     private void incMemory(){
  67.         IntentFilter filter = new IntentFilter();
  68.         filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
  69.  
  70.         BroadcastReceiver receiver = new BroadcastReceiver() {
  71.             @Override
  72.             public void onReceive(Context context, Intent intent) {
  73.                 //do something based on the intent's action
  74.             }
  75.         };
  76.         registerReceiver(receiver, filter);
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment