java.lang.OutOfMemoryError: GC overhead limit exceeded

Insert HTML at Cursor/Caret within a Content Editable Div

27. July 2014 03:29 by Scott in javascript, jquery, C#, Development  //  Tags:   //   Comments (0)

First we need to capture the cursor position within the Div it self.

$scope.editorSelection;

    $scope.SetupEditor = function () {
        var editable = document.getElementById('editor'),
selection, range;
        var captureSelection = function (e) {
            // Don't capture selection outside editable region
            var isOrContainsAnchor = false,
                isOrContainsFocus = false,
                sel = window.getSelection(),
                parentAnchor = sel.anchorNode,
                parentFocus = sel.focusNode;

            while (parentAnchor && parentAnchor != document.documentElement) {
                if (parentAnchor == editable) {
                    isOrContainsAnchor = true;
                }
                parentAnchor = parentAnchor.parentNode;
            }

            while (parentFocus && parentFocus != document.documentElement) {
                if (parentFocus == editable) {
                    isOrContainsFocus = true;
                }
                parentFocus = parentFocus.parentNode;
            }

            if (!isOrContainsAnchor || !isOrContainsFocus) {
                return;
            }
            selection = window.getSelection();

            // Get range (standards)
            if (selection.getRangeAt !== undefined) {
                range = selection.getRangeAt(0);

                // Get range (Safari 2)
            } else if (
                document.createRange &&
                selection.anchorNode &&
                selection.anchorOffset &&
                selection.focusNode &&
                selection.focusOffset
            ) {
                range = document.createRange();
                range.setStart(selection.anchorNode, selection.anchorOffset);
                range.setEnd(selection.focusNode, selection.focusOffset);
            } else {
                // Failure here, not handled by the rest of the script.
                // Probably IE or some older browser
            }
            $scope.editorSelection = range;
        };
        editable.onkeyup = captureSelection;
    }

 

From here, we need to be able to insert HTML at the selection when we choose to.

    function insertHtmlAfterSelection(html) {
        var range, expandedSelRange, node;
        if ($scope.editorSelection) {
            range = $scope.editorSelection;
            expandedSelRange = range.cloneRange();
            range.collapse(false);
            var el = document.createElement("div");
            el.innerHTML = html;
            var frag = document.createDocumentFragment(), node, lastNode;
            while ((node = el.firstChild)) {
                lastNode = frag.appendChild(node);
            }
            range.insertNode(frag);
        }
    }

 

That will be it.

I Use Bing to Search, Google to Search Hard

7. December 2011 12:39 by Scott in Development  //  Tags: ,   //   Comments (103)

There comes a point in my life where I just love free stuff.  Bills are hard, employment sucks at times, but free stuff is always great.

I am a coder by day and a coder by night.  I run into hard problems that just can’t be answered without a reference to some other place in the world. 

Right now, I am picking up PHP because of a side project my work wants me to investigate.  While PHP is nice and all, there are just some things that can’t be done.  So we have to add the extra layer of JavaScript on top of it.  Yay, JavaScript!  But then again, I consider my self a decent JavaScript developer, but not an awesome one.  For the past few days I have been learning JavaScript's parsing engine for XML.  Its pretty cool and gets the job done when I need it.  When I run into a problem, I just go to my browser and start searching.  My default search engine in BING.  Please don’t chastise me as a developer.  Hear me out. 

BING has these points, if you accrue enough, you get free stuff. The Xbox points do it for me.  So Im an avid BING searcher.  Because of this, Ive been able to buy several arcade games on Xbox, and even get free HULU subscriptions.  So as you can see, there is a big reason why to use BING.  Free stuff.  Swag as many of us software folks call it.

So here is the big problem.  Bing is good for those average every day searches.  The easy ones.  Flights, Weather, Directions etc…, but for those hard to find searches and mostly in my realm, those programming questions, BING is spammed.  You can’t search in Bing for any type of developer question without hitting some type of spam.  Mostly that damn e-freedom crap.  I can’t stand it.  So when I go searching for those hard to find results, I will use Google. 

Google always shows the best answers up top.  The spam is always below the original thoughts.  If only Bing knew to sort duplicate results by Date and Time. What ever the crawler “craws” over first should be higher than those results it “craws” over second.  Things would probably go a lot easier and I would use Bing more often for those free points.

So Bing, you might have me using you for those easy searches, but until you clean up your spam, I won’t be using you much more than that.

Multi-Threading with C# and a Website

23. November 2011 00:40 by Scott in Development, C#  //  Tags: , , ,   //   Comments (6)

Let me tell you my experience.

I have a website at http://www.utopiapimp.com that gets hit well over 1 Million times a day.  Thats 11 hits every Second.  Utopiapimp works on being a Live Stock Ticker for a online game called Utopia.  My users need live feeds 24/7 of whats happening in the game and thats what my application does for it.  Live feeds 24/7 for all users.  Its extremely dynamic as well.  Always changing, always updating.

My server that hosts my website is a VPS located with the company Ultima Hosts.  I have a middle tiered plan that costs a bit to host.  The advertising on Pimp pays for the hosting fees, but it doesn’t cover any more than that.  I have 4 GBs of Ram and 1 CPU.  I store so much in the caching system, that I take and use up at times almost all 4 GBs of Ram.  The problem mainly is the CPU.  At times the CPU hits 80% used and other times it coasts around 15% used.  So I was hoping to bring in some multi threading into Pimp to make the inserts into the DB much faster and a much faster response time for the users.

I started with just one thread, the new type of tasking in C# introduced in .Net 4.0.

Task.Factory.StartNew(() => DoSomeWork());

The Problem with this I think, is I only have one CPU.  When I implemented this method, I sadly and immediately saw a block.  The application started to hang.  I spent the next few weeks to try and stop the hang.  Yes, sadly, I introduced a bug so bad, I immediately started getting user connections loss.  I introduced a ton of code when I introduced this bug, so I was completely lost on the topic.  I thought well, it had to be something else.  Not until I wrapped the task around my error handling code to insert errors into the DB, did I realize what was going on. So I tried threading instead.

System.Threading.Thread newThread = new System.Threading.Thread(anObject.AMethod);
newThread.Start();

This didn’t work either.  I then realized, well maybe threads just don’t work on .Net Web apps.  I then thought, well maybe its just my 1 CPU.  Well, I haven’t yet upgraded to the second CPU, to test this out.

But believe me, when I get the chance, I will.

Scott

Draw a Circle Graphic with ESRI Android SDK

14. November 2011 12:56 by Scott in Development, ESRI  //  Tags: , , ,   //   Comments (3)

I wrote this code a while back.  The requirement was to have a user draw a circle with the ESRI Android SDK.  Well, their Android SDK doesn't currently support drawing circles.  It draws all other types of polygons except for the circle.  So what does a Software Engineer do, we come up with our own implementation.

So for the world to see, here it is:

First we capture the OnMotionEvent and the comments are in line.

Hope this helps someone.

    public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {

        if (tempCircleGraphic != null)
            _graphicsLayer.removeGraphic(tempCircleGraphic);

        // creates a new polygon to be drawn.
        _circleTemp = new Polygon();

        Point point = _map.toMapPoint(to.getX(), to.getY());

        // If the starting point is null, create a polyline and start a
        // path.
        if (_startPoint == null) {
            _startPoint = _map.toMapPoint(from.getX(), from.getY());
            // creates a polyline so we can measure the radius of the
            // person
            // drawing the circle.
            _polylineTemp = new Polyline();
            // starts the poly line
            _polylineTemp.startPath(_startPoint.getX(), _startPoint.getY());
        }
        // continues the draw of the poly line
        _polylineTemp.lineTo((float) point.getX(), (float) point.getY());

        // calculates the circle when getting ready to be drawn.
        int pointsAroundCircle = 50; // N
        double radius = _polylineTemp.calculateLength2D(); // radius
        for (int i = 0; i < pointsAroundCircle; i++) {
            double fi = 2 * Math.PI * i / pointsAroundCircle;
            double x = radius * Math.sin(fi + Math.PI) + _startPoint.getX();
            double y = radius * Math.cos(fi + Math.PI) + _startPoint.getY();
            if (i == 0) // starts the drawing of the circle. if
                        // beginning
                        // the for loop
                _circleTemp.startPath(x, y);
            else if (i == pointsAroundCircle - 1) // ends the circle
                                                    // when at
                                                    // the end of the
                // for loop.
                _circleTemp.closeAllPaths();
            else
                // continues drawing the cirlce while it iterates
                _circleTemp.lineTo(x, y);
        }
        // creates a new graphic and sets the geometry to a polygon.
        Graphic graphic = new Graphic();
        graphic.setGeometry(_circleTemp);

        int drawColor = getDrawColor();
        int transparentColor = Color.argb(POLYGON_ALPHA, Color.red(drawColor),
                Color.green(drawColor), Color.blue(drawColor));
        SimpleFillSymbol fillSymbol = new SimpleFillSymbol(transparentColor);
        graphic.setSymbol(fillSymbol);
        
        tempCircleGraphic = graphic;
        // add the updated graphic to graphics layer
        _graphicsLayer.addGraphic(graphic);

        // Refresh the graphics layer
        _graphicsLayer.postInvalidate();
        return true;

    }

java.lang.RuntimeException: Stub! On With JUnit and Android

2. November 2011 10:35 by Scott in Development, Android  //  Tags: , , ,   //   Comments (34)

Running into another Error I figured I would blog about.

So im getting a Stub Error while trying to JUnit test a library I built with some Android code inside of it.  The Error is as follows:

java.lang.RuntimeException: Stub!
    at android.graphics.Color.rgb(Color.java:9)
    at com.irad.conversion.gml.GmlConverter.GetAttributesFromGml(GmlConverter.java:446)
    at com.irad.conversion.gml.GmlConverter.ConvertGmlPolygonToGraphic(GmlConverter.java:562)
    at com.irad.conversion.gml.GmlConverter.ConvertGmlToEsriGraphic(GmlConverter.java:114)
    at com.irad.conversion.gml.test.GmlConverterTest.ConvertGmlToEsriPolygonRingsGraphicTest(GmlConverterTest.java:194)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

While, I couldn't figure out what was going on, I kept getting a message of Stub while running some Android API code.  Keep in mind I was testing with JUnit outside of the official Android Test Project.  And in that lies the problem.

Solution:

In order to run Android API code, you MUST run it on the emulator.  Which to me stinks and for something as easy as converting colors like I was feels needless, but I guess I must now convert all my JUnit tests to the Android JUnit Project Sadly.