Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Free memory from complex objects in Java

I try to do my best to explain my question. Maybe it's a bit abstract.

I read some literature about not invoking GC explictly in Java code, finalize method, pointing to null, etc.

I have some large XMLs files (customer invoices). Using Jaxb, the file marshals in a complex Java object. Its attributes are basic types (Integer, BigDecimal, String, etc.) but also class of other complex classes, list of other classes, list of classes with list as attribute, etc.

When I do my stuff with the object, I need to remove it from the memory. Some XML are very large and I can avoid a memory leak or OutOfMemoryError situation.

So, my questions are:

  • Is it enough to assign big object to null? I read that, if there are soft references, GC will not free the object.
  • Should I do a in deep clearing of the object, clearing all list, assigning null to the attributes, etc.?
  • What about JaxB (I'm using Java6, so JaxB is built in) and soft references? JaxB is faster than old JibX marshaller, but I don't know if it's worse in memory usage.
  • Should I wrap the megacomplex JaxB class with WeakReference or something like this?

Excuse me for mixing Java memory usage concepts, JaxB, etc. I'm studying the stability of a large running process, and the .hprof files evidence that all customers data of all invoices remains in memory. Excuse me if it's a simple, basic or rare question.

Thanks in advance

like image 940
yaki_nuka Avatar asked Jun 11 '13 15:06

yaki_nuka


People also ask

How will you free the allocated memory in Java?

In Java, the programmer allocates memory by creating a new object. There is no way to de-allocate that memory. Periodically the Garbage Collector sweeps through the memory allocated to the program, and determines which objects it can safely destroy, therefore releasing the memory.

What are complex objects in Java?

Complex objects are the objects that are built from smaller or a collection of objects. For example, a mobile phone is made up of various objects like a camera, battery, screen, sensors, etc.

How much memory is allocated to an object in Java?

Minimum object size is 16 bytes for modern 64-bit JDK since the object has 12-byte header, padded to a multiple of 8 bytes. In 32-bit JDK, the overhead is 8 bytes, padded to a multiple of 4 bytes.

How long will an object take up memory Java?

Memory usage of some typical Java objects Theoretically, a long requires 4 more bytes compared to an int. But because object sizes will typically be aligned to 8 bytes, a boxed Long will generally take up 24 bytes compared to 16 bytes for an Integer.


2 Answers

Unless something else points to parts of your big object (graph), assigning the big object reference null is enough.

Safest though, would be to use a profiler after your application has been running for a while, and look at the object references, and see if there's something that isn't properly GC'ed.

like image 183
Harald K Avatar answered Oct 16 '22 18:10

Harald K


Is it enough to assign big object to null? I read that, if there are soft references, GC will not free the object.

The short answer is yes. It is enough to assign (all strong references to) a big object to null - if you do this, the object will no longer be considered "strongly reachable" by the Garbage Collector.

Soft references will not be a problem in your case, because it's guaranteed that softly reachable objects will be garbage collected before an OutOfMemoryError is thrown. They might well prevent the garbage collector from collecting the object immediately (if they didn't, they'd act exactly the same as weak references). But this memory use would be "temporary", in that it would be freed up if it were needed to fulfil an allocation request.

Should I do a in deep clearing of the object, clearing all list, assigning null to the attributes, etc.?

That would probably be a bad idea. If the field values are only referenced by the outer big object, then they will also be garbage collected when the big object is collected. And if they are not, then the other parts of the code that reference them will not be happy to see that you're removing members from a list they're using!

In the best case this does nothing, and in the worst case this will break your program. Don't let the lure of this distract you from addressing the sole actual issue of whether your object is strongly-reachable or not.

What about JaxB (I'm using Java6, so JaxB is built in) and soft references? JaxB is faster than old JibX marshaller, but I don't know if it's worse in memory usage.

I'm not especially familiar with the relative time and space performance of those libraries. But in general, it's safe to assume a very strong "innocent until proven guilty" attitude with core libraries. If there were a memory leak bug, it would probably have been found, reported and fixed by now (unless you're doing something very niche).

If there's a memory leak, I'm 99.9% sure that it's your own code that's at fault.

Should I wrap the megacomplex JaxB class with WeakReference or something like this?

This sounds like you may be throwing GC "fixes" at the problem without thinking through what is actually needed.

If the JaxB class ought to be weakly referenced, then by all means this is a good idea (and it should be there already). But if it shouldn't, then definitely don't do this. Weak referencing is more a question of the overall semantics, and shouldn't be something you introduce specifically to avoid memory issues.

If the outer code needs a reference to the object, then it needs a reference - there's no magic you can do to have the intance be garbage collected yet still available. If it doesn't need a reference (beyond a certain point), then it doesn't need one at all - better to just nullify a standard [strong] reference, or let it fall out of scope. Weak references are a specialist situation, and are generally used when you don't have full control over the point where an object ceases to be relevant. Which is probably not the case here.

the .hprof files evidence that all customers data of all invoices remains in memory.

This suggests that they are indeed being referenced longer than is necessary.

The good news is that the hprof file will contain details of exactly what is referencing them. Look at an invoice instance that you would expect to have been GCed, and see what is referencing it and preventing it from being GCed. Then look into the class in question to see how you expect that reference to be freed, and why it hasn't been in this case.

All good performance/memory tweaking is based on measurements. Taking heap dumps, and inspecting the instances and references to them, is your measurements. Do this and act on the results, rather than trying to wrap things in WeakReferences on the hope that it might help.

like image 39
Andrzej Doyle Avatar answered Oct 16 '22 18:10

Andrzej Doyle