Path: blob/master/src/hotspot/share/memory/metaspace/freeBlocks.hpp
40957 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#ifndef SHARE_MEMORY_METASPACE_FREEBLOCKS_HPP26#define SHARE_MEMORY_METASPACE_FREEBLOCKS_HPP2728#include "memory/allocation.hpp"29#include "memory/metaspace/binList.hpp"30#include "memory/metaspace/blockTree.hpp"31#include "memory/metaspace/counters.hpp"32#include "utilities/debug.hpp"33#include "utilities/globalDefinitions.hpp"3435class outputStream;3637namespace metaspace {3839// Class FreeBlocks manages deallocated blocks in Metaspace.40//41// In Metaspace, allocated memory blocks may be release prematurely. This is42// uncommon (otherwise an arena-based allocation scheme would not make sense).43// It can happen e.g. when class loading fails or when bytecode gets rewritten.44//45// All these released blocks should be reused, so they are collected. Since these46// blocks are embedded into chunks which are still in use by a live arena,47// we cannot just give these blocks to anyone; only the owner of this arena can48// reuse these blocks. Therefore these blocks are kept at arena-level.49//50// The structure to manage these released blocks at arena level is class FreeBlocks.51//52// FreeBlocks is optimized toward the typical size and number of deallocated53// blocks. The vast majority of them (about 90%) are below 16 words in size,54// but there is a significant portion of memory blocks much larger than that,55// leftover space from retired chunks, see MetaspaceArena::retire_current_chunk().56//57// Since the vast majority of blocks are small or very small, FreeBlocks consists58// internally of two separate structures to keep very small blocks and other blocks.59// Very small blocks are kept in a bin list (see binlist.hpp) and larger blocks in60// a BST (see blocktree.hpp).6162class FreeBlocks : public CHeapObj<mtMetaspace> {6364// _small_blocks takes care of small to very small blocks.65BinList32 _small_blocks;6667// A BST for larger blocks, only for blocks which are too large68// to fit into _smallblocks.69BlockTree _tree;7071// This verifies that blocks too large to go into the binlist can be72// kept in the blocktree.73STATIC_ASSERT(BinList32::MaxWordSize >= BlockTree::MinWordSize);7475// Cutoff point: blocks larger than this size are kept in the76// tree, blocks smaller than or equal to this size in the bin list.77const size_t MaxSmallBlocksWordSize = BinList32::MaxWordSize;7879public:8081// Smallest blocks we can keep in this structure.82const static size_t MinWordSize = BinList32::MinWordSize;8384// Add a block to the deallocation management.85void add_block(MetaWord* p, size_t word_size);8687// Retrieve a block of at least requested_word_size.88MetaWord* remove_block(size_t requested_word_size);8990#ifdef ASSERT91void verify() const {92_tree.verify();93_small_blocks.verify();94};95#endif9697// Returns number of blocks.98int count() const {99return _small_blocks.count() + _tree.count();100}101102// Returns total size, in words, of all elements.103size_t total_size() const {104return _small_blocks.total_size() + _tree.total_size();105}106107// Returns true if empty.108bool is_empty() const {109return _small_blocks.is_empty() && _tree.is_empty();110}111112};113114} // namespace metaspace115116#endif // SHARE_MEMORY_METASPACE_FREEBLOCKS_HPP117118119