Question

I've run into a bit of a performance issue with Hibernate and I'm stuck.

I've got a nested foreach that iterates firstly through a map and then through each key within the map to create an object.

for(Map<String, Object> csvRow : csvRows){
    Contact contact  = new Contact();
    contact.setContactId(nextContactId);
    for(String key : csvRow.keySet())
    {
        contact.setDetails(key, csvRow.get(key));
        contactList.addContact(contact);
    }
    nextContactId++;
}

My problem arises in that the above code takes roughly 10 seconds to run. This is with a CSV input of about 2MB, but this service has to handle a file of 200MB (at least) efficiently. The reason I'm stuck is that the code below runs in roughly 1.6 seconds but only creates a single instance of a Contact object (which is fair enough) and I do see why this is occuring.

Contact contact  = new Contact();
for(Map<String, Object> csvRow : csvRows){

    contact.setContactId(nextContactId);

    for(String key : csvRow.keySet())
    {
        contact.setDetails(key, csvRow.get(key));
        contactList.addContact(contact);
    }
    nextContactId++;
}

I have tried the usual things like manually setting the object's id to null during each iteration of processing a key. Just to make things clearer, a contact object is not supposed to represent a complete person, rather it represents a piece of information for a contact. Each contact will have the same contactId, and I have a wrapper object to tie it all together, but that isn't pertinent to my question.

My question is thus: How can I keep the performance of the latter while still creating all the objects I need. Also I need to keep the object structure I have because I have no control over the source CSV which may be missing data left right and center, hence the object structure that I'm using. If you need more information, I'm eagerly awaiting your reply.

EDIT #1: Using Hibernate 4.2.8, Spring 3.2.6

Was it helpful?

Solution

Fixed the issue. It actually wasn't a Hibernate issue rather a memory management issue. See the solution here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top