OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/host_discardable_shared_memory_manager.h" | 5 #include "content/common/host_discardable_shared_memory_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/atomic_sequence_num.h" | 9 #include "base/atomic_sequence_num.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 break; | 352 break; |
353 | 353 |
354 // Stop eviction attempts when the LRU segment is currently in use. | 354 // Stop eviction attempts when the LRU segment is currently in use. |
355 if (segments_.front()->memory()->last_known_usage() >= current_time) | 355 if (segments_.front()->memory()->last_known_usage() >= current_time) |
356 break; | 356 break; |
357 | 357 |
358 std::pop_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime); | 358 std::pop_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime); |
359 scoped_refptr<MemorySegment> segment = segments_.back(); | 359 scoped_refptr<MemorySegment> segment = segments_.back(); |
360 segments_.pop_back(); | 360 segments_.pop_back(); |
361 | 361 |
362 // Attempt to purge and truncate LRU segment. When successful, as much | 362 // Attempt to purge LRU segment. When successful, released the memory. |
363 // memory as possible will be released to the OS. How much memory is | 363 if (segment->memory()->Purge(current_time)) { |
364 // released depends on the platform. The child process should perform | 364 #if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING) |
365 // periodic cleanup to ensure that all memory is release within a | 365 size_t size = segment->memory()->mapped_size(); |
366 // reasonable amount of time. | 366 DCHECK_GE(bytes_allocated_, size); |
367 if (segment->memory()->PurgeAndTruncate(current_time)) { | 367 bytes_allocated_ -= size; |
| 368 // Shrink memory segment. This will immediately release the memory to |
| 369 // the OS. |
| 370 segment->memory()->Shrink(); |
| 371 DCHECK_EQ(segment->memory()->mapped_size(), 0u); |
| 372 #endif |
368 ReleaseMemory(segment->memory()); | 373 ReleaseMemory(segment->memory()); |
369 continue; | 374 continue; |
370 } | 375 } |
371 | 376 |
372 // Add memory segment (with updated usage timestamp) back on heap after | 377 // Add memory segment (with updated usage timestamp) back on heap after |
373 // failed attempt to purge it. | 378 // failed attempt to purge it. |
374 segments_.push_back(segment.get()); | 379 segments_.push_back(segment.get()); |
375 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime); | 380 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime); |
376 } | 381 } |
377 | 382 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 | 424 |
420 enforce_memory_policy_pending_ = true; | 425 enforce_memory_policy_pending_ = true; |
421 base::MessageLoop::current()->PostDelayedTask( | 426 base::MessageLoop::current()->PostDelayedTask( |
422 FROM_HERE, | 427 FROM_HERE, |
423 base::Bind(&HostDiscardableSharedMemoryManager::EnforceMemoryPolicy, | 428 base::Bind(&HostDiscardableSharedMemoryManager::EnforceMemoryPolicy, |
424 weak_ptr_factory_.GetWeakPtr()), | 429 weak_ptr_factory_.GetWeakPtr()), |
425 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs)); | 430 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs)); |
426 } | 431 } |
427 | 432 |
428 } // namespace content | 433 } // namespace content |
OLD | NEW |