问题描述
我知道它是我应该只是谷歌,但也许你们能救我的某个时候。
我试着在我的Android应用程序运行日志处理后的某个时候我得到了内存不足的exeption。
我想知道为什么它的发生?
类LongOperation扩展的AsyncTask<字符串,太虚,字符串> {
@覆盖
保护字符串doInBackground(字符串... PARAMS){
INT系数= 59;
INT factorSuperNum = 35;
INT NUM;
名单<整数GT; alreadyIn =新的ArrayList<整数GT;();
StringBuilder的SB =新的StringBuilder();
对于(INT J = 0; J< 5000000; J ++){
alreadyIn.clear();
的for(int i = 0;我小于5;我++){
NUM =(int)的(1 +(的Math.random()*因子)%因子);
而(alreadyIn.contains(NUM))
NUM =(int)的(1 +(的Math.random()*因子)%因子);
alreadyIn.add(NUM);
}
如果(j%100000 == 0)
Log.i(SendEmail,J +);
NUM =(int)的(1 +(的Math.random()* factorSuperNum)%factorSuperNum);
alreadyIn.add(NUM);
对于(INT finalNum:alreadyIn){
sb.append(finalNum +);
}
sb.append(\ N);
}
返回null;
}
}
这code是坏的这么多的情况下...
第一个(的Math.random()*因子)%系数
为什么要乘以由随机因素?你知道,随机%X
是坏?
第二OOME是因为你在每一次迭代中添加行的StringBuilder打完5M迭代至少需要10MB(5M * 20-40bytes)
THRID连续服用5的因素,您可以使用ArrayList的使用范围从1到因子和洗牌,并采取冷杉5 elments:
进口的java.util。*;
进口的java.lang。*;
进口java.io. *;
类LOLOLOLOLOL
{
静态最终诠释F = 59;
静态最终诠释FSN = 35;
静态最后的ArrayList<整数GT; range1toF =新的ArrayList<整数GT;();
静态最终随机RND =新的随机();
静态{
的for(int i = 1; I< F;我++)
range1toF.add(ⅰ);
}
公共静态无效的主要(字串[] args)抛出java.lang.Exception的
{
最后的PrintStream PS =新的PrintStream(System.out的); //或者从文件
的for(int i = 0;我小于20;我++){
Collections.shuffle(range1toF,RND);
为(诠释J = 0; J&小于5; J ++){
ps.print(range1toF.get(J));
ps.print(,);
}
ps.println(rnd.nextInt(FSN - 1)+ 1);
}
}
}
I know its something i should just google, but maybe you guys can save me sometime.
I tried running a log process in my android app and after sometime i got a out of memory exeption.
I am trying to understand why did it happend?
class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
int factor = 59;
int factorSuperNum = 35;
int num;
List<Integer> alreadyIn = new ArrayList<Integer>();
StringBuilder sb = new StringBuilder();
for (int j=0; j<5000000; j++){
alreadyIn.clear();
for(int i=0;i<5;i++){
num = (int) (1 + (Math.random() * factor) % factor);
while (alreadyIn.contains(num))
num = (int) (1 + (Math.random() * factor) % factor);
alreadyIn.add(num);
}
if(j % 100000 == 0)
Log.i("SendEmail", j + "" );
num = (int) (1 + (Math.random() * factorSuperNum) % factorSuperNum);
alreadyIn.add(num);
for (int finalNum : alreadyIn) {
sb.append(finalNum + " ");
}
sb.append("\n");
}
return null;
}
}
this code is bad at so many cases ...
first (Math.random() * factor) % factor
why you are multiplying random by factor? do you know that random % x
is bad?
second OOME is because you add line to StringBuilder in every iteration so after 5M iteration it takes at least 10MB (5M * 20-40bytes)
thrid for taking 5 from factor you can use ArrayList with range from 1 to factor and shuffle it and take firs 5 elments:
import java.util.*;
import java.lang.*;
import java.io.*;
class LOLOLOLOLOL
{
static final int F = 59;
static final int FSN = 35;
static final ArrayList<Integer> range1toF = new ArrayList<Integer>();
static final Random rnd = new Random();
static{
for(int i = 1; i < F; i++)
range1toF.add(i);
}
public static void main (String[] args) throws java.lang.Exception
{
final PrintStream ps = new PrintStream(System.out); //or from File
for(int i = 0; i < 20; i++){
Collections.shuffle(range1toF, rnd);
for(int j = 0; j< 5; j++){
ps.print(range1toF.get(j));
ps.print(", ");
}
ps.println(rnd.nextInt(FSN - 1) + 1);
}
}
}
这篇关于Android的 - 内存不足。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!