简体   繁体   中英

Calling Calendar class functions produces GC_CONCURRENT messages

I am writing a little calendar app for Android and I need some functions the Calendar class contains (for example: calculate first day of month, current day of month, day of next month, year of a specific Unix timestamp and so on)

These functions are called frequently and they trigger GC_Concurrent pretty often which causes my app to lag.

This is the way I access the calendar functions:

private static Calendar cal;

public static int getDayOfMonth(long sec) {
    if (cal == null)
        cal = Calendar.getInstance(TimeZone.getDefault());

    //cal.clear();
    cal.setTimeInMillis(sec * 1000L);
    return cal.get(Calendar.DAY_OF_MONTH);
}

Is there a better way to access the functions without producing so much garbage or a better way to calculate the things I need?

First question: Have you profiled the code? How certain are you that this is the problem? If you have profiled and you are certain...


Have you tried using Joda Time or Joda Time Android ?

Java's default support for dates and times is really terrible, generally speaking.

You can do the same code using Joda time by doing:

public static int getDayOfMonth(long sec) {
  DateTime time = new DateTime(sec * 1000L);
  return time.getDayOfMonth();
}

You can't stop stop garbage collection, it is a daemon thread.

It's meant to improve performance; if it's slowing your program, than there likely is a inefficient use of objects/other design flaw causing the garbage collector to have redundant tasks.

I don't want to repeat what you probably already know, but you should look @ this post, it should prove useful.

Android CalendarView slowing down layout

-Eric

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM