java 你如何在Android模拟器中模拟低内存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1483047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:42:19  来源:igfitidea点击:

How do you simulate low memory in the Android emulator?

javaandroidmobile

提问by Jacques René Mesrine

Is there an easy way to simulate low memory inside the emulator ? I'm interested to perform resource cleanups similar to this post

有没有一种简单的方法来模拟模拟器内的低内存?我有兴趣执行类似于这篇文章的资源清理

采纳答案by P Shved

Invoke ulimitcommand from the shell.

ulimit从外壳调用命令。

ulimit -Sv 10000

This will set current memory limit to 10 000 Kb, so that all apps invoked from this shell afterwards won't be able to access more memory.

这会将当前内存限制设置为 10 000 Kb,以便之后从此 shell 调用的所有应用程序将无法访问更多内存。

回答by Shai Barack

If you're just looking to trigger your onTrimMemory callbacks, then this will do the trick:

如果你只是想触发你的 onTrimMemory 回调,那么这可以解决问题:

adb shell am send-trim-memory

adb shell am send-trim-memory

e.g. adb shell am send-trim-memory com.example.app MODERATE

例如 adb shell am send-trim-memory com.example.app MODERATE

回答by Dmitriy Tarasov

Another way to do this, if your phone or emulator has application Dev Tools (each emulator has). Open Dev Tools, navigate Development Settings and turn on "Immediatly destroy activities" checkbox. This can help you emulate situation when your activity destroyed as on low memory

另一种方法是,如果您的手机或模拟器具有应用程序开发工具(每个模拟器都有)。打开开发工具,导航开发设置并打开“立即销毁活动”复选框。这可以帮助您模拟活动因内存不足而被破坏的情况