The Garbage collection is slowing down my application running? - java

Here is the Fragment I am running, It used for running an if statement with dots, every time the image moves. As well as this theres an automatic timer where the images moved based on a time that I set. The app worked well until I added in the dots feature. It is still working but very slowly and cuts out of maybe 2/3 minutes of it running. Is there parts of this code I can change or delete or can I run the app on external memory? mayeb this would speed it up a bit. Ive tried to set the drawables as local variables but still no change. Is there anything else I can do.
public class HomeFragment extends Fragment {
ViewPager viewPager;
ViewPagerAdapter adapter;
LinearLayout sliderDotsPanel;
private int dotsCount;
private ImageView[] dots;
Drawable active_dots;
Drawable nonactive_dots;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
viewPager = (ViewPager) view.findViewById(R.id.view_pager);
sliderDotsPanel = (LinearLayout) view.findViewById(R.id.SliderDots);
active_dots = getDrawable(getActivity().getApplicationContext(), R.drawable.active_dot);
nonactive_dots = getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot);
adapter = new ViewPagerAdapter(this.getActivity());
viewPager.setAdapter(adapter);
dotsCount = adapter.getCount();
dots = new ImageView[dotsCount];
for (int i = 0; i < dotsCount; i++) {
dots[i] = new ImageView(getActivity());
dots[i].setImageDrawable(nonactive_dots);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
sliderDotsPanel.addView(dots[i], params);
}
dots[0].setImageDrawable(active_dots);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
for (int i = 0; i < dotsCount; i++) {
dots[i].setImageDrawable(nonactive_dots);
}
dots[position].setImageDrawable(active_dots);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Timer timer = new Timer();
timer.scheduleAtFixedRate(new myTimerTask(), 4000, 2000);
return view;
}
public class myTimerTask extends TimerTask {
#Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
while (true) {
if (viewPager.getCurrentItem() == 0) {
viewPager.setCurrentItem(1);
} else if (viewPager.getCurrentItem() == 1) {
viewPager.setCurrentItem(2);
} else if (viewPager.getCurrentItem() == 2) {
viewPager.setCurrentItem(1);
} else if (viewPager.getCurrentItem() == 1) {
viewPager.setCurrentItem(0);
}
}
}
});
}
}
}
LogCat Below
07-26 14:19:44.486 2873-2873/com.example.aids.a09application W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
07-26 14:19:44.565 2873-2873/com.example.aids.a09application I/Choreographer: Skipped 117 frames! The application may be doing too much work on its main thread.
07-26 14:19:46.622 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 787(89KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 198MB/198MB, paused 18.992ms total 102.229ms
07-26 14:19:46.952 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 997(40KB) AllocSpace objects, 0(0B) LOS objects, 1% free, 198MB/202MB, paused 15.431ms total 173.177ms
07-26 14:19:47.181 2873-2878/com.example.aids.a09application I/art: Do partial code cache collection, code=30KB, data=27KB
07-26 14:19:47.194 2873-2878/com.example.aids.a09application I/art: After code cache collection, code=21KB, data=23KB
07-26 14:19:47.194 2873-2878/com.example.aids.a09application I/art: Increasing code cache capacity to 128KB
07-26 14:19:49.800 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 37941(1634KB) AllocSpace objects, 0(0B) LOS objects, 1% free, 201MB/205MB, paused 12.622ms total 296.630ms
07-26 14:19:52.394 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 36700(1551KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 203MB/205MB, paused 13.130ms total 169.524ms
07-26 14:19:52.639 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 54270(3MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 200MB/204MB, paused 18.500ms total 234.230ms
07-26 14:19:54.910 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 66332(3MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 201MB/205MB, paused 14.535ms total 197.841ms
07-26 14:19:57.531 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 75191(4MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 201MB/205MB, paused 14.242ms total 205.903ms
07-26 14:19:59.749 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 35759(1500KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 203MB/205MB, paused 12.808ms total 147.647ms
07-26 14:20:00.920 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 12875(540KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 204MB/205MB, paused 13.586ms total 113.208ms
07-26 14:20:01.340 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 95614(6MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 198MB/202MB, paused 16.117ms total 152.367ms
07-26 14:20:01.403 2873-2879/com.example.aids.a09application I/art: Thread[3,tid=2879,WaitingInMainSignalCatcherLoop,Thread*=0x9de8de00,peer=0x12c2a8b0,"Signal Catcher"]: reacting to signal 3
07-26 14:20:01.882 2873-2879/com.example.aids.a09application I/art: Wrote stack traces to '/data/anr/traces.txt'
07-26 14:20:05.687 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 45945(2MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 201MB/205MB, paused 47.723ms total 976.583ms
07-26 14:20:09.410 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 33212(1393KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 203MB/205MB, paused 18.694ms total 163.982ms
07-26 14:20:10.980 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 93472(5MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 199MB/203MB, paused 17.361ms total 219.162ms
07-26 14:20:13.670 2873-2880/com.example.aids.a09application W/art: Suspending all threads took: 10.526ms
07-26 14:20:13.771 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 52852(2MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 201MB/205MB, paused 15.208ms total 196.607ms
07-26 14:20:17.211 2873-2880/com.example.aids.a09application W/art: Suspending all threads took: 44.452ms
07-26 14:20:17.421 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 35062(1470KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 203MB/205MB, paused 15.334ms total 273.230ms
07-26 14:20:19.082 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 89559(5MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 199MB/203MB, paused 21.820ms total 251.707ms

Delete the while (true) { just before the loop. works fine now

Related

Java Android - Either there is not enough memory for the backgroud task or there's a problem with the GC

My android app is supposed to read about 25kb from a InputStream which is wrapped in a BufferedReader. This happens in a background Thread that communicates via a Handler. But when starting to read the Thread seems to be blocked and a debug message shows up:
09-29 22:56:27.837 13625-13640/de.jzbor.epos I/art: Background sticky concurrent mark sweep GC freed 519353(7MB) AllocSpace objects, 0(0B) LOS objects, 77% free, 2MB/10MB, paused 797us total 109.196ms
I don't know how to handle or bypass it and I wasn't able to find something useful on the internet. I would be very thankful for any tipps or solutions.
Here is the code and the log:
Method:
#Override
public void run() {
if (request == null)
return;
int responseHash = 0;
int returnCode = 0;
String timestamp = "0";
int responseType = 0;
Object returnObject = "";
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if ((networkInfo == null) || (!networkInfo.isConnectedOrConnecting())) {
returnCode = 1;
} else {
try {
socket = new Socket(HOST, PORT);
PrintWriter pw = new PrintWriter(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw.println("get " + request);
pw.flush();
String responseString = "";
Log.d("EpOs", "1");
while (!br.ready());
int i = 0;
while (br.ready()){
Log.d("EpOs", "ln"+i++);
char c;
c = (char)br.read();
if (c != 0)
responseString += Character.toString(c);
}
Log.d("EpOs", "1.5");
System.out.println(responseString);
timestamp = new SimpleDateFormat("E HH:mm").format(new Date());
switch (request) {
case "service/vertretungsplan": {
try {
responseType = 101;
SubstituteDay sd = SubstitutePlanGetter.getSubstitutions(responseString)[0];
} catch (URISyntaxException | ImplicitLoginException e) {
e.printStackTrace();
returnCode = 21;
}
break;
}
default: {
returnCode = 2;
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
returnCode = 3;
}
Log.d("EpOs", "2");
Integer rc = returnCode;
Object[] objects = new Object[3];
objects[0] = rc;
objects[1] = timestamp;
objects[2] = returnObject;
Message msg = handler.obtainMessage(responseType, objects);
msg.sendToTarget();
}
Logcat:
09-29 23:12:42.866 21779-21779/? I/art: Late-enabling -Xcheck:jni
09-29 23:12:43.278 21779-21779/de.jzbor.epos I/InstantRun: starting instant run server: is main process
09-29 23:12:43.476 21779-21779/de.jzbor.epos W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-29 23:12:43.533 21779-21779/de.jzbor.epos D/EpOs: Test
09-29 23:12:44.150 21779-21779/de.jzbor.epos I/ViewRootImpl: CPU Rendering VSync enable = false
09-29 23:12:44.153 21779-21804/de.jzbor.epos D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-29 23:12:44.215 21779-21804/de.jzbor.epos I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: ()
OpenGL ES Shader Compiler Version: E031.25.03.04
Build Date: 04/28/15 Tue
Local Branch:
Remote Branch:
Local Patches:
Reconstruct Branch:
09-29 23:12:44.217 21779-21804/de.jzbor.epos I/OpenGLRenderer: Initialized EGL, version 1.4
09-29 23:12:44.231 21779-21804/de.jzbor.epos D/OpenGLRenderer: Enabling debug mode 0
09-29 23:12:44.396 21779-21779/de.jzbor.epos W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
09-29 23:12:44.501 21779-21779/de.jzbor.epos I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy#69cc367 time:42529086
09-29 23:12:48.039 21779-21829/de.jzbor.epos D/EpOs: 1
09-29 23:12:56.169 21779-21794/de.jzbor.epos I/art: Background sticky concurrent mark sweep GC freed 569396(9MB) AllocSpace objects, 0(0B) LOS objects, 77% free, 2MB/10MB, paused 768us total 112.713ms
09-29 23:13:03.718 21779-21794/de.jzbor.epos I/art: Background partial concurrent mark sweep GC freed 523840(8MB) AllocSpace objects, 0(0B) LOS objects, 83% free, 2MB/14MB, paused 656us total 116.436ms
09-29 23:13:14.916 21779-21794/de.jzbor.epos I/art: Background sticky concurrent mark sweep GC freed 784615(11MB) AllocSpace objects, 0(0B) LOS objects, 77% free, 2MB/10MB, paused 489us total 122.385ms
09-29 23:13:29.955 21779-21794/de.jzbor.epos I/art: Background sticky concurrent mark sweep GC freed 521041(7MB) AllocSpace objects, 0(0B) LOS objects, 77% free, 2MB/10MB, paused 727us total 103.527ms
09-29 23:15:34.196 21779-22644/de.jzbor.epos D/EpOs: 1
09-29 23:16:24.117 21779-21794/de.jzbor.epos I/art: Background sticky concurrent mark sweep GC freed 514462(7MB) AllocSpace objects, 0(0B) LOS objects, 76% free, 2MB/10MB, paused 539us total 101.177ms
09-29 23:17:09.050 21779-21794/de.jzbor.epos I/art: Background sticky concurrent mark sweep GC freed 514702(7MB) AllocSpace objects, 0(0B) LOS objects, 75% free, 2MB/10MB, paused 607us total 106.134ms

java android Choreographer: Skipped 271 frames

My application when I click a buttons is freezing and in anroid monit I see :
03-22 13:01:33.490 25636-26150/com.smok.maps D/dalvikvm: GC_FOR_ALLOC freed 2468K, 11% free 29508K/32996K, paused 65ms, total 65ms
03-22 13:01:36.914 25636-26150/com.smok.maps D/dalvikvm: GC_FOR_ALLOC freed 3957K, 17% free 28675K/34324K, paused 54ms, total 54ms
03-22 13:01:37.744 25636-25636/com.smok.maps I/Choreographer: Skipped 271 frames! The application may be doing too much work on its main thread.
03-22 13:01:39.907 25636-26150/com.smok.maps D/dalvikvm: GC_FOR_ALLOC freed 3903K, 19% free 27910K/34324K, paused 42ms, total 42ms
This is an action what I do :
btUnselectAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
// for (RowBean aRowBean_data : rowBeen) {
// aRowBean_data.setSelected(false);
// }
for (RowBean rowBean : Adapter.data) {
rowBean.setSelected(false);
}
adapter.notifyDataSetChanged();
for (ObjectDefExtends objectDefExtends : Singleton.getInstance().getListaODE()) {
objectDefExtends.visible = false;
editor.putInt(objectDefExtends.id.toString(), 0);
editor.apply();
}
}
});

android.os.TransactionTooLargeException occurs when Writing/Reading JSON with Gson in AsyncTask

In my App, I save E-Mails with GSON to an .json File. Because of freezing the UI, I've to do this in Background. I tried with a "new Thread" but I got a Parsing-Error of Gson, so I'm trying with AsyncTask now...
There my logcat puts out this:
03-12 14:36:34.750 30486-30504/at.guger.email I/art: Background sticky
concurrent mark sweep GC freed 274498(12MB) AllocSpace objects, 0(0B)
LOS objects, 27% free, 21MB/29MB, paused 5.096ms total 69.641ms 03-12
14:36:35.281 30486-30504/at.guger.email I/art: Background sticky
concurrent mark sweep GC freed 170008(7MB) AllocSpace objects, 0(0B)
LOS objects, 25% free, 21MB/29MB, paused 5.615ms total 54.870ms 03-12
14:36:35.741 30486-30504/at.guger.email I/art: Background sticky
concurrent mark sweep GC freed 157620(6MB) AllocSpace objects, 0(0B)
LOS objects, 24% free, 22MB/29MB, paused 5.676ms total 59.539ms 03-12
14:36:36.212 30486-30504/at.guger.email I/art: Background partial
concurrent mark sweep GC freed 151786(6MB) AllocSpace objects, 0(0B)
LOS objects, 40% free, 22MB/38MB, paused 9.185ms total 107.879ms 03-12
14:36:37.193 30486-30504/at.guger.email I/art: Background sticky
concurrent mark sweep GC freed 355102(14MB) AllocSpace objects, 0(0B)
LOS objects, 24% free, 24MB/32MB, paused 12.054ms total 130.218ms
03-12 14:36:37.753 30486-30504/at.guger.email I/art: Background sticky
concurrent mark sweep GC freed 181500(6MB) AllocSpace objects,
97(1195KB) LOS objects, 23% free, 24MB/32MB, paused 8.087ms total
105.010ms
This is repeated for maybe 100 times..
Then this is put out:
03-12 14:37:02.858 30486-31452/at.guger.email E/AndroidRuntime: FATAL
EXCEPTION: AsyncTask #3
Process: at.guger.email, PID: 30486
java.lang.RuntimeException: An error occured while executing
doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.StackOverflowError: stack size 1036KB
at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:111)
at com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson$Types.java:553)
at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:111)
at com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson$Types.java:546)
at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:111)
14:37:03.078 30486-30504/at.guger.email I/art: Background partial
concurrent mark sweep GC freed 2071991(33MB) AllocSpace objects,
16(10MB) LOS objects, 30% free, 36MB/52MB, paused 2.105ms total
219.940ms 03-12 14:37:03.088 30486-31452/at.guger.email D/Error: ERR: exClass=java.lang.StackOverflowError 03-12 14:37:03.088
30486-31452/at.guger.email D/Error: ERR: exMsg=stack size 1036KB 03-12
14:37:03.088 30486-31452/at.guger.email D/Error: ERR:
file=$Gson$Types.java 03-12 14:37:03.088 30486-31452/at.guger.email
D/Error: ERR: class=com.google.gson.internal.$Gson$Types 03-12
14:37:03.088 30486-31452/at.guger.email D/Error: ERR:
method=canonicalize line=111 03-12 14:37:03.108
30486-30499/at.guger.email W/art: Suspending all threads took:
10.559ms 03-12 14:37:03.118 30486-31452/at.guger.email D/Error: ERR: stack=java.lang.RuntimeException: An error occured while executing
doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.StackOverflowError: stack size 1036KB
at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:111)
at com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson$Types.java:553)
30486-31452/at.guger.email D/Error: ERR: TOTAL BYTES WRITTEN: 2240396
03-12 14:37:03.118 30486-31452/at.guger.email E/JavaBinder: !!! FAILED
BINDER TRANSACTION !!! 03-12 14:37:03.118 30486-31452/at.guger.email
E/AndroidRuntime: Error reporting crash
android.os.TransactionTooLargeException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:496)
at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4200)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:89)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
My Code worked if I tried to do it on MainThread:
File dir = new File(mailParcel.getContext().getFilesDir() + "/emails/" + mailParcel.getDirectory() + "/" + foldername);
if (!dir.exists()) dir.mkdirs();
for (File f : dir.listFiles()) {
f.delete();
}
String fileName;
File file;
Mail[] Mails = mailParcel.getMails();
for (int i = 0; i < Mails.length; i++) {
fileName = Mails[i].getReceiveDate().toString().replace(" ", "_").replace(":", "-");
try {
file = new File(dir, fileName + ".json");
file.createNewFile();
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
Gson gson = new GsonBuilder().create();
gson.toJson(Mails[i], writer);
writer.close();
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
catch (UnsupportedEncodingException usee) {
usee.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
I hope you can help me!
If you have another idea instead of for example doing in Asynctask, tell me please!
The answer is really simple...
I made an AsyncTask instead of the Thread.
Then the code works.

App stopped working Android Studio

How many activities can I put in my project how many pictures? I make one project and have now 13 activities also and when I have between 5 and 8 activities, my project works wonderfully. Nothing stacks, nothing crashes and now I have 13 activities and when I build my project and open it, the welcome screen works and I can open one, two or three other activities but on 4 or 5 activities open and my app stops working and crashes. I think it is out of memory, but I don't understand why it happens because I have all of my bitmaps resized and compressed and when I start a new activity after startActivity() I put finish().
Here is one java code from nocnizivotActivity.java
package teslicinfo.teslic;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class nocnizivotActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nocnizivot);
//tnt
LinearLayout lyt_tnt =(LinearLayout)findViewById(R.id.lyt_tnt);
lyt_tnt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "tnt");
startActivity(i);
finish();
}
});
//moja kafanica
LinearLayout lyt_mojakafanica =(LinearLayout)findViewById(R.id.lyt_mojakafanica);
lyt_mojakafanica.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "mojakafanica");
startActivity(i);
finish();
}
});
//kaktus
LinearLayout lyt_kaktus =(LinearLayout)findViewById(R.id.lyt_kaktus);
lyt_kaktus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "kaktus");
startActivity(i);
finish();
}
});
//caffex
LinearLayout lyt_caffex =(LinearLayout)findViewById(R.id.lyt_caffex);
lyt_caffex.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "caffex");
startActivity(i);
finish();
}
});
//faraon
LinearLayout lyt_faraon =(LinearLayout)findViewById(R.id.lyt_faraon);
lyt_faraon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "faraon");
startActivity(i);
finish();
}
});
//lane
LinearLayout lyt_trg =(LinearLayout)findViewById(R.id.lyt_trg);
lyt_trg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "trg");
startActivity(i);
finish();
}
});
//lane
LinearLayout lyt_lane =(LinearLayout)findViewById(R.id.lyt_lane);
lyt_lane.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "lane");
startActivity(i);
finish();
}
});
//kastel
LinearLayout lyt_kastel =(LinearLayout)findViewById(R.id.lyt_kastel);
lyt_kastel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "kastel");
startActivity(i);
finish();
}
});
//cepelin
LinearLayout lyt_cepelin =(LinearLayout)findViewById(R.id.lyt_cepelin);
lyt_cepelin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "cepelin");
startActivity(i);
finish();
}
});
LinearLayout lyt_dzungla =(LinearLayout)findViewById(R.id.lyt_dzungla);
lyt_dzungla.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data","dzungla");
startActivity(i);
finish();
}
});
ImageView imgnazad_nocnizivot =(ImageView)findViewById(R.id.imgnazad_nocnizivot);
imgnazad_nocnizivot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), pocetakActivity.class);
startActivity(i);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_nocnizivot, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here is the nocnizivot_layout.xml code
And this is my logcat:
11-10 10:49:40.169 2704-2704/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
11-10 10:49:40.169 2704-2704/? I/art﹕ Late-enabling JIT
11-10 10:49:40.169 2704-2704/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
11-10 10:49:40.219 2704-2704/teslicinfo.teslic W/System﹕ ClassLoader referenced unknown path: /data/app/teslicinfo.teslic-2/lib/x86
11-10 10:49:40.547 2704-2717/teslicinfo.teslic I/art﹕ Background partial concurrent mark sweep GC freed 81(3KB) AllocSpace objects, 0(0B) LOS objects, 38% free, 6MB/10MB, paused 1.464ms total 120.600ms
11-10 10:49:40.677 2704-2713/teslicinfo.teslic W/art﹕ Suspending all threads took: 16.632ms
11-10 10:49:40.742 2704-2717/teslicinfo.teslic I/art﹕ Background sticky concurrent mark sweep GC freed 21(864B) AllocSpace objects, 0(0B) LOS objects, 0% free, 21MB/21MB, paused 1.278ms total 117.549ms
11-10 10:49:40.926 2704-2724/teslicinfo.teslic D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-10 10:49:40.972 2704-2704/teslicinfo.teslic D/﹕ HostConnection::get() New Host Connection established 0xab9908d0, tid 2704
11-10 10:49:41.045 2704-2724/teslicinfo.teslic D/﹕ HostConnection::get() New Host Connection established 0xab990dd0, tid 2724
11-10 10:49:41.066 2704-2724/teslicinfo.teslic I/OpenGLRenderer﹕ Initialized EGL, version 1.4
11-10 10:49:41.151 2704-2724/teslicinfo.teslic W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-10 10:49:41.151 2704-2724/teslicinfo.teslic W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad92c960, error=EGL_SUCCESS
11-10 10:49:44.407 2704-2724/teslicinfo.teslic W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-10 10:49:44.407 2704-2724/teslicinfo.teslic W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xac13e200, error=EGL_SUCCESS
11-10 10:49:44.893 2704-2724/teslicinfo.teslic E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab9ac980
11-10 10:49:46.143 2704-2717/teslicinfo.teslic W/art﹕ Suspending all threads took: 13.446ms
11-10 10:49:46.147 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.147 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.149 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.149 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.149 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.178 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.178 2704-2704/teslicinfo.teslic I/art﹕ Alloc concurrent mark sweep GC freed 267(24KB) AllocSpace objects, 0(0B) LOS objects, 3% free, 30MB/32MB, paused 1.195ms total 27.810ms
11-10 10:49:46.182 2704-2704/teslicinfo.teslic I/art﹕ Forcing collection of SoftReferences for 1447KB allocation
11-10 10:49:46.182 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.195 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.195 2704-2704/teslicinfo.teslic I/art﹕ Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 3% free, 30MB/32MB, paused 3.038ms total 12.420ms
11-10 10:49:46.198 2704-2704/teslicinfo.teslic W/art﹕ Throwing OutOfMemoryError "Failed to allocate a 1481772 byte allocation with 1096520 free bytes and 1070KB until OOM"
11-10 10:49:46.198 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.199 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.199 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.199 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.199 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.211 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.211 2704-2704/teslicinfo.teslic I/art﹕ Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 3% free, 30MB/32MB, paused 0 total 10.985ms
11-10 10:49:46.214 2704-2704/teslicinfo.teslic I/art﹕ Forcing collection of SoftReferences for 1447KB allocation
11-10 10:49:46.214 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.227 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.227 2704-2704/teslicinfo.teslic I/art﹕ Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 3% free, 30MB/32MB, paused 1.276ms total 12.712ms
11-10 10:49:46.229 2704-2704/teslicinfo.teslic W/art﹕ Throwing OutOfMemoryError "Failed to allocate a 1481772 byte allocation with 1096520 free bytes and 1070KB until OOM"
11-10 10:49:46.231 2704-2704/teslicinfo.teslic D/skia﹕ --- allocation failed for scaled bitmap
11-10 10:49:46.233 2704-2704/teslicinfo.teslic D/AndroidRuntime﹕ Shutting down VM
11-10 10:49:46.233 2704-2704/teslicinfo.teslic E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: teslicinfo.teslic, PID: 2704
java.lang.OutOfMemoryError: Failed to allocate a 1481772 byte allocation with 1096520 free bytes and 1070KB 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.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.Resources.getDrawable(Resources.java:806)
at android.content.Context.getDrawable(Context.java:458)
at android.widget.ImageView.resolveUri(ImageView.java:811)
at android.widget.ImageView.setImageResource(ImageView.java:418)
at teslicinfo.teslic.diskotekeActivity.diskoteke_dzungla(diskotekeActivity.java:21)
at teslicinfo.teslic.diskotekeActivity.onCreate(diskotekeActivity.java:437)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-10 10:49:47.991 2704-2704/? I/Process﹕ Sending signal. PID: 2704 SIG: 9
AFAIK 13 activies are supported so i guess the problem is somewhere else..maybe you did not give enough ram to your AVD.
Anyway i feel to adivice you to use fragment.
Try this may help you add this tag in your manifest file.
<application
...
...
android:largeHeap="true"
......
......
</application>
it will allocate large heap for your app
Or try this
May cause this. For creating Bitmaps memory-efficient you should use BitmapOptions
Bitmap bm;
bm = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(filepath),100, 100, true);
mPicture = new ImageView(context);
mPicture.setImageBitmap(bm);
check this Strange out of memory issue while loading an image to a Bitmap object

Android MultiThread SpeedTest have performances problems

I've started to do a speedtest application recently.
The method is simple :
I instanciate multiple threads all downloading the same uncompressible file, then i calculate the average download speed.
But I've got some performances problems I think. When i start the test with more than one thread, i see a lot of GC_CONCURRENT freed and WAIT_FOR_CONCURRENT_GC in the logCat.
I don't understand where's that comming from.
Habitually these problems seems to appear in certain circumpstances, like instanciating too much objects, or huge objects, but i don't think it's the case here...
Here is my code (simplified - without speed calculation):
public class SpeedTest extends Activity{
private final int NB_THREADS = 2;
private long bytesIn;
private long downloadTime;
private List<Downloader> downloaders = new ArrayList<Downloader>();
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.speed_test);
bytesIn = 0;
for (int i = 0; i < NB_THREADS; i++){
downloaders.add(new Downloader());
downloaders.get(i).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, i);
}
}
private synchronized void addBytes(){ bytesIn++; }
private class Downloader extends AsyncTask<Integer, Void, Void>{
#Override
protected Void doInBackground(Integer... params){
Log.d("eduine", "Downloader " + params[0] + " started");
try{
InputStream stream = new URL("http://test-de-vitesse.ariase.com/ariasetool/1mb_random.bin").openConnection().getInputStream();
while((stream.skip(1)) != 0){
addBytes();
}
stream.close();
}catch (MalformedURLException e){
e.printStackTrace();
Log.e("eduine", "malformed url : " + e.getMessage());
}catch (IOException e){
e.printStackTrace();
Log.e("eduine", "io error : " + e.getMessage());
}
Log.d("eduine", "Downloader " + params[0] + " terminated");
return null;
}
}
}
So I'd like to know if someone already experienced this problem, or have any idea why is there these performances problems, because it distorts the speed calculations.
Thanks in advance!
EDIT : forgot to post the logCat, sorry.
02-12 14:18:40.945: D/eduine(15637): Downloader 0 started
02-12 14:18:40.955: D/eduine(15637): Downloader 1 started
02-12 14:18:41.355: D/dalvikvm(15637): GC_CONCURRENT freed 3102K, 33% free 7552K/11240K, paused 3ms+3ms, total 20ms
02-12 14:18:41.395: D/dalvikvm(15637): GC_CONCURRENT freed 1564K, 33% free 7533K/11240K, paused 3ms+3ms, total 18ms
02-12 14:18:41.465: D/dalvikvm(15637): GC_CONCURRENT freed 1657K, 33% free 7537K/11240K, paused 2ms+7ms, total 20ms
02-12 14:18:41.465: D/dalvikvm(15637): WAIT_FOR_CONCURRENT_GC blocked 8ms
02-12 14:18:41.485: D/dalvikvm(15637): GC_CONCURRENT freed 1657K, 33% free 7541K/11240K, paused 2ms+3ms, total 17ms
02-12 14:18:41.485: D/dalvikvm(15637): WAIT_FOR_CONCURRENT_GC blocked 10ms
02-12 14:18:41.505: D/dalvikvm(15637): GC_CONCURRENT freed 1669K, 33% free 7533K/11240K, paused 3ms+1ms, total 17ms
02-12 14:18:41.505: D/dalvikvm(15637): WAIT_FOR_CONCURRENT_GC blocked 10ms
02-12 14:18:41.525: D/dalvikvm(15637): GC_CONCURRENT freed 1609K, 33% free 7537K/11240K, paused 3ms+3ms, total 16ms
02-12 14:18:41.545: D/dalvikvm(15637): GC_CONCURRENT freed 1669K, 34% free 7529K/11240K, paused 3ms+2ms, total 16ms
02-12 14:18:41.545: D/dalvikvm(15637): WAIT_FOR_CONCURRENT_GC blocked 13ms
etc etc... until
02-12 14:21:14.795: D/dalvikvm(16164): GC_CONCURRENT freed 1537K, 33% free 7533K/11240K, paused 3ms+2ms, total 16ms
02-12 14:21:14.815: D/dalvikvm(16164): GC_CONCURRENT freed 1537K, 33% free 7533K/11240K, paused 2ms+2ms, total 15ms
02-12 14:21:14.835: D/dalvikvm(16164): GC_CONCURRENT freed 1537K, 33% free 7533K/11240K, paused 2ms+3ms, total 16ms
02-12 14:21:14.845: D/eduine(16164): Downloader 0 terminated at 1392211274859
02-12 14:21:14.855: D/dalvikvm(16164): GC_CONCURRENT freed 1545K, 34% free 7529K/11240K, paused 3ms+3ms, total 17ms
02-12 14:21:14.915: D/eduine(16164): Downloader 1 terminated at 1392211274925
EDIT : same problem using ThreadPool, code below.
public class SpeedTest extends Activity{
private final int NB_OF_THREADS = 8;
private long bytesIn;
private long start;
ExecutorService executorService = new ThreadPoolExecutor(NB_OF_THREADS, NB_OF_THREADS, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(NB_OF_THREADS, true), new ThreadPoolExecutor.CallerRunsPolicy());
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.speed_test);
bytesIn = 0;
start = System.currentTimeMillis();
for (int i = 0; i < NB_OF_THREADS; i++) executorService.submit(new Downloader(i));
}
private synchronized void addByte(){ bytesIn++; }
private class Downloader implements Runnable{
private int id;
private InputStream stream;
private long downloadTime;
public Downloader(int _id){ id = _id; }
#Override
public void run(){
Log.d("eduine", "Downloader " + id + " started at " + System.currentTimeMillis());
try{
HttpURLConnection connexion = (HttpURLConnection) new URL("http://test-de-vitesse.ariase.com/ariasetool/1mb_random.bin").openConnection();
connexion.setUseCaches(false);
stream = connexion.getInputStream();
while((stream.skip(1)) != 0) addByte();
stream.close();
downloadTime = (System.currentTimeMillis() - start);
downloadTime = downloadTime == 0 ? 1 : downloadTime;
}catch (MalformedURLException e){
e.printStackTrace();
Log.e("eduine", "malformed url : " + e.getMessage());
}catch (IOException e){
e.printStackTrace();
Log.e("eduine", "io error : " + e.getMessage());
}
Log.d("eduine", "Downloader " + id + " terminated at " + System.currentTimeMillis());
}
}
}
Thread objects themselves consume much memory for their stacks. To clean your test from instantiate/free threads, do not use AsyncTask (which creates Thread), but create a fixed threadpool beforehand, start test, wait it to end, and only then shutdown the thread pool.

Categories

Resources