gpt4 book ai didi

java - 如何减少Android中的内存使用?

转载 作者:行者123 更新时间:2023-12-01 11:20:09 25 4
gpt4 key购买 nike

我想在按下按钮时显示可绘制文件夹中的一些随机图像。

我将我的照片命名为“img_0”...至“img_51”

这是我尝试过的:

ImageView card1, card2, card3, card4, card5;
Button bPlay;

ArrayList<Integer> numbers = new ArrayList<Integer>();
Random randomGenerator = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

setContentView(R.layout.activity_play);

setReferences();
}


private void setReferences() {
card1 = (ImageView) findViewById(R.id.card1);
card2 = (ImageView) findViewById(R.id.card2);
card3 = (ImageView) findViewById(R.id.card3);
card4 = (ImageView) findViewById(R.id.card4);
card5 = (ImageView) findViewById(R.id.card5);
bPlay = (Button) findViewById(R.id.bPlay2);
bPlay.setOnClickListener(this);
}

private void getRandomNumbers() {
while(numbers.size() < 5) {
int random = randomGenerator .nextInt(51);
if (!numbers.contains(random)) {
numbers.add(random);
}
}
}

private void clearArrayList() {
for(int i = numbers.size()-1 ; i >= 0; i--){
numbers.remove(numbers.get(i));
}
}

private void setBitmap(ImageView iv, int n) {
String str = "img_" + numbers.get(n);

int resID = getResources().getIdentifier(str, "drawable", getPackageName());
iv.setImageResource(resID);
}

@Override
public void onClick(View v) {
getRandomNumbers();

setBitmap(card1, 0);
setBitmap(card2, 1);
setBitmap(card3, 2);
setBitmap(card4, 3);
setBitmap(card5, 4);

clearArrayList();
}

我在模拟器中查看内存选项卡,发现内存使用率非常高...

如何减少内存使用?仅当我按一次按钮时,它才会在 VM HEAP 中显示 70 和某些内容。

任何想法都非常受欢迎!

我也尝试使用位图和回收,但它给了我错误..应用程序已停止工作...

这是我尝试过的:

private void setBitmap(ImageView iv, int n) {
String str = "img_" + numbers.get(n);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), getResourceID(str, "drawable",
this));
iv.setImageBitmap(bitmap);
bitmap.recycle();
}

这是日志猫:

    07-10 16:57:10.402    1906-1906/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
07-10 16:57:14.321 1906-1937/com.symplyfyweb.gherghina.oldschoolwesternpoker D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-10 16:57:14.335 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker D/﹕ HostConnection::get() New Host Connection established 0xb4a93f40, tid 1906
07-10 16:57:14.349 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker D/Atlas﹕ Validating map...
07-10 16:57:14.813 1906-1937/com.symplyfyweb.gherghina.oldschoolwesternpoker D/﹕ HostConnection::get() New Host Connection established 0xb4b270b0, tid 1937
07-10 16:57:14.957 1906-1937/com.symplyfyweb.gherghina.oldschoolwesternpoker I/OpenGLRenderer﹕ Initialized EGL, version 1.4
07-10 16:57:15.032 1906-1937/com.symplyfyweb.gherghina.oldschoolwesternpoker D/OpenGLRenderer﹕ Enabling debug mode 0
07-10 16:57:15.054 1906-1937/com.symplyfyweb.gherghina.oldschoolwesternpoker W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-10 16:57:15.063 1906-1937/com.symplyfyweb.gherghina.oldschoolwesternpoker W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb4b23b00, error=EGL_SUCCESS
07-10 16:57:15.169 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/Choreographer﹕ Skipped 38 frames! The application may be doing too much work on its main thread.
07-10 16:57:16.748 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/Choreographer﹕ Skipped 93 frames! The application may be doing too much work on its main thread.
07-10 16:57:22.533 1906-1937/com.symplyfyweb.gherghina.oldschoolwesternpoker W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-10 16:57:22.533 1906-1937/com.symplyfyweb.gherghina.oldschoolwesternpoker W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb4b23cc0, error=EGL_SUCCESS
07-10 16:57:23.378 1906-1913/com.symplyfyweb.gherghina.oldschoolwesternpoker W/art﹕ Suspending all threads took: 19.779ms
07-10 16:57:28.898 1906-1913/com.symplyfyweb.gherghina.oldschoolwesternpoker W/art﹕ Suspending all threads took: 12.510ms
07-10 16:57:31.940 1906-1918/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Clamp target GC heap from 66MB to 64MB
07-10 16:57:32.352 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Alloc sticky concurrent mark sweep GC freed 8(432B) AllocSpace objects, 0(0B) LOS objects, 1% free, 62MB/64MB, paused 1.468ms total 17.160ms
07-10 16:57:32.378 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Clamp target GC heap from 66MB to 64MB
07-10 16:57:32.378 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Alloc partial concurrent mark sweep GC freed 14(576B) AllocSpace objects, 0(0B) LOS objects, 1% free, 62MB/64MB, paused 889us total 19.090ms
07-10 16:57:32.444 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Clamp target GC heap from 66MB to 64MB
07-10 16:57:32.444 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Alloc concurrent mark sweep GC freed 8(12KB) AllocSpace objects, 0(0B) LOS objects, 1% free, 62MB/64MB, paused 789us total 57.650ms
07-10 16:57:32.451 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Forcing collection of SoftReferences for 12MB allocation
07-10 16:57:32.501 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Clamp target GC heap from 66MB to 64MB
07-10 16:57:32.502 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Alloc concurrent mark sweep GC freed 11(288B) AllocSpace objects, 0(0B) LOS objects, 1% free, 62MB/64MB, paused 799us total 49.442ms
07-10 16:57:32.507 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 13068012 byte allocation with 1263808 free bytes and 1234KB until OOM"
07-10 16:57:32.524 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Alloc sticky concurrent mark sweep GC freed 5(448B) AllocSpace objects, 0(0B) LOS objects, 1% free, 62MB/64MB, paused 731us total 14.526ms
07-10 16:57:32.550 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Clamp target GC heap from 66MB to 64MB
07-10 16:57:32.551 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Alloc partial concurrent mark sweep GC freed 10(376B) AllocSpace objects, 0(0B) LOS objects, 1% free, 62MB/64MB, paused 954us total 19.113ms
07-10 16:57:32.664 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Clamp target GC heap from 66MB to 64MB
07-10 16:57:32.665 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 1% free, 62MB/64MB, paused 1.193ms total 108.749ms
07-10 16:57:32.675 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker W/art﹕ Suspending all threads took: 9.507ms
07-10 16:57:32.675 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Forcing collection of SoftReferences for 12MB allocation
07-10 16:57:32.770 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Clamp target GC heap from 66MB to 64MB
07-10 16:57:32.770 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker I/art﹕ Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 1% free, 62MB/64MB, paused 11.517ms total 93.336ms
07-10 16:57:32.775 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 13068012 byte allocation with 1263992 free bytes and 1234KB until OOM"
07-10 16:57:32.777 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker D/skia﹕ --- allocation failed for scaled bitmap
07-10 16:57:32.779 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
07-10 16:57:32.802 1906-1906/com.symplyfyweb.gherghina.oldschoolwesternpoker E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.symplyfyweb.gherghina.oldschoolwesternpoker, PID: 1906
java.lang.OutOfMemoryError: Failed to allocate a 13068012 byte allocation with 1263992 free bytes and 1234KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:467)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:497)
at com.symplyfyweb.gherghina.oldschoolwesternpoker.PlayActivity.setBitmap(PlayActivity.java:97)
at com.symplyfyweb.gherghina.oldschoolwesternpoker.PlayActivity.onClick(PlayActivity.java:112)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

最佳答案

您确实应该缓存图像,以便它们只需要加载一次。

这非常简单,您所要做的就是当您加载位图时,检查它是否已经在缓存中,如果存在则使用它。如果不是,则加载图像。这将防止您多次加载资源。

HashMap<String, Integer> resCache = new HashMap<String, Integer>();
...
...
private void setBitmap(ImageView iv, int n) {
String str = "img_" + numbers.get(n);
int resID = -1;
if (resCache.containsKey(str)) {
resID = resCache.get (str);
} else {
resID = getResources().getIdentifier(str, "drawable", getPackageName());
resCache.put (str, resID);
}
iv.setImageResource(resID);
}

关于java - 如何减少Android中的内存使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31344849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com