/** * Licensed under the MIT License * * Copyright (c) 2008 jinten.net * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ package { import flash.sampler.NewObjectSample; import flash.utils.Dictionary; import net.jinten.debug.utils.DebugUtils; /** * Profilerの機能を使用するための基本クラスです。 アプリケーションに対して、1つのインスタンスしか生成できません。 * @author ken - Jinten.net * @version 0.2 */ public class Profiler { //----- Variables -----// private static var _instance:Profiler = null; /** * Profilerインスタンスです。初めてこのプロパティを呼んだときにインスタンスが生成され、プロファイルが開始されます。 * ただし、プロファイルを行うためには、バージョン9,0,115,0以上のDebugプレイヤで再生されている必要があります。 * アプリケーションを再生しているFlashPlayerが条件を満たしていない場合はnullを返します。 */ public static function get instance():Profiler { if (_instance == null && DebugUtils.isProfilerEnabled()) { _instance = new Profiler(new SingletonEssence); } return _instance; } private var _running:Boolean = false; /** * 監視中か否かのフラグです。 */ public function get running():Boolean { return _running; } //----- Private Variables -----// private static var _dict:Dictionary = new Dictionary(); //----- methods -----// /** * コンストラクタ * 直接インスタンスを生成することはできません。 * instanceプロパティを参照してください。 */ public function Profiler(essense:SingletonEssence) { if (essense == null) { throw Error('use Profiler.instance'); } startSampling(); } /** * メモリの監視を開始します。 * @return */ public function startSampling():void { _running = true; flash.sampler.startSampling(); } /** * メモリの監視を一時的に停止します。  * @return  */ public function pauseSampling():void { _running = false; flash.sampler.pauseSampling(); } /** * メモリの監視を完全に停止します。(現在監視中のデータも破棄されます)  * @return  */ public function stopSampling():void { _running = false; flash.sampler.stopSampling(); } /** * startSampling()メソッド以後に生成されたインスタンスについて解析した結果をStringで返します。 * @return 解析結果 */ public function getWatchString():String { pauseSampling(); watch(); var str:String = ''; for (var type:String in _dict) { var d:Dictionary = _dict[type] as Dictionary; var num:int = 0; for (var exist_object:* in d) { num++; } str += type + ':' + num + '\n'; } startSampling(); return str; } /** * @private * startSampling()メソッド以後に生成されたインスタンスについて解析します。 */ private function watch():void { //pauseSampling(); var samples:Object = flash.sampler.getSamples(); for each(var sample:Object in samples) { if (sample is NewObjectSample) { var nos:NewObjectSample = NewObjectSample(sample); var type:String = String(nos.type); if (nos.object != undefined) { if (!(_dict[type] is Dictionary)) { _dict[type] = new Dictionary(true); } _dict[type][nos.object] = null; } } } flash.sampler.clearSamples(); //startSampling(); } } } class SingletonEssence { }