Android 为什么当我启动服务时,当应用程序开始等待阻塞GC Alloc时会崩溃?
我将在应用程序启动之初启动一项Android 为什么当我启动服务时,当应用程序开始等待阻塞GC Alloc时会崩溃?,android,service,memory-leaks,garbage-collection,Android,Service,Memory Leaks,Garbage Collection,我将在应用程序启动之初启动一项服务(位置服务)。但是在应用程序上停留(大约5到10分钟)后,我不断地等待一个阻塞GC Alloc错误,我的应用程序被卡住了一段时间,然后突然崩溃。 此外,当我在没有服务的情况下运行应用程序时,它工作得很顺利 我已经找了好几天了,但什么也解决不了我的问题。我检查了许多相关的Q/A over Stack overflow,但似乎没有任何正确或有价值的答案 我还尝试在清单中添加了android:largeHeap=“true”和multiDexEnabled true,
服务
(位置服务)。但是在应用程序上停留(大约5到10分钟)后,我不断地等待一个阻塞GC Alloc错误,我的应用程序被卡住了一段时间,然后突然崩溃。
此外,当我在没有服务的情况下运行应用程序时,它工作得很顺利
我已经找了好几天了,但什么也解决不了我的问题。我检查了许多相关的Q/A over Stack overflow,但似乎没有任何正确或有价值的答案
我还尝试在清单中添加了android:largeHeap=“true”
和multiDexEnabled true
,但这些都没有帮助
这是我的服务课
public class LocationService extends IntentService {
private Context context;
private FusedLocationProviderClient fusedLocationClient;
private LocationRequest locationRequest;
Task<LocationSettingsResponse> task;
public static String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp",
CHANNEL_NAME = "My Background Service";
public LocationService() {
super("LocationService");
}
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else startForeground(8, new Notification());
createLocationRequest();
context = this;
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(context);
task = client.checkLocationSettings(builder.build());
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
try {
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
} catch (SecurityException ignore) {
Log.e("AppLocationService", "SecurityException - " + ignore.toString(), ignore);
}
return Service.START_STICKY;
}
private final LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
List<Location> locationList = locationResult.getLocations();
if (locationList.size() != 0) {
Location location = locationList.get(0);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Intent intent = new Intent("location_update");
intent.putExtra("latitude", lat);
intent.putExtra("longitude", lng);
sendBroadcast(intent);
}
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
}
protected void createLocationRequest() {
locationRequest = LocationRequest.create();
locationRequest.setInterval(2000);
locationRequest.setFastestInterval(2000);
locationRequest.setPriority(CommonObjects.LOCATION_PRIORITY);
}
}
公共类LocationService扩展了IntentService{
私人语境;
私有FusedLocationProviderClient fusedLocationClient;
私人位置请求位置请求;
任务;
公共静态字符串通知\u CHANNEL\u ID=“com.example.simpleapp”,
频道名称=“我的后台服务”;
公共场所服务{
超级(“定位服务”);
}
@凌驾
public void onCreate(){
super.onCreate();
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O)
startMyOwnForeground();
else startForeground(8,新通知());
createLocationRequest();
上下文=这个;
fusedLocationClient=LocationServices.getFusedLocationProviderClient(上下文);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
SettingsClient=LocationServices.getSettingsClient(上下文);
task=client.checkLocationSettings(builder.build());
}
@凌驾
public int-onStartCommand(@Nullable-Intent-Intent-Intent,int-flags,int-startId){
试一试{
fusedLocationClient.RequestLocationUpdate(locationRequest、locationCallback、Looper.getMainLooper());
}捕获(SecurityException忽略){
Log.e(“AppLocationService”,“SecurityException-”+ignore.toString(),ignore);
}
return Service.START\u STICKY;
}
私有最终位置回调LocationCallback=新位置回调(){
@凌驾
public void onLocationResult(LocationResult LocationResult){
List locationList=locationResult.getLocations();
如果(locationList.size()!=0){
Location=locationList.get(0);
双纬度=location.getLatitude();
double longitude=location.getLongitude();
意向意向=新意向(“位置更新”);
意向。额外(“纬度”,纬度);
意向。额外(“经度”,液化天然气);
发送广播(意图);
}
}
};
@可空
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
受保护的无效onHandleIntent(@Nullable Intent){
}
受保护的void createLocationRequest(){
locationRequest=locationRequest.create();
locationRequest.setInterval(2000);
locationRequest.SetFastTestInterval(2000年);
locationRequest.setPriority(CommonObjects.LOCATION\u PRIORITY);
}
}
我的日志猫
您能记录所有对
onStartCommand()
和onLocationResult()
的调用并重新发布日志吗?通过将所有调用放入onStartCommand()
中,我仍然得到相同的日志和行为!我的意思是,onLocationResult()
多久被调用一次?onLocationResult()
只被调用一次。你不觉得有点奇怪吗?听着,在你的服务课上没什么好看的。你为什么不更详细地介绍一下你所做的导致问题的改变;将此代码添加到您的问题中。现在可能是开始使用的好时机,看看是什么类型的对象导致您的内存被填满(我假设是这样)。您还应该使用CPU档案器来查看应用程序的工作量。