ChatGPT解决这个技术问题 Extra ChatGPT

Best practice for creating millions of small temporary objects

What are the "best practices" for creating (and releasing) millions of small objects?

I am writing a chess program in Java and the search algorithm generates a single "Move" object for each possible move, and a nominal search can easily generate over a million move objects per second. The JVM GC has been able to handle the load on my development system, but I'm interested in exploring alternative approaches that would:

Minimize the overhead of garbage collection, and reduce the peak memory footprint for lower-end systems.

A vast majority of the objects are very short-lived, but about 1% of the moves generated are persisted and returned as the persisted value, so any pooling or caching technique would have to provide the ability to exclude specific objects from being re-used.

I don't expect fully-fleshed out example code, but I would appreciate suggestions for further reading/research, or open source examples of a similar nature.

Would the Flyweight Pattern be appropriate for your case? en.wikipedia.org/wiki/Flyweight_pattern
Do you need to encapsulate it in an object?
The Flyweight Pattern is not appropriate, because the objects do not share significant common data. As for encapsulating the data in an object, it is too large to be packed into a primitive, which is why I'm looking for alternatives to POJOs.

P
Peter Mortensen

Run the application with verbose garbage collection:

java -verbose:gc

And it will tell you when it collects. There would be two types of sweeps, a fast and a full sweep.

[GC 325407K->83000K(776768K), 0.2300771 secs]
[GC 325816K->83372K(776768K), 0.2454258 secs]
[Full GC 267628K->83769K(776768K), 1.8479984 secs]

The arrow is before and after size.

As long as it is just doing GC and not a full GC you are home safe. The regular GC is a copy collector in the 'young generation', so objects that are no longer referenced are simply just forgotten about, which is exactly what you would want.

Reading Java SE 6 HotSpot Virtual Machine Garbage Collection Tuning is probably helpful.


Experiment with the Java heap size to try to find a point where full garbage collection is rare. In Java 7 the new G1 GC is faster in some cases (and slower in others).
P
Peter Mortensen

Since version 6, the server mode of JVM employs an escape analysis technique. Using it you can avoid GC all together.


Escape analysis often disappoints, it is worth checking if the JVM has figured out what you're doing or not.
If you have experience using these option: -XX:+PrintEscapeAnalysis and -XX:+PrintEliminateAllocations. That would be great to share. Because I don't, saying honestly.
see stackoverflow.com/questions/9032519/… you will need to get a debug build for JDK 7, I admit I've not done that but with JDK 6 it's been successful.
P
Pierre Laporte

Well, there are several questions in one here !

1 - How are short-lived objects managed ?

As previously stated, the JVM can perfectly deal with a huge amount of short lived object, since it follows the Weak Generational Hypothesis.

Note that we are speaking of objects that reached the main memory (heap). This is not always the case. A lot of objects you create does not even leave a CPU register. For instance, consider this for-loop

for(int i=0, i<max, i++) {
  // stuff that implies i
}

Let's not think about loop unrolling (an optimisations that the JVM heavily performs on your code). If max is equal to Integer.MAX_VALUE, you loop might take some time to execute. However, the i variable will never escape the loop-block. Therefore the JVM will put that variable in a CPU register, regularly increment it but will never send it back to the main memory.

So, creating millions of objects are not a big deal if they are used only locally. They will be dead before being stored in Eden, so the GC won't even notice them.

2 - Is it useful to reduce the overhead of the GC ?

As usual, it depends.

First, you should enable GC logging to have a clear view about what is going on. You can enable it with -Xloggc:gc.log -XX:+PrintGCDetails.

If your application is spending a lot of time in a GC cycle, then, yes, tune the GC, otherwise, it might not be really worth it.

For instance, if you have a young GC every 100ms that takes 10ms, you spend 10% of your time in the GC, and you have 10 collections per second (which is huuuuuge). In such a case, I would not spend any time in GC tuning, since those 10 GC/s would still be there.

3 - Some experience

I had a similar problem on an application that was creating a huge amount of a given class. In the GC logs, I noticed that the creation rate of the application was around 3 GB/s, which is way too much (come on... 3 gigabytes of data every second ?!).

The problem : Too many frequent GC caused by too many objects being created.

In my case, I attached a memory profiler and noticed that a class represented a huge percentage of all my objects. I tracked down the instantiations to find out that this class was basically a pair of booleans wrapped in an object. In that case, two solutions were available :

Rework the algorithm so that I do not return a pair of booleans but instead I have two methods that return each boolean separately

Cache the objects, knowing that there were only 4 different instances

I chose the second one, as it had the least impact on the application and was easy to introduce. It took me minutes to put a factory with a not-thread-safe cache (I did not need thread safety since I would eventually have only 4 different instances).

The allocation rate went down to 1 GB/s, and so did the frequency of young GC (divided by 3).

Hope that helps !


3
3 revs, 2 users 69%

If you have just value objects (that is, no references to other objects) and really but I mean really tons and tons of them, you can use direct ByteBuffers with native byte ordering [the latter is important] and you need some few hundred lines of code to allocate/reuse + getter/setters. Getters look similar to long getQuantity(int tupleIndex){return buffer.getLong(tupleInex+QUANTITY_OFFSSET);}

That would solve the GC problem almost entirely as long as you do allocate once only, that is, a huge chunk and then manage the objects yourself. Instead of references you'd have only index (that is, int) into the ByteBuffer that has to be passed along. You may need to do the memory align yourself as well.

The technique would feel like using C and void*, but with some wrapping it's bearable. A performance downside could be bounds checking if the compiler fails to eliminate it. A major upside is the locality if you process the tuples like vectors, the lack of the object header reduces the memory footprint as well.

Other than that, it's likely you'd not need such an approach as the young generation of virtually all JVM dies trivially and the allocation cost is just a pointer bump. Allocation cost can be a bit higher if you use final fields as they require memory fence on some platforms (namely ARM/Power), on x86 it is free, though.


N
Nitsan Wakart

Assuming you find GC is an issue (as others point out it might not be) you will be implementing your own memory management for you special case i.e. a class which suffers massive churn. Give object pooling a go, I've seen cases where it works quite well. Implementing object pools is a well trodden path so no need to re-visit here, look out for:

multi-threading: using thread local pools might work for your case

backing data structure: consider using ArrayDeque as it performs well on remove and has no allocation overhead

limit the size of your pool :)

Measure before/after etc,etc


P
Peter Mortensen

I've met a similar problem. First of all, try to reduce the size of the small objects. We introduced some default field values referencing them in each object instance.

For example, MouseEvent has a reference to Point class. We cached Points and referenced them instead of creating new instances. The same for, for example, empty strings.

Another source was multiple booleans which were replaced with one int and for each boolean we use just one byte of the int.


Just out of interest: What did it buy you performance wise? Did you profile your application before and after the change, and if so, what were the results?
@Axel the objects use much less memory so GC is not called so often. Definitely we profiled our app but there was even visual effect of the improved speed.
P
Peter Mortensen

I dealt with this scenario with some XML processing code some time ago. I found myself creating millions of XML tag objects which were very small (usually just a string) and extremely short-lived (failure of an XPath check meant no-match so discard).

I did some serious testing and came to the conclusion that I could only achieve about a 7% improvement on speed using a list of discarded tags instead of making new ones. However, once implemented I found that the free queue needed a mechanism added to prune it if it got too big - this completely nullified my optimisation so I switched it to an option.

In summary - probably not worth it - but I'm glad to see you are thinking about it, it shows you care.


D
David Plumpton

Given that you are writing a chess program there are some special techniques you can use for decent performance. One simple approach is to create a large array of longs (or bytes) and treat it as a stack. Each time your move generator creates moves it pushes a couple of numbers onto the stack, e.g. move from square and move to square. As you evaluate the search tree you will be popping off moves and updating a board representation.

If you want expressive power use objects. If you want speed (in this case) go native.


r
rkj

One solution I've used for such search algorithms is to create just one Move object, mutate it with new move, and then undo the move before leaving the scope. You are probably analyzing just one move at a time, and then just storing the best move somewhere.

If that's not feasible for some reason, and you want to decrease peak memory usage, a good article about memory efficiency is here: http://www.cs.virginia.edu/kim/publicity/pldi09tutorials/memory-efficient-java-tutorial.pdf


Dead link. Is there another source for that article?
g
gyorgyabraham

Just create your millions of objects and write your code in the proper way: don't keep unnecessary references to these objects. GC will do the dirty job for you. You can play around with verbose GC as mentioned to see if they are really GC'd. Java IS about creating and releasing objects. :)


Sorry mate, I disagree with your approach... Java, like any programming language, is about solving a problem within it's constraints, if the OP is constrained by GC how are you helping him?
I'm telling him how Java actually works. If he is unable to dodge the situation of having millions of temp objects, the best advice could be, the temp class should be lightweight and he must ensure that he releases the references as soon as possible, not a single step anymore. Am I missing something?
Java supports creating garbage, and would clean it up for you, that much is true. If the OP can't dodge the creation of objects, and he is unhappy with time spent in GC it's a sad ending. My objection is to the recommendation you make to make more work for GC because that is somehow proper Java.
l
luke1985

I think you should read about stack allocation in Java and escape analysis.

Because if you go deeper into this topic you may find that your objects are not even allocated on the heap, and they are not collected by GC the way that objects on the heap are.

There is a wikipedia explanation of escape analysis, with example of how this works in Java:

http://en.wikipedia.org/wiki/Escape_analysis


I
Ilya Gazman

I am not a big fan of GC, so I always try finding ways around it. In this case I would suggest using Object Pool pattern:

The idea is to avoid creating new objects by store them in a stack so you can reuse it later.

Class MyPool
{
   LinkedList<Objects> stack;

   Object getObject(); // takes from stack, if it's empty creates new one
   Object returnObject(); // adds to stack
}

Using pool for small objects is quite a bad idea, you need a pool per thread to boot (or the shared access kills any performance). Such pools also perform worse than a good garbage collector. Last: the GC is godsend when dealing w/ concurrent code/structures - many algorithms are significantly easier to implement as naturally there is no ABA issue. Ref. counting in concurrent environment requires at least an atomic operation + memory fence (LOCK ADD or CAS on x86)
The managing of objects in the pool may be more expensive than letting the garbage collector run.
@ThorbjørnRavnAndersen Generally I agree with you, but note that detecting such difference is quite a challenge, and when you do come to conclusion that GC works better in your case, it must be a very unique case if such difference matters. How ever the other way around, it could possibly be that Object pool will save your app.
I simply don't get your argument? It is very hard to detect if GC is faster than object pooling? And therefore you should use object pooling? The JVM is optimized for clean coding and short lived objects. If those are what this question is about (which I hope if OP generates a million of them pr second) then it should only be if there is a provable advantage to switch to a more complex and error prone scheme as the one you suggest. If this is too hard to prove, then why bother.
M
Michael Röschter

Object pools provide tremendous (sometime 10x) improvements over object allocation on the heap. But the above implementation using a linked list is both naive and wrong! The linked list creates objects to manage its internal structure nullifying the effort. A Ringbuffer using an array of objects work well. In the example give (a chess programm managing moves) the Ringbuffer should be wrapped into a holder object for the list of all computed moves. Only the moves holder object references would then be passed around.