1. 程式人生 > >mixare的measureText方法在頻繁調用時拋出“referencetable overflow max 1024”的解決方式

mixare的measureText方法在頻繁調用時拋出“referencetable overflow max 1024”的解決方式

def ken post zygote 內存泄露 oot col idt stack

這幾天在搞基於位置的AR應用,採用了github上兩款開源項目:

mixare

android-argument-reality-framework

這兩個項目實現機制大致同樣。我選取的是android-argument-reality-framework。原因是我覺得他的代碼結構要清晰非常多(純屬個人意見)。

這兩個項目的demo在執行時都會crash,通過查看控制臺,能夠看到例如以下信息:

07-31 14:35:38.685: W/dalvikvm(13686): ReferenceTable overflow (max=1024)
07-31 14:35:38.685: W/dalvikvm(13686): JNI pinned array reference table (0x5fc810) dump:
07-31 14:35:38.685: W/dalvikvm(13686):   Last 10 entries (of 1024):
07-31 14:35:38.685: W/dalvikvm(13686):      1023: 0x4130ada8 char[] (29 elements)
07-31 14:35:38.685: W/dalvikvm(13686):      1022: 0x4130a768 char[] (29 elements)
07-31 14:35:38.685: W/dalvikvm(13686):      1021: 0x413ad8d0 char[] (29 elements)
07-31 14:35:38.685: W/dalvikvm(13686):      1020: 0x413aa990 char[] (36 elements)
07-31 14:35:38.685: W/dalvikvm(13686):      1019: 0x40ea2eb0 char[] (5 elements)
07-31 14:35:38.685: W/dalvikvm(13686):      1018: 0x40e262d8 char[] (5 elements)
07-31 14:35:38.685: W/dalvikvm(13686):      1017: 0x40ee8000 char[] (4 elements)
07-31 14:35:38.685: W/dalvikvm(13686):      1016: 0x40e97418 char[] (4 elements)
07-31 14:35:38.685: W/dalvikvm(13686):      1015: 0x413a9e38 char[] (22 elements)
07-31 14:35:38.685: W/dalvikvm(13686):      1014: 0x413a9858 char[] (33 elements)
07-31 14:35:38.685: W/dalvikvm(13686):   Summary:
07-31 14:35:38.685: W/dalvikvm(13686):         1 of byte[] (128 elements)
07-31 14:35:38.685: W/dalvikvm(13686):        50 of char[] (4 elements) (2 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        50 of char[] (5 elements) (2 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        25 of char[] (22 elements) (25 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        24 of char[] (23 elements) (24 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        24 of char[] (24 elements) (24 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        24 of char[] (25 elements) (24 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        24 of char[] (27 elements) (24 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        24 of char[] (28 elements) (24 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):       508 of char[] (29 elements) (508 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        24 of char[] (31 elements) (24 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        49 of char[] (33 elements) (49 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        25 of char[] (34 elements) (25 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        50 of char[] (36 elements) (50 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        25 of char[] (37 elements) (25 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        49 of char[] (39 elements) (49 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        24 of char[] (44 elements) (24 unique instances)
07-31 14:35:38.685: W/dalvikvm(13686):        24 of char[] (63 elements) (24 unique instances)
07-31 14:35:38.685: E/dalvikvm(13686): Failed adding to JNI pinned array ref table (1024 entries)
07-31 14:35:38.685: I/dalvikvm(13686): "main" prio=5 tid=1 RUNNABLE
07-31 14:35:38.685: I/dalvikvm(13686):   | group="main" sCount=0 dsCount=0 obj=0x40a59460 self=0x343830
07-31 14:35:38.685: I/dalvikvm(13686):   | sysTid=13686 nice=0 sched=0/0 cgrp=default handle=1074517384
07-31 14:35:38.685: I/dalvikvm(13686):   | schedstat=( 23316582697 3886607959 27513 ) utm=2194 stm=137 core=0
07-31 14:35:38.685: I/dalvikvm(13686):   at android.graphics.Paint.native_measureText(Native Method)
07-31 14:35:38.685: I/dalvikvm(13686):   at android.graphics.Paint.measureText(Paint.java:1288)
07-31 14:35:38.685: I/dalvikvm(13686):   at org.mixare.lib.gui.PaintScreen.getTextWidth(PaintScreen.java:153)
07-31 14:35:38.685: I/dalvikvm(13686):   at org.mixare.lib.gui.TextObj.prepTxt(TextObj.java:107)
07-31 14:35:38.685: I/dalvikvm(13686):   at org.mixare.lib.gui.TextObj.<init>(TextObj.java:65)
07-31 14:35:38.685: I/dalvikvm(13686):   at org.mixare.lib.gui.TextObj.<init>(TextObj.java:50)
07-31 14:35:38.685: I/dalvikvm(13686):   at org.mixare.marker.ImageMarker.drawTitle(ImageMarker.java:135)
07-31 14:35:38.685: I/dalvikvm(13686):   at org.mixare.marker.ImageMarker.draw(ImageMarker.java:120)
07-31 14:35:38.685: I/dalvikvm(13686):   at org.mixare.DataView.draw(DataView.java:252)
07-31 14:35:38.685: I/dalvikvm(13686):   at org.mixare.AugmentedView.onDraw(MixView.java:1411)
07-31 14:35:38.685: I/dalvikvm(13686):   at android.view.View.draw(View.java:10978)
07-31 14:35:38.685: I/dalvikvm(13686):   at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
07-31 14:35:38.685: I/dalvikvm(13686):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-31 14:35:38.685: I/dalvikvm(13686):   at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
07-31 14:35:38.685: I/dalvikvm(13686):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-31 14:35:38.685: I/dalvikvm(13686):   at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
07-31 14:35:38.685: I/dalvikvm(13686):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-31 14:35:38.685: I/dalvikvm(13686):   at android.view.View.draw(View.java:10981)
07-31 14:35:38.685: I/dalvikvm(13686):   at android.widget.FrameLayout.draw(FrameLayout.java:450)
07-31 14:35:38.685: I/dalvikvm(13686):   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2126)
07-31 14:35:38.690: I/dalvikvm(13686):   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2026)
07-31 14:35:38.690: I/dalvikvm(13686):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)
07-31 14:35:38.690: I/dalvikvm(13686):   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-31 14:35:38.690: I/dalvikvm(13686):   at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 14:35:38.690: I/dalvikvm(13686):   at android.os.Looper.loop(Looper.java:137)
07-31 14:35:38.690: I/dalvikvm(13686):   at android.app.ActivityThread.main(ActivityThread.java:4575)
07-31 14:35:38.690: I/dalvikvm(13686):   at java.lang.reflect.Method.invokeNative(Native Method)
07-31 14:35:38.690: I/dalvikvm(13686):   at java.lang.reflect.Method.invoke(Method.java:511)
07-31 14:35:38.690: I/dalvikvm(13686):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-31 14:35:38.690: I/dalvikvm(13686):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-31 14:35:38.690: I/dalvikvm(13686):   at dalvik.system.NativeStart.main(Native Method)
07-31 14:35:38.690: E/dalvikvm(13686): VM aborting
通過

id=145">https://code.google.com/p/mixare/issues/detail?id=145和

id=36908">https://code.google.com/p/android/issues/detail?id=36908大致了解到了原因就是在頻繁調用native_measureText可能出現了內存泄露,可是始終沒有好的解決方式。

最後經過一番折騰,最終在

id=22">https://code.google.com/p/android-augment-reality-framework/issues/detail?

id=22裏找到的解決的,給出的解決方式例如以下:

http://stackoverflow.com/questions/16813706/android-apps-activity-force-back。

改動android-augment-reality-framework項目的src/com/jwetherell/augmented_reality/ui/objects/PaintableObject.java中的getTextWidth方法為例如以下代碼就可以:

/**
	 * Get the width of the text String.
	 * 
	 * @param txt
	 *            CharSequence to get the width of.
	 * @param start
	 *            Start of the text.
	 * @param end
	 *            End of the text.
	 * @return float width of the text String.
	 * @throws NullPointerException
	 *             if the String param is NULL.
	 */
	public float getTextWidth(CharSequence text) {
		if (text == null)
			throw new NullPointerException();

		TextPaint textPaint = new TextPaint(paint);
		int widthWrap = 1000; // you may have to change this
		StaticLayout layout = measure(textPaint, text, widthWrap);
		return getMaxLineWidth(layout);
	}

	private float getMaxLineWidth(StaticLayout layout) {
		float maxLine = 0.0f;
		int lineCount = layout.getLineCount();
		for (int i = 0; i < lineCount; i++) {
			if (layout.getLineWidth(0) > maxLine) {
				maxLine = layout.getLineWidth(0);
			}
		}
		return maxLine;
	}

	private StaticLayout measure(TextPaint textPaint, CharSequence text,
			Integer wrapWidth) {
		int boundedWidth = Integer.MAX_VALUE;
		if (wrapWidth != null && wrapWidth > 0) {
			boundedWidth = wrapWidth;
		}
		StaticLayout layout = new StaticLayout(text, textPaint, boundedWidth,
				Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
		return layout;
	}



mixare的measureText方法在頻繁調用時拋出“referencetable overflow max 1024”的解決方式