How can I force memory pressure for Android debugging?

前端 未结 1 1933
失恋的感觉
失恋的感觉 2021-01-04 07:39

I am trying to get a handle on some issues my Android app is having which I think are related to memory pressure when I am running in explicit \"foreground\" mode (Service.s

相关标签:
1条回答
  • 2021-01-04 07:51

    EDIT: (taking into account Christoph's comment)

    You can create artificial memory pressure. To do so, create a button in your MainActivity. Then, in the button's onClick method, create 10,000 objects (experiment with different amounts of objects). Then, while your app is running, click that button numerous times which should create a lot of memory pressure.

    The code could look like this:

    class MainActivity {
        ArrayList<String> stringArray = new ArrayList<>();
    
        ...
    
        public void click(View view) {
            // Add 10,000 String objects to Array List
    
            for (int i = 0; i < 10000; i++) {
            stringArray.add("A string");
            }
        }
    }
    

    Assuming that you set the button's onClick XML attribute to click.

    0 讨论(0)
提交回复
热议问题