How to Pass Custom Parameters to Event Handler

Just getting started with Dojo. I want to pass a couple of custom parameters to an event handler. In jQuery, you can do it like this:

    customData: 'foo'
}, handlerFunction);

And customData can be accessed from handlerFunction like this:

function handlerFunction(event) {

I’m migrating a bit of jQuery code over to Dojo. How can I pass those parameters to the Dojo event handler?


Well, generaly, closures allow you to pass “hidden” parameters to a function:

function make_event_handler(customData){
    return function(evt){
        //customData can be used here
        //just like any other normal variable

So when connecting an event in dojo:

dojo.connect(node, 'onclick', make_event_handler(17));

Another possibility that I like a lot is using dojo.partial / dojo.hitch to create the closures for you.

function event_handler(customData, evt){

dojo.connect(node, 'onclick', dojo.partial(event_handler, 17))

Note that all of these these required your event handlers to be created with passing the extra parameter(s) in mind. I don’t know if you can do a more direct translation of the JQuery code since that would require extra massaging of the evt variable and I don’t think dojo does that.


Android Bitmap Limit – Preventing java.lang.OutOfMemory

I’m currently struggling with an odd behavior of the Android platform — the Bitmap / Java heap memory limit. Depending on the device, Android limits the app developer to 16, 24, or 32 MiB of Java heap space (or you might find any random value on a rooted phone). This is arguably quite small, but relatively straightforward as I can measure usage with the following API’s:

Runtime rt = Runtime.getRuntime();
long javaBytes = rt.totalMemory() - rt.freeMemory();
long javaLimit = rt.maxMemory();

Easy enough; now for the twist. In Android, bitmaps, with few exceptions, are stored in the native heap and do not count towards the Java heap. Some bright-eyed, purist developer at Google decided that this was “bad” and allowed the developer to get “more than their fair share.” So there is this nice little piece of code that calculates the native memory usage incurred by bitmaps, and possibly other resources, and sums that with the Java heap and if you go over ….. java.lang.OutOfMemory. ouch

But no big deal. I have a lot of bitmaps, and don’t need all of them all the time. I can “page out” some of the ones that aren’t being used at the moment:

So, for attempt #1, I refactored the code so I could wrap every single bitmap load with a try/catch:

while(true) {
    try {
        return BitmapFactory.decodeResource(context.getResources(), android_id, bitmapFactoryOptions);
    } catch (java.lang.OutOfMemory e) {
        // Do some logging

        // Now free some space (the code below is a simplified version of the real thing)
        Bitmap victim = selectVictim();
        System.gc(); // REQUIRED; else, weird behavior ensues

See, here’s a nice little log snippet showing my code catching the exception, and recycling some bitmaps:

E/Epic    (23221): OUT_OF_MEMORY (caught java.lang.OutOfMemory)
I/Epic    (23221): ArchPlatform[android].logStats() -
I/Epic    (23221): LoadedClassCount=0.00M
I/Epic    (23221): GlobalAllocSize=0.00M
I/Epic    (23221): GlobalFreedSize=0.02M
I/Epic    (23221): GlobalExternalAllocSize=0.00M
I/Epic    (23221): GlobalExternalFreedSize=0.00M
I/Epic    (23221): EpicPixels=26.6M (this is 4 * #pixels in all loaded bitmaps)
I/Epic    (23221): NativeHeapSize=29.4M
I/Epic    (23221): NativeHeapAllocSize=25.2M
I/Epic    (23221): ThreadAllocSize=0.00M
I/Epic    (23221): totalMemory()=9.1M
I/Epic    (23221): maxMemory()=32.0M
I/Epic    (23221): freeMemory()=4.4M
W/Epic    (23221): Recycling bitmap 'game_word_puzzle_11_aniframe_005'
I/Epic    (23221): BITMAP_RECYCLING: recycled 1 bitmaps worth 1.1M).  age=294

Note how totalMemory – freeMemory is only 4.7 MiB, but with ~26? MiB of native memory taken up by bitmaps, we are in the 31/32 MiB range where we hit the limit. I’m still a bit confused here as my running tally of all loaded bitmaps is 26.6 MiB, yet the native alloc size is only 25.2 MiB. So I’m counting something wrong. But it’s all in the ballpark and definately demonstrates the cross-pool “summation” happening with the mem-limit.

I THOUGHT I had it fixed. But no, Android would not give up so easily…

Here’s what I get from two of my four test devices:

I/dalvikvm-heap(17641): Clamp target GC heap from 32.687MB to 32.000MB
D/dalvikvm(17641): GC_FOR_MALLOC freed <1K, 41% free 4684K/7815K, external 24443K/24443K, paused 24ms
D/dalvikvm(17641): GC_EXTERNAL_ALLOC freed <1K, 41% free 4684K/7815K, external 24443K/24443K, paused 29ms
E/dalvikvm-heap(17641): 1111200-byte external allocation too large for this process.
E/dalvikvm(17641): Out of memory: Heap Size=7815KB, Allocated=4684KB, Bitmap Size=24443KB, Limit=32768KB
E/dalvikvm(17641): Trim info: Footprint=7815KB, Allowed Footprint=7815KB, Trimmed=880KB
E/GraphicsJNI(17641): VM won't let us allocate 1111200 bytes
I/dalvikvm-heap(17641): Clamp target GC heap from 32.686MB to 32.000MB
D/dalvikvm(17641): GC_FOR_MALLOC freed <1K, 41% free 4684K/7815K, external 24443K/24443K, paused 17ms
I/DEBUG   ( 1505): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG   ( 1505): Build fingerprint: 'verizon_wwe/htc_mecha/mecha:2.3.4/GRJ22/98797:user/release-keys'
I/DEBUG   ( 1505): pid: 17641, tid: 17641
I/DEBUG   ( 1505): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000
I/DEBUG   ( 1505):  r0 0055dab8  r1 00000000  r2 00000000  r3 0055dadc
I/DEBUG   ( 1505):  r4 0055dab8  r5 00000000  r6 00000000  r7 00000000
I/DEBUG   ( 1505):  r8 000002b7  r9 00000000  10 00000000  fp 00000384
I/DEBUG   ( 1505):  ip 0055dab8  sp befdb0c0  lr 00000000  pc ab14f11c  cpsr 60000010
I/DEBUG   ( 1505):  d0  414000003f800000  d1  2073646565637834
I/DEBUG   ( 1505):  d2  4de4b8bc426fb934  d3  42c80000007a1f34
I/DEBUG   ( 1505):  d4  00000008004930e0  d5  0000000000000000
I/DEBUG   ( 1505):  d6  0000000000000000  d7  4080000080000000
I/DEBUG   ( 1505):  d8  0000025843e7c000  d9  c0c0000040c00000
I/DEBUG   ( 1505):  d10 40c0000040c00000  d11 0000000000000000
I/DEBUG   ( 1505):  d12 0000000000000000  d13 0000000000000000
I/DEBUG   ( 1505):  d14 0000000000000000  d15 0000000000000000
I/DEBUG   ( 1505):  d16 afd4242840704ab8  d17 0000000000000000
I/DEBUG   ( 1505):  d18 0000000000000000  d19 0000000000000000
I/DEBUG   ( 1505):  d20 0000000000000000  d21 0000000000000000
I/DEBUG   ( 1505):  d22 0000000000000000  d23 0000000000000000
I/DEBUG   ( 1505):  d24 0000000000000000  d25 0000000000000000
I/DEBUG   ( 1505):  d26 0000000000000000  d27 0000000000000000
I/DEBUG   ( 1505):  d28 00ff00ff00ff00ff  d29 00ff00ff00ff00ff
I/DEBUG   ( 1505):  d30 0000000000000000  d31 3fe55167807de022
I/DEBUG   ( 1505):  scr 68000012

That’s a native crash. A segfault no less (sig11). By definition, a segfault is ALWAYS a bug. This is absolutely an Android bug in the native code handling GC and/or mem-limit checking. But it’s still my app that crashes resulting in bad reviews, returns, and lower sales.

So I have to compute the limit myself. Except that I’ve struggled here. I tried adding up the pixels myself (EpicPixels), but I still hit the memcrash periodically, so I’m undercounting something. I tried adding the javaBytes (total – free) to NativeHeapAllocSize, but this would occassionally cause my app to become “anorexic”, freeing and freeing bitmaps until there was nothing left to purge.

  1. Does anyone know the exact computation used to calculate the memory limit and trigger java.lang.OutOfMemory?

  2. Has anyone else hit this issue and worked through it? Do you have any pearls of wisdom?

  3. Does anyone know which Google employee dreamed up this scheme so I can punch him for ruining ~40 hrs of my life? j/k

ANSWER: The limit is for NativeHeapAllocSize < maxMemory(); however, due to memory fragmentation, Android crashes well before the actual limit. Thus, you have to limit yourself to a value somewhat less than the actual limit. This “safety factor” is app dependent, but a few MiB seems to work for most people. (can I just say that I’m blown away by how broken this behavior is)


Use this snipplet, worked for me

 * Checks if a bitmap with the specified size fits in memory
 * @param bmpwidth Bitmap width
 * @param bmpheight Bitmap height
 * @param bmpdensity Bitmap bpp (use 2 as default)
 * @return true if the bitmap fits in memory false otherwise
public static boolean checkBitmapFitsInMemory(long bmpwidth,long bmpheight, int bmpdensity ){
    long reqsize=bmpwidth*bmpheight*bmpdensity;
    long allocNativeHeap = Debug.getNativeHeapAllocatedSize();

    final long heapPad=(long) Math.max(4*1024*1024,Runtime.getRuntime().maxMemory()*0.1);
    if ((reqsize + allocNativeHeap + heapPad) >= Runtime.getRuntime().maxMemory())
        return false;
    return true;


Here is an example of use

        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        if ( (runInSafeMemoryMode()) && (!Manager.checkBitmapFitsInMemory(bmpFactoryOptions.outWidth, bmpFactoryOptions.outHeight, 2)) ){
            Log.w(TAG,"Aborting bitmap load for avoiding memory crash");
            return null;        

Bulk insert in Java using prepared statements batch update

I am trying to fill a resultSet in Java with about 50,000 rows of 10 columns and then inserting them into another table using the batchExecute method of PreparedStatement.

To make the process faster I did some research and found that while reading data into resultSet the fetchSize plays an important role.

Having a very low fetchSize can result into too many trips to the server and a very high fetchSize can block the network resources, so I experimented a little bit and set up an optimum size that suits my infrastructure.

I am reading this resultSet and creating insert statements to insert into another table of a different database.

Something like this (just a sample, not real code):

for (i=0 ; i<=50000 ; i++) {
    statement.setString(1, "");
    statement.setLong(2, 1);
  • Will the executeBatch method try to send all the data at once ?
  • Is there a way to define the batch size?
  • Is there any better way to speed up the process of bulk insertion?

While updating in bulk (50,000 rows 10 cols), is it better to use a updatable ResultSet or PreparedStaement with batch execution?


I’ll address your questions in turn.

  • Will the executeBatch method tries to send all the data at once?

This can vary with each JDBC driver, but the few I’ve studied will iterate over each batch entry and send the arguments together with the prepared statement handle each time to the database for execution. That is, in your example above, there would 50,000 executions of the prepared statement with 50,000 pairs of arguments, but these 50,000 steps can be done in a lower-level “inner loop,” which is where the time savings come in. As a rather stretched analogy, it’s like dropping out of “user mode” down into “kernel mode” and running the entire execution loop there. You save the cost of diving in and out of that lower-level mode for each batch entry.

  • Is there a way to define the batch size

You’ve defined it implicitly here by pushing 50,000 argument sets in before executing the batch via Statement#executeBatch(). A batch size of one is just as valid.

  • Is there any better way to speed up the process of bulk insertion?

Consider opening a transaction explicitly before the batch insertion, and commit it afterward. Don’t let either the database or the JDBC driver impose a transaction boundary around each insertion step in the batch. You can control the JDBC layer with the Connection#setAutoCommit(boolean) method. Take the connection out of auto-commit mode first, then populate your batches, start a transaction, execute the batch, then commit the transaction via Connection#commit().

This advice assumes that your insertions won’t be contending with concurrent writers, and assumes that these transaction boundaries will give you sufficiently consistent values read from your source tables for use in the insertions. If that’s not the case, favor correctness over speed.

  • Is it better to use a updatable ResultSet or PreparedStatement with batch execution?

Nothing beats testing with your JDBC driver of choice, but I expect the latter—PreparedStatement and Statement#executeBatch() will win out here. The statement handle may have an associated list or array of “batch arguments,” with each entry being the argument set provided in between calls to Statement#executeBatch() and Statement#addBatch() (or Statement#clearBatch()). The list will grow with each call to addBatch(), and not be flushed until you call executeBatch(). Hence, the Statement instance is really acting as an argument buffer; you’re trading memory for convenience (using the Statement instance in lieu of your own external argument set buffer).

Again, you should consider these answers general and speculative so long as we’re not discussing a specific JDBC driver. Each driver varies in sophistication, and each will vary in which optimizations it pursues.


Is it possible to use struct-like constructs in Java?

I’m considering using Java for a large project but I haven’t been able to find anything that remotely represented structures in Java. I need to be able to convert network packets to structures/classes that can be used in the application.

I know that it is possible to use RandomAccessFile but this way is NOT acceptable. So I’m curious if it is possible to “cast” a set of bytes to a structure like I could do in C. If this is not possible then I cannot use Java.

So the question I’m asking is if it is possible to cast aligned data to a class without any extra effort beyond specifying the alignment and data types?


You’re basically asking whether you can use a C-specific solution to a problem in another language. The answer is, predictably, ‘no’.

However, it is perfectly possible to construct a class that takes a set of bytes in its constructor and constructs an appropriate instance.

class Foo {

  int someField;
  String anotherField;

  public Foo(byte[] bytes) {
    someField = someFieldFromBytes(bytes);
    anotherField = anotherFieldFromBytes(bytes);

You can ensure there is a one-to-one mapping of class instances to byte arrays. Add a toBytes() method to serialize an instance into bytes.


Android methods overriding

When we override a method in a subclass, we call the superclass method within this method, for example:

protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    width = w ;
    height = h ;

    Log.d(TAG, "onSizeChanged: width " + width + ", height "+ height);

    super.onSizeChanged(w, h, oldw, oldh);

So why do we need to call super.onSizeChanged()?

I delete the line super.onSizeChanged(), and the result is the same as with it.

Or the same in onCreate method, we call super.onCreate().


There are certain methods that do essential things, such as onCreate and onPause methods of activity. If you override them without calling the super method, those things won’t happen, and the activity won’t function as it should. In other cases (usually when you implement an interface) there is no implementation that you override, only declaration and therefore there is no need to call the super method.

One more thing, sometimes you override a method and your purpose is to change the behavior of the original method, not to extend it. In those cases you should not call thesuper method. An example for that is the paint method of swing components.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..