Monthly ArchiveOctober 2007
Java & Programming 15 Oct 2007 08:02 pm
Reverse an Array in Place
I was just looking through some code. I noticed that an array needed to be reversed and that the original order was never needed again. This was a perfect time to reverse the array in place. This was Java so I looked in the Arrays class. But alas there wasn't a reverse for primitive arrays! Java Lists get a reverse method in the Collections class. But primitive arrays get no such love.
Also, I thought of using the asList method from Arrays, but that just seems like a ton of overhead.
So instead I wrote a quick little method for ints and any other primitive type.
Here is the int example:
-
public static void reverseInPlace(int[] iar) {
-
final int length = iar.length;
-
final int halfLength = length / 2;
-
final int lengthMinus1 = length - 1;
-
for (int i = 0; i <halfLength; ++i) {
-
final int index2 = lengthMinus1 - i;
-
-
//swap
-
int tmp = iar[i];
-
iar[i] = iar[index2];
-
iar[index2] = tmp;
-
}
-
}
Basically, this method just swaps the items from the front to back until reaching the middle of the array. If the loop continued on, it would just switch everything back and the original order would be preserved!
The other implementations for the rest of the primitive types can be found here.
Process Explorer & Utilities & Windows 12 Oct 2007 01:27 pm
Yet Another Reason to Love Process Explorer — Part 2
This is a bit different from my last post about Process Explorer. Process Explorer is so powerful that I accidentally killed a different process than I wanted. This wasn't through some fancy schmancy search with regular expressions and kill accident. I just highlighted a process and clicked the .
I then proceeded to click 'Yes' on the simple dialog: .
Notice the blackened out word at the end of the Dialog's text? Well that is essentially what my brain did when I read that dialog.
So, long story short I killed the wrong process. Nothing seemed to break for a few hours. Then when I went to debug a couple of things, I wasn't able to connect to a service that was supposed to be running. I restarted somethings on my end, then remembered, "Oh yeah, I killed a random process earlier." I restarted the killed service and lo and behold everything started working again.
Lesson learned here kids: Don't kill random processes and then wonder why stuff breaks!
Process Explorer is part of the Microsoft Sysinternals set of utilities.
Javascript & Programming & Web 12 Oct 2007 12:40 pm
onbeforeunload IE7 Woes!
Was just having a problem getting the IE7 onbeforeunload event to be canceled. Found this blog about it: OnBeforeUnload, IE7, Assigning event handlers to null and the problems that arise.
Pretty simple, just wrap the return with a boolean:
-
var goodExit = false;
-
window.onbeforeunload = confirmExit;
-
function confirmExit()
-
{
-
// normally this if-check wouldn't be here,
-
// cause the event handler was nulled elsewhere.
-
// But IE7 doesn't respect the null, and
-
// wants the handler to not return anything
-
// (not even null).
-
if (!goodExit)
-
{
-
return "Leaving this page will cause all unplaced orders to be discarded."+
-
" Are you sure you want to leave the page?";
-
}
-
// notice no return statement here!
-
}
-
</script>
-
-
. . . . .
-
-
<input type="image" src="<%=AppService.BASEDIR%>/images/lgrequest.gif" onclick="javascript: goodExit = true; window.onbeforeunload = null; document.forms[0].submit();" />
This example was stolen from Brandon Himes and his blog, The Busted Mug. Thank you Brandon!
Process Explorer & Utilities & Windows 08 Oct 2007 08:26 am
Yet Another Reason to Love Process Explorer — Part 1
Well, I just found another reason to love Process Explorer here: How To Identify What Programs Started svchost.exe in Windows.
This article describes how to use the command line and/or Process Explorer to find out what the hell svchost.exe is doing.
Open a command prompt and enter:
tasklist /svc /FI "IMAGENAME eq svchost.exe"
The above command will list all the svchost.exe processes and display the programs (DLL's) that have been started by svchost.exe.
Process Explorer is part of the Microsoft Sysinternals set of utilities.
Hopefully, this will become a series of posts. We'll see!
Java & Programming 05 Oct 2007 01:33 pm
A Good Reasoning to Nullify an Object!
If you overwrite a reference with a new object, the object is first created and /then/ the reference is overwritten, which means the object can be only GCed /after/ the new object has been created.
Usually this doesn’t matter. However, if you want to overwrite an object which is so big that it only fits once into the memory, you’ll need to null the reference before creating/assigning the new instance.
Eg:
//FatObject fits only once into memory
FatObject fatty;
fatty=new FatObject();
fatty=new FatObject();Will bomb with OOME. Whereas…
FatObject fatty;
fatty=new FatObject();
fatty=null;
fatty=new FatObject();Will be fine, because the second creation of the FatObject will trigger a full GC and the GC will be able to clear enough memory (since the old reference has been nulled).
Well, that rarely matters, but it’s good to know.
Jos Hirth wrote this in response to this post by Jeroen van Erp.
In this post, Jeroen was talking about a long standing bug/issue/misunderstanding of how Strings are handled in Java. The problem that occurs only comes up when taking a substring of a large String. If all that is wanted is the small substring, the substring must be wrapped by a new String call. This will allow the larger String to be garbage collected. Normally when a substring is taken, the underlying char array is shared. This is usually good practice, because it saves memory.
But, and this is a big but, if the substring is taken from a large string, then memory can be leaked, because a lot of memory is wasted in the underlying character array.
Now, why did I quote the above passage from the comment area? Cause I had never thought of the issue Jos brought up. It's interesting to think of an object of which only one can fit in memory. Let alone creating a second!
Uncategorized 05 Oct 2007 01:32 pm
Wow, it’s been a long time!
I haven't really posted in here for a long time.
But I think I am going to try to more. I want to turn this into a place where I take snippets of blogs or articles and place the relevant parts here. Keeping the links in this blog with the snippets will help me remember things. If the page disappears, well then at least I have this snippet!
(that is as long as this blog lives)