Java GC Part1 <ZZ>

 

What is Garbage Collection?

Garbage Collection – not just garbage collection

Garbage collection is a format of automatic memory management. Memory which has been dynamically allocated but which is no longer in use is reclaimed without intervention by the application. Only memory which is unreachable is freed.

Memory management involves a lot more than just reclaiming unused memory,it includes:

1). freeing memory

2). allocating memory

3). arranging memory

all of these are important and must be considered when choosing and tuning garbage collection policies.

Garbage Collection – not just a performance cost

Garbage collection pauses are easily identifiable pauses when the application is prevented from dong ‘real’ work, so garbage collection is often considered to be a necessary evil.

Garbage collection can provide performance benefits:

u       Faster freeing of memory: even without garbage collection,freeing memory takes time,some collectors can free memory much more quickly than free() can.

u       Faster memory allocation:allocating memory takes time,and it is particularly slow when the heap is fragmented, or multiple threads are contending for allocationg locks on the heap.

Garbage collection can help with both of these problems:

-Fragmentation: rearrange objects on contention to ensure no lengthy free-list searches are required.

-Contention: Batch-allocation chunks of heap to threads so they don’t have to go back to the central allocator for each new request.

u       Faster memory access not all memory access is equally fast. Garbage collection can speed up memory access by rearranging objects in memory.

Since memory access is one of the main things an application does, this can make a big performance difference.

 

What a cache is, and why u should care

 

Memory access is very slow compared to instruction processing. To prevent memory access from being a serious bottleneck, memory caches are added.

1). Most modern systems have a hierarchy of caches of increasing speed and decreasing size.

2). Access to objects already in the cache is far faster than pulling an object into the cache.

Improvements in processor speed continue to outpace improvements in memory speed, so caches are becoming increasingly important.

1). One benchmark measurement reports 45of CPU cycles were spent stalled waiting for memory requests.

2). Understanding caches is not just for low-level geeks anymore.

Locality: when an object is loaded into the cache, its neighbours are also loaded into the cache. This makes relative positions of objects important to performance.

1). An application will go much faster if objects which tend to be accessed around the same time are located near one another.

2). Locality can be improved by:

- allocating in an unfregmented heap;

- compacting the heap

- rearranging objects so they are near objects they reference.

 

Garbage collection and the cache

Garbage collection can hinder or help interaction with the cache.

-          Cache pollution: depending on the algorithm, garbage collection may visit quite a lot of memory during a collection. This means the cache won’t be right for the application because it will be full of stuff the garbage collection just visited.

-          Compaction: Means objects are closer to their neighbours and more likely to be in the cache.

-          Re-arrangement: Means objects are closer to their friends and more likely to be in the cache at the right time.

 

Understanding garbage collection pause times

Small pause times do not guarantee good throughput

Garbage collection does work and causes pauses and the pauses prevent my application from doing work so the shorter the pause the better”  --- Not true!!!

Reported work and hidden work:

two kinds of garbage collection work:

-  work done while the application is stopped.

-  work done concurrently with an application.

However, only the first kind work is reported , concurrent work still impacts the application. Most concurrent algorithms involve: 1). Extra work, such as write barriers

       2). Repeated work, when work is undone by application activity and needs redoing.

Reported pause times are only a crude <粗糙的>indicator of the actual garbage collection load.

Mean pause and total pause

Mean pauses can be short even when the total pause is long, so looking at individual pause is not enough.

- shrinking the heap will generally shorten pause times,however, collection will have to happen much more frequently.

- The total time will be higher and the application performance will be worse.

 

The relative impace of collection, allocation, and access

“Even when a garbage collector speeds a lot of time paused,application performance may be better”

Investing more time in collection can give big wins for allocation and access. For instance,compacting the heap. Collectors which compact the heap frequently give longer pauses, but allocation is much faster from a compacted heap and object access is also likely to be faster.

 

Small pause times do not even guarantee good response times.

Ok, I get that the application would go fater if I could tolerate long pauses, but response times are critical for my application so the shorter the pause times, the better off I will be”  --- Still not true!!!

Response times are not the same as pause times.

 

Why Provide more than one garbage collection policy?

-          Most JVMs provide several policies for different requirements and workloads.

-          None of the policies are bad, but the default is not necessarily best in every circumstance.

-          They differ in the following:

1). When and how is the work done?

2). What happens to garbage?

3). How is the heap laid out?

 

When and how is the work done?

Stop-the-world: All application threads are stopped during garbage collection, and is the most efficient.

Incremental: Garbage collections are divided into smaller partial collections, and it can reduces application pause.

Concurrent:  1).  Work appears to happen concurrently with application work;

                     2).  Often very very finely divided incremental;

                     3).  Usually needs a brief stop-the-world pause to finish up.

 

What happens to garbage? And what happens to what’s kept?

Free-list collectors: 1). The heap is searched for unreachable objects which are added to a list of free space.

                 2). New objects are allocated from the free list.

                 3). When the heap becomes fragmented, it is compacted by rearranging objects.

Copy Collectors:   1). Reachable objects are copied to fresh heap,what’s left is garbage

                 2). Collecting grabage is free!

                 3). Collecting not-garbage is not-free,especially for large objects.

 

How is the heap laid out?

Flat heap: everything is in one unstructured area.

Heap with large object area: very large objects are kept away from normal objects, cause large objects are expensive to allocate, and expensive to compact.

Generational:   1). Exploits the observation that most objects die young.

                         2). Divides the heap into generations

                            - younger generations are collected more frequently

                            - if a copying collector is used, collecting the young generations is very fast since collecting dead objects is free.

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24123206/viewspace-669556/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24123206/viewspace-669556/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值