Memory Leak LibGDX ANdroid - android

I am making a live wallpaper app with libgdx in which I change the assets depending on the time. For. e.g from 6am to 6pm I have the "morning graphics" after that I have "evening graphics" from 6pm to 6am.
The way I have structured the Assets is as follows
I have 12 static arrays of type AtlasRegion 1 static Texture Region variable and 1 static texture variable.
I have two static functions loadMorning() and loadEvening() for loading the assets.
In the funcions I load as follows
For all arrays if they are not null do array.clear()
then load the regions. Dispose the TextureRegion variable and set texture variable to null before resetting their values.
The thing that is happening is that after every change of assets the memory seems to increasing
As a user's perepective I am using this app to see the memory
https://play.google.com/store/apps/details?id=mem.usage&hl=en
When I start my app for the first time .. it is showing as 68MB in the app ,
Day1 Morning Stats
Day 1
ID Heap Size Allocated Free %Used #Objects
1 10.812 MB 3.186 MB 7.626 MB 29.47% 45,405
Pss Private Private Swapped Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 0 0 0 0 16620 4285 38
Dalvik Heap 8692 8604 0 0 11072 3293 7779
Dalvik Other 1374 1216 0 0
Stack 96 96 0 0
Other dev 33016 4908 4 0
.so mmap 1266 692 136 0
.apk mmap 160 0 116 0
.dex mmap 287 20 8 0
Other mmap 5 4 0 0
Unknown 1431 1412 0 0
TOTAL 46327 16952 264 0 27692 7578 7817
Objects
Views: 1 ViewRootImpl: 0
AppContexts: 3 Activities: 0
Assets: 2 AssetManagers: 2
Local Binders: 11 Proxy Binders: 19
Death Recipients: 0
OpenSSL Sockets: 0
SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 0
Day 1 Evening Stats
adb log after evening assets are loaded
D/dalvikvm(2451): GC_FOR_ALLOC freed 1619K, 71% free 3281K/11072K, paused 14ms, total 15ms
D/dalvikvm(2451): GC_FOR_ALLOC freed 1517K, 71% free 3281K/11072K, paused 11ms, total 11ms
I/dalvikvm-heap(2451): Grow heap (frag case) to 6.548MB for 1331595-byte allocation
D/dalvikvm(2451): GC_CONCURRENT freed 1862K, 67% free 4127K/12376K, paused 2ms+2ms, total 13ms
D/dalvikvm(2451): GC_EXPLICIT freed 2384K, 74% free 3268K/12376K, paused 2ms+3ms, total 27ms
ID Heap Size Allocated Free %Used #Objects
1 10.816 MB 3.191 MB 7.625 MB 29.50% 45,525
This adb log right after change
Pss Private Private Swapped Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 0 0 0 0 16728 4346 29
Dalvik Heap 1654 1576 0 0 11076 3348 7728
Dalvik Other 1435 1296 0 0
Stack 100 100 0 0
Other dev 63332 32644 4 0
.so mmap 1110 692 116 0
.apk mmap 7 0 4 0
.dex mmap 586 20 368 0
Other mmap 5 4 0 0
Unknown 1504 1488 0 0
TOTAL 69733 37820 492 0 27804 7694 7757
Objects
Views: 1 ViewRootImpl: 0
AppContexts: 3 Activities: 0
Assets: 2 AssetManagers: 2
Local Binders: 10 Proxy Binders: 17
Death Recipients: 0
OpenSSL Sockets: 0
SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 0
The memory showing in the app is now 117MB
This keeps increasing , next morning the size shown in app is about 150 MB.
I need some pointers where to look to understand this better.

One remedy against resource leaks in the code that you cannot control is restarting the application. You either kill the process or do System.exit().
But before doing that, you have to schedule a restart.
Please note that although different android releases should all have been compatible, they are not compatible in application restart behavior. At least, v2.x and v4.0 did differ (in v2.x System.exit() caused application restart with the topmost activity closed), not sure about the latest releases.

Related

Dumpsys meminfo: kB or KB?

Is the memory returned by adb shell dumsys meminfo in kB or KB?
where:
kB = 1000 bytes
KB = 1024 bytes
It's KB (1024 bytes), or kibibytes (KiB), contrary to the kB notation in their printfs which is supposed to mean 1000 bytes as you noted.
Here's how I know. On my Linux box, if I do free -b,
total used free shared buffers cached
Mem: 67459153920 60865880064 6593273856 307834880 1373028352 40107618304
So look at the total value of 67459153920. That's in bytes (-b). Now if I look at meminfo,
MemTotal: 65878080 kB
and 67459153920 / 1024 = 65878080, so that value is in KB or aka KiB (1024 bytes).
https://en.wikipedia.org/wiki/Kilobyte
Note that I tested this on my Linux desktop because Android Linux doesn't happen to include the free command, but dumpsys meminfo is just reporting the results of cat /proc/meminfo which is implemented at the Linux kernel level of Android fs/proc/meminfo.c.

How to resolve the usage of the Gfx dev memory

I am using adb shell dumpsys meminfo to get informations about the current memory usage of my app.
The Gfx dev memory increases frequently and seems to cause memory leaks.
Is there an adb command or and Android Studio function to get more informations about the Gfx dev memory and where it is used/referenced?
Pss Private Private Swapped Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 17380 17324 0 0 36864 19380 17483
Dalvik Heap 30366 29796 0 0 50519 48334 2185
Dalvik Other 672 672 0 0
Stack 220 220 0 0
Gfx dev 4704 4704 0 0
Other dev 5 0 4 0
.so mmap 2109 356 872 0
.jar mmap 1 0 0 0
.apk mmap 707 0 420 0
.ttf mmap 35 0 4 0
.dex mmap 7442 0 7400 0
.oat mmap 2712 0 948 0
.art mmap 1458 948 72 0
Other mmap 42 4 0 0
EGL mtrack 13888 13888 0 0
GL mtrack 8924 8924 0 0
Unknown 172 172 0 0
TOTAL 90837 77008 9720 0 87383 67714 19668
Additional Info:
Gfx dev is not available for Kitkat devices and below.
You can use below command to get more info on Gfx;
adb shell dumpsys gfxinfo

Project Android compilation failed on EC2 instance

I use an EC2 instance (Ubuntu), and I want launch gradlew in my Android project, through the Terminal.
But I have this error:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':app'.
> Could not download artifact 'maven-ant-tasks.jar (org.apache.maven:maven-ant-tasks:2.1.3)'
> Failed to download resource 'https://jcenter.bintray.com/org/apache/maven/maven-ant-tasks/2.1.3/maven-ant-tasks-2.1.3.jar'.
> No space left on device
I checked the space, and Inodes and all is ok:
root#ip-xx-xx-xx-xx:/home/mytest/sandbox/MyApplication# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 99G 12G 83G 13% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 492M 12K 492M 1% /dev
tmpfs 100M 324K 99M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 497M 0 497M 0% /run/shm
none 100M 0 100M 0% /run/user
overflow 1.0M 0 1.0M 0% /tmp
root#ip-xx-xx-xx-xx:/home/mytest/sandbox/MyApplication# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/xvda1 6553600 241915 6311685 4% /
none 127032 2 127030 1% /sys/fs/cgroup
udev 125790 403 125387 1% /dev
tmpfs 127032 307 126725 1% /run
none 127032 1 127031 1% /run/lock
none 127032 1 127031 1% /run/shm
none 127032 2 127030 1% /run/user
overflow 127032 4 127028 1% /tmp
If I download manually this jar (maven-ant-tasks-2.1.3.jar) with wget command, the download is successful.
I tried with sudo privileges, and the same error appears (No space left on device).
Previously, I followed these explanations, to be sure about the space : EC2 instance on Amazon and I am greeted with "No space left on the disk". Again, the same error appears (No space left on device).
Thank you for your help guys!

How to get higher precision of "CPU%" than that from TOP command?

When I use TOP command, I could get the following info:
shell#android:/ $ top -n 1
User 31%, System 10%, IOW 0%, IRQ 0%
User 346 + Nice 10 + Sys 120 + Idle 637 + IOW 6 + IRQ 0 + SIRQ 2 = 1121
PID PR CPU% S #THR VSS RSS PCY UID Name
481 1 26% S 89 762832K 81688K fg system system_server
1699 0 5% S 27 676472K 39092K fg u0_a72 wm.cs.systemmonitor
11243 0 3% S 28 673140K 29796K bg u0_a111 com.weather.Weather
13327 2 1% S 23 680472K 35844K bg u0_a83 com.rhmsoft.fm
659 0 1% S 17 663044K 33136K bg u0_a13 android.process.media
20260 1 0% R 1 1208K 508K shell top
We can see the CPU% is round to integer, is there any way I could get a process's CPU% with higher precision?
-- Clarifications on the bounty -- Alex
The question refers to Android system, and preferably to a non-rooted device. While Android provides advanced profiling techniques for Java applications, tools for native code (C++) are limited. top command on Android allows to show the statistics for all threads running in the system, both Java threads and C++ threads. I am looking for an answer that will help with the following quest:
My app uses 2% CPU when it is inactive in background, while it should be below 0.1%. If I run top -t, I get 0% for all 15 threads that belong to my process (some threads are Java threads, e.g. the Main, UI thread; others are pthreads that never attach to JVM). How can I guess which thread eats the battery?
I would be glad to get even more details about this unexpected activity, and Android provides great helpers like TraceView for Java threads. Any insight regarding tools for native code will be highly appreciated.
You didn't mention it in your post, but in the comment you said that you really need CPU utilization per thread, not per process.
If you can't find a tool that's accurate enough, you can look directly in /proc/[pid]/task/[ThreadName] as described in the man page for /proc. This gives total CPU time consumed in "clock ticks" since execution began. Getting better resolution than this is probably difficult or impossible.
Edit
From the OP's comment, a command that lists the relevant information is:
adb shell cat /proc/${pid}/task/*/stat | awk -F\ '{print $1, $14}'
This just cats the correct /proc files to the debugging host, which runs a tiny awk program to print the columns for pid and user time. You could also easily use cut -d " " -f1,14 or something similar in perl to get the columns if awk isn't available.
Try this:
ps -eo pcpu,pid,user,args | sort -r -k1 | less
%CPU PID USER COMMAND
9.0 2721 user bash
1.4 956 root ...
0.5 2212 user ...
EDIT:
You can use adb shell and busybox (http://www.busybox.net/downloads/BusyBox.html)
adb shell busybox top
c:\ adb push busybox /system/bin
c:\ adb shell
# busybox top
CPU: 2.3% usr 3.1% sys 3.9% nic 90.5% idle 0.0% io 0.0% irq 0.0% sirq
Load average: 1.06 1.66 10.63 1/589 8048
←[7m PID PPID USER STAT VSZ %MEM CPU %CPU COMMAND←[0m
31619 2180 10112 S 217m 67.0 0 3.8 com.mgeek.android.DolphinBrowser.B
2232 2180 1000 S 551m169.6 0 2.6 system_server
8038 8037 0 R 2068 0.6 0 0.8 busybox top
2178 1 0 S 11092 3.3 0 0.6 /system/bin/drexe
6812 2180 10104 S 199m 61.2 0 0.5 android.tether
2291 2180 1001 S 324m 99.8 0 0.3 com.android.phone
2308 2180 10006 S 325m100.0 0 0.1 com.sec.android.app.dialertab
2177 1 1001 S 9624 2.8 0 0.1 /system/bin/rild
5 2 0 SW< 0 0.0 0 0.1 [events/0]
30087 2180 10022 S 358m110.4 0 0.0 com.samsung.vvm
2304 2180 10006 S 311m 96.0 0 0.0 com.sec.android.app.twlauncher
16110 2180 10006 S 296m 91.3 0 0.0 android.process.acore
2445 2180 10006 S 272m 83.8 0 0.0 com.sec.android.provider.logsprovi
8064 2180 10002 S 238m 73.4 0 0.0 com.google.process.gapps
31537 2180 10037 S 227m 69.9 0 0.0 com.google.android.gm
2288 2180 10048 S 221m 68.1 0 0.0 com.swype.android.inputmethod
2285 2180 10013 S 215m 66.3 0 0.0 com.tat.livewallpaper.aurora
30664 2180 10011 S 213m 65.8 0 0.0 com.android.email
31191 2180 10099 S 209m 64.4 0 0.0 com.sirma.mobile.bible.android
2377 2180 10087 S 207m 63.9 0 0.0 android.tts
(Taken from here)
Got this information from another thread:
3) Getting CPU info
~$ adb shell dumpsys cpuinfo
Output:
Load: 0.08 / 0.4 / 0.64
CPU usage from 42816ms to 34683ms ago:
system_server: 1% = 1% user + 0% kernel / faults: 16 minor
kdebuglog.sh: 0% = 0% user + 0% kernel / faults: 160 minor
tiwlan_wq: 0% = 0% user + 0% kernel
usb_mass_storag: 0% = 0% user + 0% kernel
pvr_workqueue: 0% = 0% user + 0% kernel
+sleep: 0% = 0% user + 0% kernel
+sleep: 0% = 0% user + 0% kernel
TOTAL: 6% = 1% user + 3% kernel + 0% irq
EDIT:
You can also try this command: echo $(adb shell ps | grep com.android.phone | awk '{ system("adb shell cat /proc/" $2 "/stat");}' | awk '{print $14+$15;}')
Also:
using top : This will show you the cpu stats
top -b -n 1 |grep ^Cpu
using ps: This will show you the % cpu usage for each process.
ps -eo pcpu,pid,user,args | sort -r -k1 | less
EDIT2:
In realtion to your comments and the bounty description (How can I guess which thread eats the battery?) I found an interesting page:
http://ziyang.eecs.umich.edu/projects/powertutor/
As stated there:
You can use PowerTutor to monitor the power consumption of any
application.
Try this for an instance and see if it meets your requirements.
FINAL EDIT:
Check out the Systrace documentation on the developer.android.com site:
http://developer.android.com/tools/debugging/systrace.html
http://developer.android.com/tools/help/systrace.html
I'm sorry if you already tried that, but that's one concrete method to measure the performance.
Use DDMS and method profiling to get a TraceView.
Basically:
Launch your app in debug mode
In DDMS, in the Devices tab, click "Start method profiling"
Do stuff on your device to recreate the conditions you want to monitor
Click "Stop method profiling"
You'll get a fairly detailed graph with each thread's execution that you can drill down into
More details here:
http://developer.android.com/tools/debugging/debugging-tracing.html
Disclaimer: I've only done this with a simple test app so I don't know how much mileage you'll get out of it. It does seem to give a bit more precision than what has been described so far, and does not require root.

How to speed up APK deployments?

In a prototype project a lot of assets & drawables are shipped within the APK.
There is no real option than having a res/ folder 18MB big.
As my phone (HTC Desire) doesn't have enough available memory, I have to use the emulator and notice how extremely slow this is.
Here is the output of the adb logcat command while deploying out of IntelliJ. What I don't understand mainly is why a file system format is being done on every deployment?
D/AndroidRuntime( 1031):
D/AndroidRuntime( 1031): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
D/AndroidRuntime( 1031): CheckJNI is ON
D/AndroidRuntime( 1031): --- registering native functions ---
D/dalvikvm( 270): GC_EXPLICIT freed 141 objects / 7400 bytes in 80ms
D/VoldCmdListener( 29): asec list
I/PackageHelper( 270): Size of container 18 MB 17245066 bytes
D/VoldCmdListener( 29): asec create smdl2tmp1 18 fat {} 10027
D/SntpClient( 59): request time failed: java.net.SocketException: Address family not supported by protocol
I//system/bin/newfs_msdos( 29): /system/bin/newfs_msdos: warning, /dev/block/dm-2 is not a character device
I//system/bin/newfs_msdos( 29): /system/bin/newfs_msdos: Skipping mount checks
I//system/bin/newfs_msdos( 29): Bogus heads from kernel - setting sane value
I//system/bin/newfs_msdos( 29): Bogus sectors from kernel - setting sane value
I//system/bin/newfs_msdos( 29): /dev/block/dm-2: 37376 sectors in 4672 FAT32 clusters (4096 bytes/cluster)
I//system/bin/newfs_msdos( 29): bps=512 spc=8 res=32 nft=2 sec=37485 mid=0xf0 spt=63 hds=64 hid=0 bspf=37 rdcl=2 infs=1 bkbs=2
I/logwrapper( 29): /system/bin/newfs_msdos terminated by exit(0)
I/Vold ( 29): Filesystem formatted OK
D/VoldCmdListener( 29): asec path smdl2tmp1
I/PackageHelper( 270): Created secure container smdl2tmp1 at /mnt/asec/smdl2tmp1
I/DefContainer( 270): Created container for smdl2tmp1 at path : /mnt/asec/smdl2tmp1
Now - this takes most time due to file transfer I guess.
Once finished:
I/DefContainer( 270): Copied /data/local/tmp/com.myapp.android to /mnt/asec/smdl2tmp1/pkg.apk
D/VoldCmdListener( 29): asec finalize smdl2tmp1
I/DefContainer( 270): Finalized container smdl2tmp1
I/DefContainer( 270): Unmounting smdl2tmp1 at path /mnt/asec/smdl2tmp1
D/dalvikvm( 270): GC_EXPLICIT freed 166 objects / 13208 bytes in 115ms
D/VoldCmdListener( 29): asec unmount smdl2tmp1 force
D/VoldCmdListener( 29): asec mount smdl2tmp1 {} 1000
D/VoldCmdListener( 29): asec path smdl2tmp1
D/PackageParser( 59): Scanning package: /mnt/asec/smdl2tmp1/pkg.apk
D/dalvikvm( 270): GC_EXPLICIT freed 75 objects / 3664 bytes in 234ms
I either didn't notice on any other APP before, but something seems to be too much here?
Emulator is on Froyo, 2.2.
Any thoughts or ideas on how to speed up this?
Else I guess my laptop is too slow and the whole "asec", encrypted APK things, take too long. Would be nice to speed that up too.
Thanks.
Looks like the secure container stuff is due to the app being installed to SD card by default. Try changing your preferred location to internal storage. This might speed things up a bit.

Categories

Resources