ASP.NET核心高内存消耗

时间:2018-03-19 19:34:50

标签: asp.net-core

我有一些ASP.NET核心项目,我看起来消耗的内存。我只是被它吓坏了。它不是那么大的项目。我有一些控制器,它消耗大约350MB的Ram,这对于Web服务器来说是大量的内存。

我的问题是我可以以某种方式减少它吗?我有一个想法是你为数据关系模型构建模型,但这不会减少我想要的mutch。还有其他一些方法,我想尝试一下:)

最后一点,我现在的ASP.NET核心是一个非常复杂的框架,这意味着我不会将它用于小项目,这只是用于教育目的。最后一个问题:记忆大问题是什么?

Screenshot of my proje

我更大的控制者:)

using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RustWiki.Data;
using RustWiki.Models.WikiViewModels;

namespace RustWiki.Controllers.api
{
[Produces("application/json")]
public class TableManagerController : Controller
{
    private readonly ApplicationDbContext _context;
    public TableManagerController(ApplicationDbContext context)
    {
        _context = context;
    }

    protected override void Dispose(bool disposing)
    {
        _context.Dispose();
        base.Dispose(disposing);
    }

    //Get
    [HttpGet]
    [Route("/api/ArmorTypes")]
    public IActionResult GetArmorTypes() => Ok(_context.ArmorTypes.ToList());

    [HttpGet]
    [Route("/api/Cloating")]
    public IActionResult GetCloating() => Ok(_context.Cloating.Include(c => c.ArmorType).Include(c => c.Defencis).Include(c => c.Item.ItemType).ToList());

    [HttpGet]
    [Route("/api/Defencis")]
    public IActionResult GetDefences() => Ok(_context.Defencis.ToList());

    [HttpGet]
    [Route("/api/Dmgs")]
    public IActionResult GetDmg() => Ok(_context.Dmgs.ToList());

    [HttpGet]
    [Route("/api/Equipment")]
    public IActionResult GetEquipment() => Ok(_context.Equipment.Include(e => e.Dmg).Include(e => e.Item.ItemType).ToList());

    [HttpGet]
    [Route("/api/Items")]
    public IActionResult GetItems() => Ok(_context.Items.Include(i => i.ItemType).ToList());

    [HttpGet]
    [Route("/api/ItemTypes")]
    public IActionResult GetItemType() => Ok(_context.ItemTypes.ToList());

    [HttpGet]
    [Route("/api/Resources")]
    public IActionResult GetResource() => Ok(_context.Resources.ToList());

    //Delete

    [HttpDelete]
    [Route("/api/ArmourTypes/{id}")]
    public IActionResult DelArmourType(int id) => Delete(_context.ArmorTypes, id);

    [HttpDelete]
    [Route("/api/Cloating/{id}")]
    public IActionResult DelCloating(int id) => Delete(_context.Cloating, id);

    [HttpDelete]
    [Route("/api/Defences/{id}")]
    public IActionResult DelDefencis(int id) => Delete(_context.Defencis, id);

    [HttpDelete]
    [Route("/api/Dmgs/{id}")]
    public IActionResult DelDmg(int id) => Delete(_context.Dmgs, id);

    [HttpDelete]
    [Route("/api/Equipments/{id}")]
    public IActionResult DelEquipment(int id) => Delete(_context.Equipment, id);

    [HttpDelete]
    [Route("/api/Items/{id}")]
    public IActionResult DelItem(int id) => Delete(_context.Items, id);

    [HttpDelete]
    [Route("/api/ItemTypes/{id}")]
    public IActionResult DelItemType(int id) => Delete(_context.ItemTypes, id);

    [HttpDelete]
    [Route("/api/Resources/{id}")]
    public IActionResult DelResource(int id) => Delete(_context.Resources, id);

    private IActionResult Delete<T>(DbSet<T> set, int id) where T : class
    {
        var obj = set.SingleOrDefault(delegate (T o)
        {
            return (o as IWikiModel)?.Id == id;
        });
        if (obj == null)
            return NotFound();
        set.Remove(obj);
        _context.SaveChanges();
        return Ok(obj);
    }

    //Create

    [HttpPost]
    [Route("/api/ArmourType")]
    public IActionResult CreateArmourType([FromBody]ArmorType type)
    {
        return Ok();
    }
}

}

感谢所有答案。

2 个答案:

答案 0 :(得分:1)

对于新手来说,任何框架都会很复杂。话虽如此,与完整框架相比,.NETCore更轻,更简化了运行时间,使其更适合小型项目。

当你说你的API耗费了大量内存时,它真的存在吗?您目前正在做什么基准测试?你是在本地运行它并查看内存消耗吗?如果是这样的话,Debug中运行的东西要比调试符号之类的应用程序代码更多,这会增加看似很小的应用程序的内存占用量。

如果您确实需要寻找内存泄漏或高消耗(这是针对您的情况的度量标准),那么请使用经过测试的探查器,如ANTS Profiler

答案 1 :(得分:1)

提示:确保在Startup.cs(IoC包含的地方)内设置了每个请求和服务的正确注册。 :)

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection#service-lifetimes-and-registration-options