为什么ical4j这么久建? [英] Why is ical4j taking so long to build?
问题描述
我试图解析在Android中使用ical4j谷歌日历的iCal(的.ics)文件。但它接管40秒打造从输入流的日历。
I am trying to parse a google calendar ical (.ics) file using ical4j in android. But its taking over 40 seconds to build the calendar from the input stream.
calendar = builder.build(fis);
该iCal文件仅在大小150KB。
此外,当我使用相同的code和在PC上运行它,日历的建设发生在不到一秒钟。
我也注意到在LogCat中大量垃圾收集。
任何一个能帮助我吗?
The ical file is only 150KB in size. Also, When I use the same code and run it in PC, the building of calendar takes place in less than a second. I have also noticed huge amounts of Garbage Collection in the LogCat. Can Any one help me?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView = (TextView)findViewById(R.id.Hello_World);
new Download().execute();
}
final class Download extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute(){
TextView.setText("Downloading");
}
@Override
protected Void doInBackground(Void... arg0) {
try {
URL url = new URL(URL);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.connect();
FileOutputStream fos = openFileOutput(fileName, MainActivity.MODE_PRIVATE);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d("log_tag", "Error: " + e);
}
return null;
}
@Override
protected void onPostExecute(Void Result) {
TextView.setText("Saved...Loading Data");
new Loadicaldata().execute();
}
}
final class Loadicaldata extends AsyncTask<Void, Void, Void> {
String Summary = null;
@Override
protected Void doInBackground(Void... arg0) {
FileInputStream fis = null;
try {
fis = openFileInput(fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_UNFOLDING, true);
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION, true);
CalendarBuilder builder = new CalendarBuilder();
Calendar calendar = null;
try {
calendar = builder.build(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
b.append(calendar.getProperty("X-WR-CALNAME").getValue());
ComponentList events = calendar.getComponents(Component.VEVENT);
VEvent Event = (VEvent) events.get(0);
Summary = Event.getDescription().getValue();
/*
for (Object event : calendar.getComponents(Component.VEVENT)) {
if (((VEvent) event).getSummary() != null) {
b.append("\n\n");
b.append(((VEvent) event).getSummary().getValue());
b.append(": ");
b.append(((VEvent) event).getStartDate().getDate());
}
}
*/
return null;
}
@Override
protected void onPostExecute(Void Result) {
TextView.setText(Summary);
}
}
LogCat中:
http://dl.dropbox.com/u/35866688/LogCat.txt
另外,我可以肯定地排除IO错误的可能性,因为Calendar.load方法采用长为好。
Also, I can safely rule out the possibility of IO error, as Calendar.load method takes that long as well.
这是iCal文件,如果有人有兴趣。
<一href=\"https://www.google.com/calendar/ical/m0es4hhj4g9d69ibak88tvoup0%40group.calendar.google.com/public/basic.ics\" rel=\"nofollow\">https://www.google.com/calendar/ical/m0es4hhj4g9d69ibak88tvoup0%40group.calendar.google.com/public/basic.ics
This is the ical file if anyone is interested. https://www.google.com/calendar/ical/m0es4hhj4g9d69ibak88tvoup0%40group.calendar.google.com/public/basic.ics
推荐答案
一种可能性是,你是从 doInBackground
法无缓冲输入流中读取。如果 CalendarBuilder.build(...)
方法一次读取一个字节,这会产生大量的系统调用,而放慢改革的步伐显著。
One possibility is that you are reading from an unbuffered input stream in the doInBackground
method. If the CalendarBuilder.build(...)
method reads one byte at a time, this will generate a lot of system calls, and slow things down significantly.
一个第二个可能性是,该问题是由垃圾收集引起的。有没有很多可以做的,但增加堆大小可能的可能的帮助。 (过度GC开销的原因之一是关闭不好,如果他们不能在每个GC循环回收的内存运行与堆是接近满。地方选区尾部的效率...)
A second possibility is that the problem is caused by garbage collection. There's not a lot you can do about that, but increasing the heap size might might help. (One cause of excessive GC overheads is running with a heap that is close to full. The efficiency of GCs tail off badly if they are unable to reclaim much memory in each GC cycle ... )
这篇关于为什么ical4j这么久建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!